带GIF动画帧的Java内存问题

时间:2014-11-25 16:23:05

标签: java memory gif

我想提取(主要是访问)gif文件的帧。

一个简单的gif,320 * 240 *(270帧)= 1.7mb但是当我提取它并尝试使用它时,它只是触及了java堆空间的顶层而我只是耗尽了内存。

我试图将图像保存到我的本地磁盘,这很好,但保存它需要更长的时间来显示预览。

我使用此https://gist.github.com/devunwired/4479231来获取帧。

现在我希望使用gif文件本身并使用随机访问概念访问每个帧。

GIF解码器我认为使用以前的图像数据,然后需要我读取所有N-1帧以达到帧数N.:(

请帮忙。

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案

感谢https://stackoverflow.com/a/16234122/2655623https://stackoverflow.com/a/8935070/2655623

由于 com.sun.imageio.plugins.gif。* 文件,您可能还会收到弃用警告。但那没关系。我将这些文件复制到我的项目中,以解决问题。现在我可以获取图像(通过索引搜索它们)而不将所有图像放入内存中。 ;)

编辑:请在您的libs中使用http://www.java2s.com/Code/Jar/a/Downloadaeawtjar.htm(替换com.sun以获取弃用警告)。

import com.sun.imageio.plugins.gif.GIFImageReader;
import com.sun.imageio.plugins.gif.GIFImageReaderSpi;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author Pasban
 */
public class GifReader {

    private GIFImageReader ir;
    private int count;

    public GifReader(File gif) {
        open(gif);
    }

    private void open(File gif) {
        count = 0;
        ir = new GIFImageReader(new GIFImageReaderSpi());
        try {
            ir.setInput(ImageIO.createImageInputStream(gif));
            count = ir.getNumImages(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public int getCount() {
        return count;
    }

    public BufferedImage getImage(int frame) {
        try {
            return ir.read(frame);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
相关问题