Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedImage to Bytes Java

I am trying to get the bytearray from a bufferedImage but the length of my ByteArrayOutputstream is always zero and I get no bytes. This is what I am doing

any ideas why this is not working

BufferedImage scaledPicture = .....

ByteArrayOutputStream baos = new ByteArrayOutputStream( );
ImageIO.write(scaledPicture, extension, baos);
baos.flush();
byte[] toByteArray = baos.toByteArray();
baos.close();
return toByteArray;

Any ideas?

like image 917
Farouk Alhassan Avatar asked Apr 01 '11 02:04

Farouk Alhassan


2 Answers

What is the return value of ImageIO.write? Does ImageIO.getImageWritersByFormatName with your extension return at least 1 ImageWriter? If not, then the format may not be supported.

like image 109
Melv Avatar answered Sep 30 '22 23:09

Melv


Does the 'ImageIO.write(...)' call return true? If no, the JVM might be failing to find a writer of the appropriate format.

like image 31
Peter Wagener Avatar answered Sep 30 '22 22:09

Peter Wagener