import com.ImagePanel;
import com.GUIConsole;import javax.swing.*;
import java.io.*;public class ImagePanelTest { public static void main(String[] args) throws Exception{
String pathname = "d:/tmp-img.dat";
String imgpathname = "d:/131945.jpg"; ImageIcon imgIcon=new ImageIcon(imgpathname);
//写
ObjectOutputStream out=new ObjectOutputStream(
new FileOutputStream(new File(pathname)));
out.writeObject(imgIcon);
out.close();
//读
ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File(pathname)));
imgIcon=(ImageIcon)in.readObject();
in.close();
// 显示
ImagePanel imagePanel=new ImagePanel();
imagePanel.setImageIcon(imgIcon);
GUIConsole.run(imagePanel,imgIcon.getIconWidth(),imgIcon.getIconHeight());
}
}
import com.GUIConsole;import javax.swing.*;
import java.io.*;public class ImagePanelTest { public static void main(String[] args) throws Exception{
String pathname = "d:/tmp-img.dat";
String imgpathname = "d:/131945.jpg"; ImageIcon imgIcon=new ImageIcon(imgpathname);
//写
ObjectOutputStream out=new ObjectOutputStream(
new FileOutputStream(new File(pathname)));
out.writeObject(imgIcon);
out.close();
//读
ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File(pathname)));
imgIcon=(ImageIcon)in.readObject();
in.close();
// 显示
ImagePanel imagePanel=new ImagePanel();
imagePanel.setImageIcon(imgIcon);
GUIConsole.run(imagePanel,imgIcon.getIconWidth(),imgIcon.getIconHeight());
}
}
解决方案 »
- 在Java Swing中,如何做一个可用鼠标拖动改变内部面板大小的JFrame
- 有没有jbilder 9中文帮助文档?
- 哪位大哥有《完美编程》后面那些编程项目的答案,谢谢
- 如何获取桌面的宽度和高度
- JAVA关于界面自动关闭
- 怎样把存有数组的Vetctor对象直接转换为数组吗?
- 请问,如何在tree中向选中的节点(非叶节点)中,添加子节点呢
- 关于Float和Double的简单问题
- ************我想买两本书,帮忙推荐一下,up有分!*************
- 求高手点拨一个“JavaSE的程序中的"小异常"”.
- 面试题??
- 《JAVA 2 核心技术》(卷一)中文版第124页,例4-5的疑问
import java.sql.*;import alan.jdbc.common.ConnectionFactory;
import alan.jdbc.common.DatabaseUtils;
public class BlobOperation
{
//插入一个blob
public static void addLob(long id, String binFile) throws SQLException
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null; try
{
con = ConnectionFactory.getConnection(); //用你自己的方法获得连接
con.setAutoCommit(false); String sql = "INSERT INTO Blob_Tbl(id, binfile, bincontent)";
sql += " VALUES(?, ?, ?)";
ps = con.prepareStatement(sql); ps.setLong(1, id);
ps.setString(2, binFile);
ps.setBlob(3, oracle.sql.BLOB.empty_lob()); ps.executeUpdate();
DatabaseUtils.closeObject(ps); ps = con.prepareStatement("SELECT bincontent FROM Blob_Tbl WHERE id = " + id + " for update ");
rs = ps.executeQuery(); if (rs.next())
{
oracle.sql.BLOB binContent = (oracle.sql.BLOB) rs.getBlob(1); /* write blob content */
OutputStream binOut = binContent.getBinaryOutputStream(); //取得输出流
BufferedOutputStream out = new BufferedOutputStream(binOut);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(binFile));
int c;
while ((c = in.read()) != -1)
{
out.write(c); //真正的插入
}
in.close();
out.close();
}
con.commit();
} catch (Exception e)
{
e.printStackTrace();
try
{
con.rollback();
} catch (SQLException se)
{
}
throw new SQLException(e.getMessage());
} finally
{
DatabaseUtils.closeObject(rs, ps, con);
}
}
//读取一个blob
public static void fetchLob(long id, String filename) throws SQLException
{
Connection con = null;
Statement st = null;
ResultSet rs = null; try
{
con = ConnectionFactory.getConnection(); String sql = "SELECT * From Blob_Tbl Where id = " + id;
st = con.createStatement(); rs = st.executeQuery(sql);
while (rs.next())
{ String binFile = rs.getString("binfile");
oracle.sql.BLOB binContent = (oracle.sql.BLOB) rs.getBlob("bincontent"); /* read blob content */
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
BufferedInputStream in = new BufferedInputStream(binContent.getBinaryStream()); int c;
while ((c = in.read()) != -1)
{
out.write(c);
}
in.close();
out.close();
} } catch (Exception e)
{
throw new SQLException(e.getMessage());
} finally
{
DatabaseUtils.closeObject(rs, st, con);
}
} public static void main(String[] args) throws Exception
{
if (args.length == 0)
{
addLob(1, "a.jpg");
} else
{
fetchLob(1, "b.jpg"); //读取后输出到b.jpg
}
}
}
new FileOutputStream(new File(pathname)));
out.writeObject(imgIcon);
out.close();
new FileOutputStream(new File(pathname)));
out.writeObject(imgIcon);
out.close();
问题是:你为什么要把ImageIcon的图像写成图形文件?你的ImageIcon载入的图像从何而来?
不好取得图形数据啊,看看文档先。
但是,你在load一个图形到ImageIcon时,不是可以取得图形文件数据吗?ByteArrayOutputStream byteOut=new ByteArrayOutputStream();
//将图形文件数据写入 byteOut
//....
byte[] imgData=byteOut.toByteArray();
ImageIcon=new ImageIcon(imgData);在你的GUI中保持一个imgData变量就成。
实现Serializable是保证Object可以被“序列化”,和图象数据没有任何关系的。
写了个例子,应该满足你的要求了。package com.gui;import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;/**
* User: Saro
* Date: 2005-2-16
* Time: 22:54:26
*/
public class ImagePanel extends JPanel{
private ImageIcon imageIcon; public ImagePanel(){
JButton pasteBtn1 = new JButton("粘贴剪贴板上的图片");
this.add(pasteBtn1);
pasteBtn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
pasteBufferImage();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}); JButton pasteBtn2 = new JButton("粘贴剪贴板上的图形文件");
this.add(pasteBtn2);
pasteBtn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
pasteImageFile();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
} /**
* 粘贴剪贴板上的图形数据
*
* @throws UnsupportedFlavorException
* @throws IOException
*/
private void pasteBufferImage() throws UnsupportedFlavorException, IOException {
File imageDir=new File("f:");
Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();
BufferedImage img=(BufferedImage)cb.getContents(this).getTransferData(DataFlavor.imageFlavor);
File imagefile = new File(imageDir,"test.jpg");
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(imagefile));
writeImage(img,out);
out.close();
imageIcon=new ImageIcon(img);
paintAll(getGraphics());
} /**
* 粘贴图形文件
*
* @throws UnsupportedFlavorException
* @throws IOException
*/
private void pasteImageFile() throws UnsupportedFlavorException, IOException {
File imageDir=new File("f:");
Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();
java.util.List imageList=(java.util.List)cb.getContents(this).getTransferData(DataFlavor.javaFileListFlavor);
//假定只有一个图形文件
File imageFile=(File)imageList.get(0);
imageIcon=new ImageIcon(imageFile.getAbsolutePath());
paintAll(getGraphics());
} /**
* 将 BufferedImage写入数据流中
*
* @param image BufferedImage对象
* @param out 输出数据流
* @throws IOException
*/
public void writeImage(BufferedImage image,OutputStream out) throws IOException{
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.getDefaultJPEGEncodeParam(image).setQuality(1.0f, true);
encoder.encode(image);
} public void paint(Graphics g){
super.paint(g);
if(imageIcon!=null){
imageIcon.paintIcon(this,g,0,0);
}
}
}
1:同样是从JLabel中,利用getIcon方法获取的图片,系统如何判断哪些是BufferedImage类型,哪些
不是?目前的结果是,我ctrl+v拷贝上去的,都判断为是,而从数据库中获取然后显示上去的都判
断为不是,这个原因我还没想透。
2:为何无法作到将一个单纯的ImageIcon对象保存到本地,而必须通过提前保存其字节数组来作到?感
觉不应该的。或者问题可以转换为,如何获得ImageIcon对象的字节数组?哪些对象可以获得其对应
的字节数组?一般通过什么方式?
愿与大家探讨;-)