从 BufferedImages 编写动画 gif [英] Writing an animated gif from BufferedImages

查看:32
本文介绍了从 BufferedImages 编写动画 gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以加载一个 spritesheet,为表单上的每一帧创建 BufferedImages,然后将它写入一个动画 gif.使用 Elliot Kroo 在 使用 ImageIO 创建动画 GIF? 上提供的类,我能够成功输出文件.但是,gif 动画不太正确.我提供的表格是一个带有透明背景的 .png,因此每个后续帧都放在最后一帧的顶部,通过透明背景显示差异(示例).这是使用 gif 编写器的代码:

I have a program that loads a spritesheet, creates BufferedImages of each frame on the sheet, and writes it to an animated gif. Using the class provided by Elliot Kroo on Creating animated GIF with ImageIO?, I was able to successfully output a file. However, the gif doesn't animate quite right. The sheet I provided was a .png with a transparent background, so every subsequent frame is just put on top of the last frames, with differences showing through the transparent background (Example). Here's the code that uses the gif writer:

ImageOutputStream output = new FileImageOutputStream(new File(savePath));
GifSequenceWriter writer = new GifSequenceWriter(output, data[0].getType(), delay, true);
for(BufferedImage bi:data)
{
    writer.writeToSequence(bi);
}
writer.close();
output.close();

其中 data 是作为 BufferedImage 的每个帧的数组(我也检查过,似乎不是问题).这是 .gifs 或 Java ImageWriters 的限制吗?或者我可以在某处编辑设置以防止这种情况发生吗?如果没有必要,我宁愿不添加背景.

Where data is an array of each frame as a BufferedImage (which I have also checked, and don't seem to be the problem). Is this a limitation of .gifs or the Java ImageWriters? Or can I edit a setting somewhere to prevent this? I'd rather not put a background if I don't have to.

推荐答案

假设 data 是一个 BufferedImageimageType 参数="http://elliot.kroo.net/software/java/GifSequenceWriter/GifSequenceWriter.java" rel="nofollow noreferrer">GifSequenceWriter 构造函数可能是 TYPE_INT_ARGB 用于从 .png 文件读取的数据.我希望 transparentColorFlagtrue,但您必须凭经验确定 transparentColorIndex.

Assuming data is an array of BufferedImage, the imageType parameter to the GifSequenceWriter constructor would likely be TYPE_INT_ARGB for data read from a .png file. I would expect transparentColorFlag to be true, but you'd have to determine the transparentColorIndex empirically.

graphicsControlExtensionNode.setAttribute("transparentColorFlag", "TRUE");
graphicsControlExtensionNode.setAttribute("transparentColorIndex", ???);

另请参阅此答案.

这篇关于从 BufferedImages 编写动画 gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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