0

I would like to extract (mainly access) frames of a gif file.

A simple gif with 320*240*(270 frame) = 1.7mb but when I extract it and try to use it, it just hit the roof of java's heap space and I simply run out of memory.

I tried to save images to my local disk which it is fine, but saving it takes a little bit longer to show a preview.

I used this https://gist.github.com/devunwired/4479231 to get the frames.

Now I am looking to use the gif file itself and access each frame with random access concept.

GIF Decoders I think use the previous image data, and then it needs for me to read all N-1 frames to reach fram N. :(

Please help.

Soley
  • 1,716
  • 1
  • 19
  • 33
  • Show the code that actually reads in the data into something like (I presume) a byte array? What are your Java VM memory opts? –  Nov 25 '14 at 16:25
  • This is the code I am using. http://www.java2s.com/Code/Java/2D-Graphics-GUI/DecodesaGIFfileintooneormoreframes.htm I used netbeans memory profile and it showed me that when I load this gif, it really takes 75mb of memory for every time I load it. – Soley Nov 25 '14 at 16:28
  • It's trying to stuff everything into an ArrayList, it looks like. This is not going to go well. I don't know the problem domain well enough to advise, but perhaps you can find a 64-bit OS to run a 64-bit JVM on. This might give you enough headroom. –  Nov 25 '14 at 16:42

1 Answers1

0

Ok, I found the solution

Thanks to https://stackoverflow.com/a/16234122/2655623 and https://stackoverflow.com/a/8935070/2655623

You may also get a deprecation warning because of com.sun.imageio.plugins.gif.* files. But that is fine. I copied these files into my project so the issue is solved. Now I can get images (Search them by index) without putting all images into the memory. ;)

Edit: Please use http://www.java2s.com/Code/Jar/a/Downloadaeawtjar.htm (replacement for com.sun for deprecate warning) within your libs.

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;
    }
}
Community
  • 1
  • 1
Soley
  • 1,716
  • 1
  • 19
  • 33
  • Be aware that dependencies on com.sun classes will eventually fail, and not just when they are finally removed. –  Nov 25 '14 at 17:24
  • Unfortunately that is true. I would rather copy the dependencies within my project and use them. Is there any other way to overcome to this gif issue? – Soley Nov 25 '14 at 17:41
  • Now I need to use http://www.java2s.com/Code/Jar/a/Downloadaeawtjar.htm to prevent such issues. Thanks for the point. – Soley Nov 25 '14 at 17:50