GIF PLAYER JAVA CODE
Page 1 of 1
GIF PLAYER JAVA CODE
- Code:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import org.w3c.dom.Node;
class WriteAnimatedGif {
public static void configure(IIOMetadata meta,
String delayTime,
int imageIndex) {
String metaFormat = meta.getNativeMetadataFormatName();
if (!"javax_imageio_gif_image_1.0".equals(metaFormat)) {
throw new IllegalArgumentException(
"Unfamiliar gif metadata format: " + metaFormat);
}
Node root = meta.getAsTree(metaFormat);
//find the GraphicControlExtension node
Node child = root.getFirstChild();
while (child != null) {
if ("GraphicControlExtension".equals(child.getNodeName())) {
break;
}
child = child.getNextSibling();
}
IIOMetadataNode gce = (IIOMetadataNode) child;
gce.setAttribute("userInputFlag", "FALSE");
gce.setAttribute("delayTime", delayTime);
//only the first node needs the ApplicationExtensions node
if (imageIndex == 0) {
IIOMetadataNode aes =
new IIOMetadataNode("ApplicationExtensions");
IIOMetadataNode ae =
new IIOMetadataNode("ApplicationExtension");
ae.setAttribute("applicationID", "NETSCAPE");
ae.setAttribute("authenticationCode", "2.0");
byte[] uo = new byte[]{
//last two bytes is an unsigned short (little endian) that
//indicates the the number of times to loop.
//0 means loop forever.
0x1, 0x0, 0x0
};
ae.setUserObject(uo);
aes.appendChild(ae);
root.appendChild(aes);
}
try {
meta.setFromTree(metaFormat, root);
} catch (IIOInvalidTreeException e) {
//shouldn't happen
throw new Error(e);
}
}
/** Adapted from code via GeoffTitmus on
http://forums.sun.com/thread.jspa?messageID=9988198 */
public static void saveAnimate(
BufferedImage[] frames,
//should ideally be an array -one for each frame
String delayTime,
File file) throws Exception {
ImageWriter iw = ImageIO.getImageWritersByFormatName("gif").next();
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
iw.setOutput(ios);
iw.prepareWriteSequence(null);
for (int i = 0; i < frames.length; i++) {
BufferedImage src = frames[i];
ImageWriteParam iwp = iw.getDefaultWriteParam();
IIOMetadata metadata = iw.getDefaultImageMetadata(
new ImageTypeSpecifier(src), iwp);
configure(metadata, delayTime, i);
IIOImage ii = new IIOImage(src, null, metadata);
iw.writeToSequence(ii, (ImageWriteParam) null);
}
iw.endWriteSequence();
ios.close();
}
public static void main(String[] args) throws Exception {
String[] props = {
"java.vendor",
"java.version",
"java.vm.version",
"os.name",
"os.version"
};
StringBuffer sb = new StringBuffer();
String eol = System.getProperty("line.separator");
for (String prop : props) {
sb.append(prop);
sb.append(" \t");
sb.append(System.getProperty(prop));
sb.append(eol);
}
System.out.print(sb);
String name = "animatedstars.gif";
// alow a runtime argument to provide the OP file name
if (args.length > 0) {
name = args[0];
}
String[] names = {
"bronze",
"silver",
"gold",
"platinum"
};
String pre = "http://forums.sun.com/im/";
String suff = "-star.gif";
BufferedImage[] frames = new BufferedImage[names.length];
for (int ii = 0; ii < names.length; ii++) {
URL url = new URL(pre + names[ii] + suff);
System.out.println(url);
frames[ii] = ImageIO.read(url);
}
File f = new File(name);
// ensure we are getting a fresh file
if (f.exists()) {
if (!f.delete()) {
System.err.println("Cannot delete file! " + f);
System.exit(1);
}
}
f.createNewFile();
// save an animated GIF at 2 FPS.
saveAnimate(frames, "50", f);
JOptionPane.showMessageDialog(null, new ImageIcon(f.toURI().toURL()));
// show it in the system default viewer
Desktop.getDesktop().open(f);
}
}
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|