I am trying to extract all the frames of an animated gif to an array of bufferedimages. I have been reading Convert each animated GIF frame to a separate BufferedImage and it was fairly easy to write each frame to a seperate file. But my problem comes up when I try to fill an ArrayList with the frames instead of writing them. Every image in the ArrayList is just the last frame of the gif.
To make it more clear, this code will write each frame to seperate files perfectly:
ArrayList<BufferedImage> frames = new ArrayList<BufferedImage>();
BufferedImage master = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
ImageReader ir = new GIFImageReader(new GIFImageReaderSpi());
ir.setInput(ImageIO.createImageInputStream(gif));
for (int i = 0; i < ir.getNumImages(true); i++)
{
master.getGraphics().drawImage(ir.read(i), 0, 0, null);
ImageIO.write(master, "gif", new File(dirGifs + "/frames" + i + ".gif"));
}
However, this code will only gives me an ArrayList full of the same frame (being the last frame of the gif)
ArrayList<BufferedImage> frames = new ArrayList<BufferedImage>();
BufferedImage master = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
ImageReader ir = new GIFImageReader(new GIFImageReaderSpi());
ir.setInput(ImageIO.createImageInputStream(gif));
for (int i = 0; i < ir.getNumImages(true); i++)
{
master.getGraphics().drawImage(ir.read(i), 0, 0, null);
frames.add(master);
}
I thought that it was because I wasnt disposing of the graphics afterwards, but I tried creating a graphics object and disposing it and nothing changed. Need help!