Java:从没有 ImageReader 的动画 GIF 中获取帧 [英] Java: get frames from animated GIF without ImageReader

查看:33
本文介绍了Java:从没有 ImageReader 的动画 GIF 中获取帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 AppEngine 应用程序.它的功能之一是将动画 .gif 图像拆分为单独的帧.我已经搜索了很多以找到如何做到这一点的方法,最后找到了解决方案.不幸的是,该解决方案基于 ImageReader 而我无法在服务器上使用它,因为:

I'm developing AppEngine application. One of it's features is splitting an animated .gif image into separate frames. I've searched a lot to find the way how to do it and finally found the solution. Unfortunately the solution is based on ImageReader and I cant use it on the server, because:

Google App Engine 的 Java 不支持 javax.imageio.ImageReader运行环境

javax.imageio.ImageReader is not supported by Google App Engine's Java runtime environment

有没有其他方法可以在没有这个类的情况下解码 GIF 图像?

Are there any other ways to decode GIF-image without this class?

推荐答案

首先是关于框架本身的一些事情.将动画 .gif 图像拆分为单独的帧有两个含义.1) 从字面上看,帧是动画 GIF 意义上的帧.问题是构成动画 GIF 图像的帧是相关的.动画 GIF 的处理方法决定了在绘制当前帧时如何处理前一帧.你可以覆盖它;在绘制新框架之前填充背景颜色,或者您可以在绘制新框架之前做任何您认为合适的事情.如果你觉得上面的情况很复杂,那么框架的透明度呢?绘制每一帧的逻辑位置?

First some thing about frame itself. There are two implications about splitting an animated .gif image into separate frames. 1) Literally, a frame is a frame in the sense of an animated GIF. The problem is frames which constitute an animated GIF image are related. The disposal method of an animated GIF dictates what to do with the previous frame when drawing the current frame. You can override it; fill with background color before drawing the new frame, or you can do whatever you think appropriate before drawing the new frame. If you think the above situation is complicated, what about transparency of frames? logical position to draw each frames?

如果我们沿着这条路走,就不需要使用专用的ImageReader,只需读取图像的相关部分并复制每一帧数据,连同标题和调色板一起保存即可.结果是:生成的图像可能看起来很奇怪且毫无意义.看下面的例子:

If we go along this road, there is no need to use a dedicated ImageReader, just read relevant parts of the image and copy each frame data, save it along with a header and color palette. The consequence is: the resulting image might look weird and meaningless. Look at the example below:

第一帧

第二帧

和原来的

您可以看到第二帧看起来不太好.事实是,第二帧是建立在第一帧之上的透明帧(此动画 GIF 仅包含 2 帧).您希望看穿第二帧,然后他们一起制作动画.

You can see the second frame doesn't look so good. The truth is, the second frame is a transparent one which build on top of the first frame (this animated GIF only contains 2 frames). You are expect to see through the second frame and altogether, they make an animation.

现在让我们看看将动画 .gif 图像拆分为单独的帧的第二个含义.2) 在这种情况下,帧实际上是一个基于前一帧的合成,这就是我们在查看动画 GIF 时所看到的.为了实现这一点,我们必须考虑到帧循环的历史、每个帧的逻辑位置以及帧本身的透明度.

Now let's see what the second implication of splitting an animated .gif image into separate frames. 2) In this case, the frame is a actually is a composite which builds upon the previous frames and which is what we are seeing when viewing an animated GIF. In order to achieve this, we have to take into effect the history of the frame loop, the logical position of each frame, and the transparency of the frames themselves.

让我们看看我们现在得到了什么:

Let's see what we get now:

第一帧

第二帧

现在第一帧与第一种情况相同,但第二帧构建在第一帧之上,不再透明.

Now the first frame is the same as in the first situation, but the second frame is constructed on top of the first one and it's not transparent anymore.

在第二种情况下,我们必须对帧进行解码和编码才能达到预期的结果.除了看起来不错之外,另一个好处是您可以将生成的图像保存为编码器支持的任何格式.

In the second case, we do have to decode and encode the frames to achieve the desired result. Besides looking nice, another good thing about this is you can save the resulting images in any format the encoder support.

本文中的示例由iCafe

这篇关于Java:从没有 ImageReader 的动画 GIF 中获取帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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