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

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

问题描述

我有一个程序,可加载一张Spritesheet,在工作表上创建每个帧的BufferedImages并将其写入动画gif.使用Elliot Kroo在使用ImageIO创建动画GIF上提供的课程?,我能够成功输出文件.但是,gif的动画效果不完全正确.我提供的工作表是具有透明背景的.png文件,因此每个后续框架都放在最后一帧的顶部,并且在透明背景下显示出差异( 2qvawdx.gif"rel =" nofollow noreferrer>示例).这是使用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();

其中数据是作为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.

推荐答案

假定dataimageType参数/GifSequenceWriter/GifSequenceWriter.java"rel =" nofollow noreferrer> GifSequenceWriter 构造函数对于从.png文件读取的数据可能是TYPE_INT_ARGB.我希望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天全站免登陆