Thursday, March 19, 2009

How do I convert a Java Image to a png byte array?

The ImageIO class can be used to write an image as a JPEG encoded stream.If you write that stream to a ByteArrayOutputStream then you will end up with a byte array that contains the JPEG encoded image.


ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", out);
byte[] imageBytes = out.toByteArray();

No comments: