BufferedImage与byte[]互转 .

来源:互联网 发布:mac eclipse 真机调试 编辑:程序博客网 时间:2024/06/07 23:09
 

一、需要用到的类


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端:

view plaincopy to clipboardprint?
  1. package org.exam3;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.DataOutputStream;  
  6. import java.io.File;  
  7. import java.net.ServerSocket;  
  8. import java.net.Socket;  
  9.   
  10. import javax.imageio.ImageIO;  
  11.   
  12. public class T6Server {  
  13.   
  14.     public static void main(String[] args) throws Exception {  
  15.         ServerSocket server = new ServerSocket(8888);  
  16.         Socket s = server.accept();  
  17.         DataOutputStream dout = new DataOutputStream(s.getOutputStream());  
  18.         BufferedImage image = ImageIO.read(new File("1.gif"));  
  19.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  20.         boolean flag = ImageIO.write(image, "gif", out);  
  21.         byte[] b = out.toByteArray();  
  22.         dout.write(b);  
  23.         s.close();  
  24.     }  
  25.   
  26. }  

Client端:

view plaincopy to clipboardprint?
  1. package org.exam3;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Graphics;  
  5. import java.awt.event.ActionEvent;  
  6. import java.awt.event.ActionListener;  
  7. import java.awt.image.BufferedImage;  
  8. import java.io.ByteArrayInputStream;  
  9. import java.io.DataInputStream;  
  10. import java.io.File;  
  11. import java.io.PrintWriter;  
  12. import java.net.Socket;  
  13.   
  14. import javax.imageio.ImageIO;  
  15. import javax.swing.JButton;  
  16. import javax.swing.JFrame;  
  17. import javax.swing.JPanel;  
  18.   
  19. public class T6Client extends JFrame {  
  20.     JButton button;  
  21.     MyPanel panel;  
  22.     public T6Client() {  
  23.         setSize(300400);  
  24.         button = new JButton("获取图像");  
  25.         add(button,BorderLayout.NORTH);  
  26.         button.addActionListener(new ActionListener() {  
  27.             public void actionPerformed(ActionEvent event) {  
  28.                 try {  
  29.                     Socket s = new Socket("localhost",8888);  
  30.                     PrintWriter out = new PrintWriter(s.getOutputStream());  
  31.                     out.print("a");  
  32.                     DataInputStream in = new DataInputStream(s.getInputStream());  
  33.                     byte[]b = new byte[1000000];  
  34.                     in.read(b);  
  35.                     ByteArrayInputStream bin = new ByteArrayInputStream(b);  
  36.                     BufferedImage image = ImageIO.read(bin);  
  37.                     ImageIO.write(image, "gif"new File("2.gif"));  
  38.                     s.close();  
  39.                 } catch (Exception e) {  
  40.                 }  
  41.             }  
  42.         });  
  43.         panel = new MyPanel();  
  44.         add(panel);  
  45.   
  46.     }  
  47.     public static void main(String[] args) throws Exception {  
  48.         T6Client frame = new T6Client();  
  49.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  50.         frame.setVisible(true);  
  51.     }  
  52.   
  53. }  


原创粉丝点击