0

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;
}
CORRIT_
  • 55
  • 1
  • 8
  • 1
    It is not possible. An animated GIF is a *sequence* of images. But a `BufferedImage` represents a single image. – Stephen C Oct 20 '21 at 07:55
  • See also https://stackoverflow.com/questions/36054058 ... whose comments also say that it isn't possible to represent an animated GIF as a **single** `BufferedImage`. – Stephen C Oct 20 '21 at 08:00
  • The dup link gives a practical solution. – Stephen C Oct 20 '21 at 08:01

0 Answers0