javajpeg库的简单介绍

博主:adminadmin 2023-01-23 06:42:07 327

今天给各位分享javajpeg库的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

用JAVA做一个JPEG图像生成器

运行下试试!·

import java.awt.*;

import java.util.*;

import java.awt.geom.*;

import java.awt.image.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.*;

import com.sun.image.codec.jpeg.*;

class Point

{int x,y;

Point(int x,int y)

{this.x=x;this.y=y;

}

}

public class MakeJPEG extends Canvas implements MouseMotionListener,MouseListener,ActionListener

{

int x=-1,y=-1,橡皮擦通知=0,清除通知=0;

Vector v=null;int n=1;

Graphics2D ggg ;

BufferedImage image;

Frame window;

Button 保存,调色板,橡皮,清除,画笔,获取屏幕,绘制图形;

Color 画笔颜色;

Panel pCenter,pSouth,pNorth;

public MakeJPEG()

{

保存=new Button("将绘制的图形或屏幕保存为JPG文件");

获取屏幕=new Button("获取屏幕");

绘制图形=new Button("绘制图形");

调色板=new Button("打开调色板");

画笔=new Button("画笔");

橡皮=new Button("橡皮");

清除=new Button("清除");

调色板.addActionListener(this);

绘制图形.addActionListener(this);

保存.addActionListener(this);

画笔.addActionListener(this);

橡皮.addActionListener(this);

清除.addActionListener(this);

获取屏幕.addActionListener(this);

画笔颜色=new Color(0,0,0);

addMouseMotionListener(this);

addMouseListener(this);

v=new Vector();

setBackground(Color.white);

image=new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);

ggg=image.createGraphics();

Rectangle2D rect=new Rectangle2D.Double(0,0,200,200);

ggg.setColor(getBackground());

ggg.fill(rect);

window=new Frame("JPEG图像生成器");

pCenter=new Panel();

pCenter.setLayout(null);

pCenter.add(this);

pCenter.setBackground(Color.gray);

this.setBounds(80,30,210,210);

window.add(pCenter,BorderLayout.CENTER);

pNorth=new Panel();

pNorth.add(保存);

pNorth.add(绘制图形);

pNorth.add(获取屏幕);

window.add(pNorth,BorderLayout.NORTH);

pSouth=new Panel();

pSouth.add(调色板);

pSouth.add(橡皮);

pSouth.add(清除);

pSouth.add(画笔);

window.add(pSouth,BorderLayout.SOUTH);

window.setVisible(true);

window.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

window.setBounds(100,80,390,380);

window.validate();

}

public void paint(Graphics g)

{

if(x!=-1y!=-1橡皮擦通知==0清除通知==0)

{

g.setColor(画笔颜色);

n=v.size();

for(int i=0;in-1;i++)

{

Point p1=(Point)v.elementAt(i);

Point p2=(Point)v.elementAt(i+1);

g.drawLine(p1.x,p1.y,p2.x,p2.y);

ggg.setColor(g.getColor());

ggg.drawLine(p1.x,p1.y,p2.x,p2.y);

}

}

else if(橡皮擦通知==1清除通知==0)

{

g.setColor(getBackground());

g.fillRect(x-2,y-2,4,4);

ggg.setColor(getBackground());

ggg.fillRect(x-2,y-2,4,4);

}

else if(清除通知==1橡皮擦通知==0)

{

g.setColor(getBackground());

g.fillRect(0,0,200,200);

ggg.setColor(getBackground());

ggg.fillRect(0,0,200,200);

}

g.drawImage(image,0,0,200,200,this);

}

public void mouseDragged(MouseEvent e)

{

x=(int)e.getX();

y=(int)e.getY();

Point p=new Point(x,y);

v.addElement(p);

repaint();

}

public void mouseMoved(MouseEvent e)

{}

public void mousePressed(MouseEvent e)

{}

public void mouseReleased(MouseEvent e)

{

v.removeAllElements();

}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void update(Graphics g)

{

{

paint(g);

}

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==橡皮)

{

橡皮擦通知=1;

清除通知=0 ;

}

else if(e.getSource()==清除)

{

清除通知=1;

橡皮擦通知=0;

repaint();

}

else if(e.getSource()==画笔)

{

橡皮擦通知=0;

清除通知=0;

}

else if(e.getSource()==保存)

{

FileDialog savedialog=new FileDialog(window,"保存图型到JPG格式",FileDialog.SAVE);

savedialog.setVisible(true);

if(savedialog.getFile()!=null)

{

try{

String fileName=savedialog.getFile();

FileOutputStream out=new FileOutputStream(fileName);

JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(image);

param.setQuality(1.0f,false);

encoder.setJPEGEncodeParam(param);

encoder.encode(image);

out.close();

}

catch(Exception EE)

{

}

}

}

else if(e.getSource()==获取屏幕)

{

Robot robot=null;

try{

robot=new Robot();

}

catch(Exception er)

{

}

Rectangle screenRect=null;

int width=getToolkit().getScreenSize().width;

int height=getToolkit().getScreenSize().height;

screenRect=new Rectangle(0,0,width,height);

window.setVisible(false);

this.window.setVisible(false);

image=robot.createScreenCapture(screenRect);

window.setVisible(true);

repaint();

}

else if(e.getSource()==调色板)

{

Color tempColor=JColorChooser.showDialog(window,"调色板",画笔颜色);

{

if(tempColor!=null)

{

画笔颜色=tempColor;

画笔.setForeground(画笔颜色);

}

}

}

else if(e.getSource()==绘制图形)

{

window.dispose();

this.window.dispose();

MakeJPEG canvas=new MakeJPEG();

}

}

public static void main(String args[])

{

new MakeJPEG();

}

}

Java支持GIF和JPEG图像格式吗

Java1.0支持显示GIF和JPEG格式的图像文件,这些文件的扩展名为.gif、.jpg或.jpeg。在java.awt.image包、java.awt包和java.applet包中都有对图像的支持

java怎么把jpeg图片存到access2003里,然后再读出来。

设置成OLE字段,然后参考如下代码:

MS Access在测试阶段比较利于携带,Web开发初期我经常使用它。后期再移植到SQLServer或Oracle上。但最近在需要对数据库中插入图片文件时,发现了一个问题,即JDK自带的JDBC-ODBC不支持java.sql.Blob里的方法,经过查阅Java API和程序调试,我找到了个变通的方法,即:

1,在写入BLOB类型字段时,使用java.sql.PreparedStatement的setBinaryStream方法,

2,读出BLOB类型字段时,因为返回的是字节数组byte[]类型,可以把它转换成ByteArrayInputStream然后读出内容写到文件里去。

这样即使用JDK自带的JDBC-ODBC驱动, 也能自如的在数据库里读写上传下载的文件了,哈哈。

import java.sql.*;

import java.io.*;

//对BLOB字段先写入(要求被写入的文件存在),再读出来

//要求先建立一个item表,有三个字段,id(int),file_name(char),file_blob(blob)

//对Access, blob字段应该设置成为“OLE对象”类型

public class blobtest{

public static void main(String[] args){

Connection conn = null;

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

/*这里的数据库的url一定要写正确,这是关键,其中DBQ可以绝对路径,也可以是相对路径,为了体现数据存储路径的/独立性,你可以将数据库copy到不同的位试一下*/

String dbUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=blob.mdb";

conn = DriverManager.getConnection(dbUrl,"","");

File file1=new File("fileToWrite.doc");

File file2=new File("fileRead.doc");

//BlobWriteForOracle( conn, file1);

//BlobReadForOracle( conn, file2);

BlobWriteForAccess( conn, file1);

BlobReadForAccess( conn, file2);

conn.close();

}catch(Exception ex){

System.err.println(ex.getMessage());

public static void BlobWriteForAccess( Connection conn, File file){

try{

conn.setAutoCommit(false); // 取消Connection对象的auto commit属性

String file_name=file.getName();

// get maxid ( to avoid insert id repeatly )

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("select max(id) from item");

rs.next();

int maxid = rs.getInt(1);

//maxid = (maxid==null)?0:maxid;

int id = maxid+1 ;

//System.out.println("write_id="+id);

PreparedStatement pstmt = conn.prepareStatement( "insert into item ( id, file_name, file_blob ) values ( " + id + ", ? , ? )" );

FileInputStream in = new FileInputStream(file );

int length = in.available();

pstmt.setString( 1, file_name );

pstmt.setBinaryStream( 2, in , in.available() );

System.out.println( "插入了 "+ pstmt.executeUpdate ()+ " 行数据, "

+ "id =" + id

+ ", 文件名是" + file.toString() +" , 共 "+ length +" bytes" );

conn.commit();

pstmt.close();

}catch(Exception ex){

ex.printStackTrace();

System.out.print("["+ex.getMessage()+"]");

try{

conn.rollback();

}catch(SQLException sqle){

System.err.println(sqle.getMessage());

}

}

public static void BlobReadForAccess( Connection conn, File file){

try{

conn.setAutoCommit(false); // 取消Connection对象的auto commit属性

String file_name=file.getName();

// get maxid ( to avoid insert id repeatly )

Statement stmt1 = conn.createStatement();

ResultSet rs1 = stmt1.executeQuery("select max(id) from item");

rs1.next();

int maxid = rs1.getInt(1);

//maxid = (maxid==null)?0:maxid;

int id = maxid;

//System.out.println("read_id="+id);

String sql="SELECT file_blob FROM item WHERE id=" + id + ""; //

Statement stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery(sql);

rs.next();

Object obj1 = rs.getObject("file_blob"); // 得到BLOB对象

//System.out.println("type is :"+obj1.getClass().getName());

byte[] blob=(byte[])obj1;

FileOutputStream out=new FileOutputStream(file); // 建立输出流

ByteArrayInputStream in=new ByteArrayInputStream(blob); // 建立输入流

int size=1024;

byte[] buffer=new byte[size]; // 建立缓冲区

int len;

while((len=in.read(buffer)) != -1)

out.write(buffer,0,len);

in.close();

out.close();

conn.commit();

}catch(Exception ex){

ex.printStackTrace();

System.out.print("["+ex.getMessage()+"]");

try{

conn.rollback();

}catch(SQLException sqle){

System.err.println(sqle.getMessage());

}

关于javajpeg库和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。