So I was wondering how I could convert an Image (from java.awt.Image) to a BufferedImage without loosing the GIFS animation. Im working on a method which gets a byte[]
(an Image as byte array) then I need to translate it to a Image
(with a ByteArrayInputStream
), resize it, and then return the newly resized image back as byte[]
. The problem is BufferedImage.getScaledInstace(...)
returns an java.awt.Image
, but I need a BufferedImage
to convert it back to a byte[]
.
My code so far:
private byte[] resizeImage(byte[] bytes, String format, int size) throws IOException {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes));
double ratio = (double) image.getHeight() / (double) image.getWidth();
Image newImage = image.getScaledInstance(size, (int) (size * ratio), Image.SCALE_SMOOTH);
// TODO: Convert into byte array
return null;
}