BufferedImage与byte[]互转

转载:http://www.cnblogs.com/XL-Liang/archive/2011/12/30/2307220.html


一、需要用到的类

java.awt.image.BufferedImage;

javax.imageio.ImageIO;

java.io.*;

 

二、为什么要将BufferedImage转为byte数组

 

在传输中,图片是不能直接传的,因此需要把图片变为字节数组,然后传输比较方便;只需要一般输出流的write方法即可;

而字节数组变成BufferedImage能够还原图像;

 

三、如何取得BufferedImage

 

BufferedImage image = ImageIO.read(new File("1.gif"));

 

四、BufferedImage  ---->byte[]

 

ImageIO.write(BufferedImage image,String format,OutputStream out);方法可以很好的解决问题;

参数image表示获得的BufferedImage;

参数format表示图片的格式,比如“gif”等;

参数out表示输出流,如果要转成Byte数组,则输出流为ByteArrayOutputStream即可;

执行完后,只需要toByteArray()就能得到byte[];

 

五、byte[] ------>BufferedImage

 

ByteArrayInputStream in = new ByteArrayInputStream(byte[]b);    //将b作为输入流;

BufferedImage image = ImageIO.read(InputStream in);     //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();

 

六、显示BufferedImage

 

public void paint(Graphics g){

super.paint(g);

g.drawImage(image);    //image为BufferedImage类型

}

 

七、实例

 

要求:编写一个网络程序,通过Socket将图片从服务器端传到客户端,并存入文件系统;

Server端:

复制代码
package org.exam3;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.net.ServerSocket;
import java.net.Socket;

import javax.imageio.ImageIO;

public class T6Server {

public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8888);
Socket s = server.accept();
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
BufferedImage image = ImageIO.read(new File("1.gif"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean flag = ImageIO.write(image, "gif", out);
byte[] b = out.toByteArray();
dout.write(b);
s.close();
}

}
复制代码



Client端:

 

复制代码
package org.exam3;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.net.Socket;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class T6Client extends JFrame {
JButton button;
MyPanel panel;
public T6Client() {
setSize(300, 400);
button = new JButton("获取图像");
add(button,BorderLayout.NORTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
Socket s = new Socket("localhost",8888);
PrintWriter out = new PrintWriter(s.getOutputStream());
out.print("a");
DataInputStream in = new DataInputStream(s.getInputStream());
byte[]b = new byte[1000000];
in.read(b);
ByteArrayInputStream bin = new ByteArrayInputStream(b);
BufferedImage image = ImageIO.read(bin);
ImageIO.write(image, "gif", new File("2.gif"));
s.close();
} catch (Exception e) {
}
}
});
panel = new MyPanel();
add(panel);

}
public static void main(String[] args) throws Exception {
T6Client frame = new T6Client();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}
复制代码

本文摘自:http://blog.csdn.net/xiazdong/article/details/6929975


你可能感兴趣的:(编码学习)