日期:2014-05-20 浏览次数:20848 次
package picToBin;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
class ImagePanel extends JPanel {
private BufferedImage image;
public ImagePanel() {
try {
byte[] b = fileT0Byte();
File file=getFileFromBytes(b);
//File file = (File) getObject(b);
image = ImageIO.read(file);
// image = ImageIO.read(new File("c:\\rose.jpg"));
} catch (IOException ex) {
// handle exception...
}
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
public static byte[] fileT0Byte() throws IOException {
String path = "c://2013-08.jpg";
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[fis.available()];
fis.read(b);// 写入到byte数组
return b;
}
// public static Object getObject(byte[] b) {
// Object o = null;
// try {
// ByteArrayInputStream bais = new ByteArrayInputStream(b);
// ObjectInputStream ois;
// ois = new ObjectInputStream(bais);
// o = ois.readObject();
// // ArrayList str = (ArrayList) ois.readObject(); //读取类
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// return o;
// }
public static File getFileFromBytes(byte[] b) {
BufferedOutputStream stream = null;
File file = null;
try {
file = new File("c:/tmp.jpg");
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e) {
e.printStackTrace();