使用 ImageIO 创建动画 GIF? [英] Creating animated GIF with ImageIO?

查看:39
本文介绍了使用 ImageIO 创建动画 GIF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人设法说服 ImageIO 编写动画 GIF,特别是设置正确的元数据?我的第一次尝试大致如下(其中b1和b2是BufferedImages):

Has anybody managed to convince ImageIO to write an animated GIF, and in particular setting the correct metadata? My first attempt is roughly as follows (where b1 and b2 are BufferedImages):

ImageTypeSpecifier spec = new ImageTypeSpecifier(b1);
ImageWriter wr = ImageIO.getImageWriters(spec, "GIF").next();
wr.setOutput(ImageIO.createImageOutputStream(new File("C:\\Flashing.gif")));
ImageWriteParam param = wr.getDefaultWriteParam();
IIOMetadata meta = wr.getDefaultImageMetadata(spec, param);
wr.prepareWriteSequence(meta);
wr.writeToSequence(new IIOImage(b1, null, null), param);
wr.writeToSequence(new IIOImage(b2, null, null), param);

这似乎几乎可以工作,但是:

This appears to almost work, but:

  • 我显然需要以某种方式设置适当的"元数据来设置图像之间的时间并理想地使它们循环(我天真地希望默认值会做一些明智的事情,但是嘿...)
  • 它默认写入的任何元数据显然都不太正确:GIMP 在加载文件时会给出一些错误消息,尽管将图像嵌入到 Firefox 的测试网页中确实会非常快速地连续显示两个图像,这非常接近我想要的......

如果有人从 ImageIO 中编写了动画 GIF 并且可以分享工作代码片段或如何解决上述问题,我们将不胜感激!

If anyone has got writing animated GIFs from ImageIO to work and can share a snippet of working code or how to fix the above problems, it would be greatly appreciated!

推荐答案

我遇到了这个问题,决定尝试一下;创建一个可用的类(感谢ax)花费了少量但非平凡的费用——所以我想我可以分享代码:这里是一个小类,用于从数字创建动画 gif 图像的其他图像.存档版本

I ran across this question, and decided to try it out; It took a small but non-trivial amount create a usable class (thanks ax) -- so I thought I might share the code around: here is a small class for creating an animated gif image from a number of other images. Archived version

这篇关于使用 ImageIO 创建动画 GIF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆