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

查看:121
本文介绍了使用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.

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

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