Re: Help with GIF writer in imageio
Dear Andrew,
Thanks very much for responding to this. I wanted to use the imageio
facilities, so persevered and have come up with the answer. I've
supplied some more snippets of code below that should help anyone else
who wants to do something similar.
The basis of my problems was that I was trying to add the
GraphicControlExtension to the stream meta data, rather than the image
meta data, and (as clearly seen in the documentation on the GIF meta
data) this is not where the GraphicControlExtension belongs. See (for
example):
http://java.sun.com/javase/6/docs/api/javax/imageio/metadata/doc-files/gif_metadata.html
To prepare some suitable image meta data, I used code like:
BufferedImage offScreenImage = // Prepare a BufferedImage
ImageWriter gifWriter = getWriter(offScreenImage); // my method to
create a writer
ImageWriteParam imageWriteParam = gifWriter.getDefaultWriteParam();
ImageTypeSpecifier imageTypeSpecifier = new
ImageTypeSpecifier(offScreenImage);
IIOMetadata imageMetaData =
gifWriter.getDefaultImageMetadata(imageTypeSpecifier,
imageWriteParam);
String metaFormatName = imageMetaData.getNativeMetadataFormatName();
IIOMetadataNode root = (IIOMetadataNode)
imageMetaData.getAsTree(metaFormatName);
IIOMetadataNode graphicsControlExtensionNode = getNode(root,
"GraphicControlExtension");
graphicsControlExtensionNode.setAttribute("disposalMethod", "none");
graphicsControlExtensionNode.setAttribute("userInputFlag", "FALSE");
graphicsControlExtensionNode.setAttribute("transparentColorFlag",
"FALSE");
graphicsControlExtensionNode.setAttribute("delayTime",
Integer.toString(timeBetweenFramesMS / 10));
graphicsControlExtensionNode.setAttribute("transparentColorIndex",
"0");
IIOMetadataNode commentsNode = getNode(root, "CommentExtensions");
commentsNode.setAttribute("CommentExtension", "Created by MAH");
IIOMetadataNode appEntensionsNode = getNode(root,
"ApplicationExtensions");
IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");
child.setAttribute("applicationID", "NETSCAPE");
child.setAttribute("authenticationCode", "2.0");
int loop = loopContinuously ? 0 : 1;
child.setUserObject(new byte[]{ 0x1, (byte) (loop & 0xFF), (byte)
((loop >> 8) & 0xFF)});
appEntensionsNode.appendChild(child);
imageMetaData.setFromTree(metaFormatName, root);
outputStream =
ScreenShotWriterThread.getOutputStream(frame, gifWriter,
suggestedFileName);
gifWriter.setOutput(outputStream);
Graphics offScreenGraphics = offScreenImage.createGraphics();
gifWriter.prepareWriteSequence(null);
for (int i = 0; i < nFrames; i++) {
// Draw into the BufferedImage, and then do
gifWriter.writeToSequence(new IIOImage(offScreenImage, null,
imageMetaData),
imageWriteParam);
}
gifWriter.endWriteSequence();
I have a convenience method to get or create the meta data nodes:
/**
Returns an existing child node, or creates and returns a new child
node (if the requested node
does not exist).
@param rootNode the <tt>IIOMetadataNode</tt> to search for the
child node.
@param nodeName the name of the child node.
@return the child node, if found or a new node created with the
given name.
*/
private static IIOMetadataNode getNode(IIOMetadataNode rootNode,
String nodeName) {
int nNodes = rootNode.getLength();
for (int i = 0; i < nNodes; i++) {
if (rootNode.item(i).getNodeName().compareToIgnoreCase(nodeName)
== 0) {
return((IIOMetadataNode) rootNode.item(i));
}
}
IIOMetadataNode node = new IIOMetadataNode(nodeName);
rootNode.appendChild(node);
return(node);
}
Andrew Thompson wrote:
m...@le.ac.uk wrote:
...
I ....would really appreciate some help
with writing animated GIF images.
I'd advise studying the code of an Animated GIF
encoding library that is known to work. Kevin
Weiner's library is often used*..
<http://www.fmsware.com/stuff/gif.html>
* I used it for the 'business end' of 'The Giifer'.
<http://www.physci.org/giffer/giffer.jnlp>
HTH
Andrew T.