Is there a way to java decompose * .gif on image []. Very, very urgent
1 answer
This is how you get raw frames from gif , but they will only contain deltas between frames:
static List<BufferedImage> getFramesRaw(File gif) throws IOException { List<BufferedImage> frames = new ArrayList<>(); ImageReader ir = new GIFImageReader(new GIFImageReaderSpi()); ir.setInput(ImageIO.createImageInputStream(gif)); for(int i = 0; i < ir.getNumImages(true); i++) frames.add(ir.read(i)); return frames; } And this is how you can glue them together into whole pictures:
static List<BufferedImage> getFrames(File gif) throws IOException { List<BufferedImage> copies = new ArrayList<>(); List<BufferedImage> frames = getFramesRaw(gif); copies.add(frames.remove(0)); for (BufferedImage frame : frames) { BufferedImage img = new BufferedImage(copies.get(0).getWidth(), copies.get(0).getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.drawImage(copies.get(copies.size()-1),0,0,null); g.drawImage(frame,0,0,null); copies.add(img); } return copies; } Checked on this gif
PS: as far as I know the format supports exotic situations and tricks, for example, when frames of different sizes, the code above does not take into account ..
|
