I've looked all over the place, but can't seem to find any easy to understand explanation. (I've found classes and methods written by other Java users that can do this, but I'm hoping to write my own.)
Asked
Active
Viewed 3,356 times
1 Answers
5
Here is the createImage()
method of GIFanim. Perhaps that will give you a start.
public byte[] createImage() throws Exception {
ImageWriter iw = ImageIO.getImageWritersByFormatName("gif").next();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
iw.setOutput(ios);
iw.prepareWriteSequence(null);
int i = 0;
for (AnimationFrame animationFrame : frameCollection) {
BufferedImage src = animationFrame.getImage();
ImageWriteParam iwp = iw.getDefaultWriteParam();
IIOMetadata metadata = iw.getDefaultImageMetadata(
new ImageTypeSpecifier(src), iwp);
configure(metadata, "" + animationFrame.getDelay(), i);
IIOImage ii = new IIOImage(src, null, metadata);
iw.writeToSequence(ii, null);
i++;
}
iw.endWriteSequence();
ios.close();
return os.toByteArray();
}
Note that this is a very naïve implementation, that produces images that are significantly larger than can be made with a library that compresses the color palette and performs other optimizations. Implementing a library like that would be a significant task.

Andrew Thompson
- 168,117
- 40
- 217
- 433
-
I couldn't find the library that this was taken from, on the website it was an end user application instead of a code library. Do you have a copy of the code base this is from? – Neil Flodin Dec 19 '11 at 16:58
-
@neilf did you finish that? – Anton Shkurenko Nov 26 '15 at 15:45