Mutual transfer between bufferedimage and byte []

Source: Internet
Author: User
I. classes to be used

Java. AWT. image. bufferedimage;

Javax. ImageIO. ImageIO;

Java. Io .*;

Ii. Why should I convert bufferedimage into a byte array?

During transmission, Images cannot be directly transmitted. Therefore, you need to change the image to a byte array for convenient transmission. You only need the write method of the output stream;

The byte array becomes bufferedimage, which can restore the image;

3. How to obtain bufferedimage

Bufferedimage image = ImageIO. Read (new file ("1.gif "));

4. bufferedimage ----> byte []

ImageIO. Write (bufferedimage image, string format, outputstream out); this method can solve the problem well;

The image parameter indicates the obtained bufferedimage;

The format parameter indicates the image format, such as "GIF;

The out parameter indicates the output stream. to convert it to a byte array, the output stream is bytearrayoutputstream;

After execution, you only need tobytearray () to get byte [];

5. byte [] ------> bufferedimage

Bytearrayinputstream in = new bytearrayinputstream (byte [] B); // use B as the input stream;

Bufferedimage image = ImageIO. Read (inputstream in); // use in as the input stream to read the image and store it in the image. Here, in can be bytearrayinputstream ();

6. Display bufferedimage

Public void paint (Graphics g ){

Super. Paint (g );

G. drawimage (image, X, Y, width, height, null); // The image is of the bufferedimage type.

}

To automatically call the paint method, you must callRepaint() Method;

VII. Instances

Requirement: Write a network program, upload images from the server to the client through socket, and store the images to the file system;

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 (8889); system. out. println ("the Server opens the connection... port: 8889 "); socket S = server. accept (); While (true) {system. out. println ("a client connects to the server and the server transmits images... "); dataoutputstream dout = new dataoutputstream (S. getoutputstream (); bufferedimage image = ImageIO. read (new file ("1.gif"); // read 1.gif and transmit bytearrayoutputstream out = new bytearrayoutputstream (); Boolean flag = ImageIO. write (image, "GIF", out); byte [] B = out. tobytearray (); dout. write (B); S. close (); system. out. println ("after the image is transferred, the server opens the connection... port 8889 "); s = server. accept ();}}}

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. bytearrayoutputstream; 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; jpanel Panel; int COUNT = 0; bufferedimage image; Public t6client () {setsize (300,400 ); button = new jbutton ("Get image"); add (button, borderlayout. north); button. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent event) {try {socket S = new socket ("localhost", 8889); PR Intwriter out = new printwriter (S. getoutputstream (); out. print ("A"); datainputstream in = new datainputstream (S. getinputstream (); byte [] B = new byte [1024]; bytearrayoutputstream bout = new bytearrayoutputstream (); int length = 0; while (length = in. read (B ))! =-1) {bout. write (B, 0, length);} bytearrayinputstream bin = new bytearrayinputstream (bout. tobytearray (); image = ImageIO. read (BIN); repaint (); ImageIO. write (image, "GIF", new file ("output-" + Count + ". GIF "); count ++; S. close () ;}catch (exception e) {}}); Panel = new jpanel (); add (panel) ;}@ overridepublic void paint (Graphics g) {super. paint (g); G. drawimage (image, 20, 20,300,150, null); // image is of the bufferedimage type} public static void main (string [] ARGs) throws exception {t6client frame = new t6client (); frame. setdefaclocloseoperation (jframe. exit_on_close); frame. setvisible (true );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.