Although it would be thought that performing this conversion should be something simple and logical, like calling a method within the Image class, but it is far from reality.
The way I do that process is to convert the image to a 'BufferedImage' which with a few extra steps is possible to write it into a 'ByteArrayOutputStream' to get the bytes of the image.
public static byte[] imageToBytes(Image image) throws IOException {
BufferedImage bImage = imageToBufferedImage(image);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", bos);
return bos.toByteArray();
}
public static BufferedImage imageToBufferedImage(Image image) {
BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics bg = bi.getGraphics();
bg.drawImage(image, 0, 0, null);
bg.dispose();
return bi;
}
Although it would be thought that performing this conversion should be something simple and logical, like calling a method within the Image class, but it is far from reality.
The way I do that process is to convert the image to a 'BufferedImage' which with a few extra steps is possible to write it into a 'ByteArrayOutputStream' to get the bytes of the image.