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

问题描述 投票:18回答:1

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

感谢你们!

java jpeg gif bufferedimage
1个回答
28
投票

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

Link

该类提供以下方法:

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道具。

© www.soinside.com 2019 - 2024. All rights reserved.