If I have a java.awt.Image object in hand, how can I get a byte array of the image data?
If I had the subclass BufferedImage I could do this:
ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
ImageIO.write( thumbnail , "jpeg" , baos ) ;
baos.flush();
final byte[] bytes = baos.toByteArray() ;
…where ImageIO.write takes an RenderedImage — an interface implemented by BufferedImage but not implemented by its superclass java.awt.Image.
Unfortunately, I do not have a BufferedImage or RenderedImage. After having called getScaledInstance on my original BufferedImage object to get a reduced thumbnail image, I have only a java.awt.Image object in hand. I am trying to get a byte array of the data in that java.awt.Image object.
BufferedImage- for example ;)imgscalrorjava-image-scalingrather than the outmoded and troublesomejava.awt.Image.getScaledInstanceroute I was using when I posted this Question.java.awt.Image.getScaledInstance, see The Perils of Image.getScaledInstance() Blog posted 2007-2015 by Chris Adamson.RescaleOp"scales" color sample values (making them lighter, darker etc), it does not change image dimensions. For that, useAffineTransformOp. Otherwise, good advice. I'd still use a library like imgscalr to get good quality thumbnails though... :-)