25

I am trying to set up a simple Java program that creates one single animated gif from multiple other images (jpg). Can anyone give me a hook on how to achieve this in Java? I already searched Google but couldn't find anything really helpful.

Thank you guys!

Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
user2399314
  • 521
  • 3
  • 8
  • 9

1 Answers1

33

Here you have an example of a class that creates an animated gif from different images:

Link

Edit: links seems to be dead. Anyway, just to be clear, this code was done by Elliot Kroo.

Edit 2: Thanks to @Marco13 for finding the WayBack Machine link. Updated the reference!

The class provides these methods:

class GifSequenceWriter {
    public GifSequenceWriter(
        ImageOutputStream outputStream,
        int imageType,
        int timeBetweenFramesMS,
        boolean loopContinuously);

    public void writeToSequence(RenderedImage img);

    public void close();
}

And also a little example:

public static void main(String[] args) throws Exception {
  if (args.length > 1) {
    // grab the output image type from the first image in the sequence
    BufferedImage firstImage = ImageIO.read(new File(args[0]));

    // create a new BufferedOutputStream with the last argument
    ImageOutputStream output = 
      new FileImageOutputStream(new File(args[args.length - 1]));

    // create a gif sequence with the type of the first image, 1 second
    // between frames, which loops continuously
    GifSequenceWriter writer = 
      new GifSequenceWriter(output, firstImage.getType(), 1, false);

    // write out the first image to our sequence...
    writer.writeToSequence(firstImage);
    for(int i=1; i<args.length-1; i++) {
      BufferedImage nextImage = ImageIO.read(new File(args[i]));
      writer.writeToSequence(nextImage);
    }

    writer.close();
    output.close();
  } else {
    System.out.println(
      "Usage: java GifSequenceWriter [list of gif files] [output file]");
  }
}

Props to Elliot Kroo for this code.

aran
  • 10,978
  • 5
  • 39
  • 69
  • 11
    It is good to include main part of linked content in answer, in case link goes offline. – Pshemo May 20 '13 at 12:44
  • 1
    You only included interface of class `GifSequenceWriter` (there is not body in constructor and methods, so we cant really use it). What is most important is [how was it implemented](http://elliot.kroo.net/software/java/GifSequenceWriter/GifSequenceWriter.java). – Pshemo May 20 '13 at 12:50
  • How do I use that code? – user2399314 May 20 '13 at 13:00
  • 1
    @user2399314 I updated this answer a little. Take a look at the link. You will find code of `GifSequenceWriter` class there. Now you can use it like in example shown in this answer. – Pshemo May 20 '13 at 13:09
  • @Asier Aranbarri there is only one problem: the gif animation does not behave like normal gif since it does not loop :( – user2399314 May 20 '13 at 15:42
  • 1
    @Asier Aranbarri I already fixed it. I just needed to change the loop setting from 'false' to 'true'. – user2399314 May 20 '13 at 17:23
  • @AsierAranbarri Does this compute a gif with 8-bit or 24-bit colors? – Michael Jul 03 '14 at 07:09
  • Where it generates the .gif file? – Jas Feb 02 '18 at 01:45
  • The last argument is the location of the out.file: // create a new BufferedOutputStream with the last argument ImageOutputStream output = new FileImageOutputStream(new File(args[args.length - 1])); – aran Feb 02 '18 at 11:45
  • It works, but the image quality is low. I'm having trouble setting it by tweaking ImageWriteParam settings. – Don Smith Aug 03 '19 at 18:18
  • 1
    @Pshemo thanks for that advice, years after. Links are sadly gone. – aran Jul 07 '20 at 00:20
  • 1
    The wayback machine still got it: https://web.archive.org/web/20191226041038/elliot.kroo.net/software/java/GifSequenceWriter/GifSequenceWriter.java – Marco13 Jul 12 '20 at 22:19
  • Not able to understand why the question has been closed. The problem statement is clear and the answer is spot on. – Arvind Kumar Avinash Mar 11 '21 at 06:27
  • Just in case Wayback Machine link breaks, someone has created a Github Gist with the same file -> https://gist.github.com/jesuino/528703e7b1974d857b36 – anemo Jun 04 '22 at 22:37