24

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!

4

1 回答 1

31

这里有一个从不同图像创建动画 gif 的类的示例:

关联

编辑:链接似乎已经死了。无论如何,为了清楚起见,这段代码是由 Elliot Kroo 完成的。

编辑 2:感谢您@Marco13找到 WayBack Machine 链接。更新了参考!

该类提供了这些方法:

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

    public void writeToSequence(RenderedImage img);

    public void close();
}

还有一个小例子:

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]");
  }
}

Elliot Kroo提供此代码的道具。

于 2013-05-20T12:35:52.417 回答