【问题标题】:Is there a way to create one Gif image from multiple images in Java? [closed]有没有办法从 Java 中的多个图像创建一个 Gif 图像? [关闭]
【发布时间】:2013-05-15 00:24:30
【问题描述】:

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

谢谢你们!

【问题讨论】:

  • 您是指动画 gif 吗?还是你想要一个由几个小 gif 组成的大 gif?或者您想使用 gif 的透明度将一个 gif 粘贴到另一个上?
  • 我想要一个 gif 动画。
  • 这个link 提供的信息远不止创建动画 GIF。
  • this 独立的动画 GIF 作家。

标签: java jpeg gif bufferedimage


【解决方案1】:

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

Link

编辑:链接似乎已失效。无论如何,为了清楚起见,这段代码是由 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

【讨论】:

  • 最好将链接内容的主要部分包含在答案中,以防链接脱机。
  • 你只包含了类GifSequenceWriter的接口(构造函数和方法中没有body,所以我们不能真正使用它)。最重要的是how was it implemented
  • @user2399314 我稍微更新了这个答案。看看链接。你会在那里找到GifSequenceWriter 类的代码。现在您可以像此答案中显示的示例一样使用它。
  • @Asier Aranbarri 我已经修好了。我只需要将循环设置从“假”更改为“真”。
  • @Pshemo 感谢您多年后的建议。可悲的是,链接不见了。
猜你喜欢
  • 1970-01-01
  • 2019-10-20
  • 1970-01-01
  • 2015-11-21
  • 2020-09-27
  • 2012-10-06
  • 2021-08-30
  • 2016-04-26
  • 2022-06-14
相关资源
最近更新 更多