[英]Creating animated GIF with ImageIO?
Has anybody managed to convince ImageIO to write an animated GIF, and in particular setting the correct metadata?有没有人设法说服 ImageIO 编写动画 GIF,特别是设置正确的元数据? My first attempt is roughly as follows (where b1 and b2 are BufferedImages):
我的第一次尝试大致如下(其中 b1 和 b2 是 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:这似乎几乎有效,但是:
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!如果有人从 ImageIO 编写动画 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.
创建一个可用的类(感谢ax )花费了少量但非平凡的时间 - 所以我想我可以分享代码:这是一个用于从许多其他图像创建动画 gif 图像的小类。 Archived version
存档版本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.