有没有办法在Java中从多个图像创建一个Gif图像?[已关闭]

2022-09-01 19:08:36

我正在尝试设置一个简单的Java程序,从多个其他图像(jpg)创建一个动画gif。任何人都可以给我一个关于如何在Java中实现这一点的钩子吗?我已经搜索了谷歌,但找不到任何真正有用的东西。

谢谢你们!


答案 1

下面,您有一个从不同图像创建动画 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)的此代码。


答案 2

推荐