从 BufferedImages 编写动画 gif

发布于 2024-11-28 00:51:15 字数 792 浏览 2 评论 0 原文

我有一个程序,可以加载 spritesheet,在表单上创建每个帧的 BufferedImages,并将其写入动画 gif。使用 Elliot Kroo 在 使用 ImageIO 创建动画 GIF? 上提供的课程,我能够成功输出文件。然而,gif 动画效果不太正确。我提供的工作表是具有透明背景的 .png,因此每个后续帧都放在最后帧的顶部,通过透明背景显示差异(示例)。这是使用 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 的数组(我也检查过,似乎不是问题)。这是 .gif 或 Java ImageWriter 的限制吗?或者我可以在某处编辑设置来防止这种情况发生吗?如果不需要的话我宁愿不放背景。

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();

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-12-05 00:51:15

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

graphicsControlExtensionNode.setAttribute("transparentColorFlag", "TRUE");
graphicsControlExtensionNode.setAttribute("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", ???);

See also this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文