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);
}
}
gif player using java codethis code plays the file star.gif from the internet but i want it to p
Page 1 of 1
4 Replies - 3822 Views - Last Post: 24 July 2009 - 11:04 PM
#1
gif player using java code
Posted 23 July 2009 - 09:32 AM
Replies To: gif player using java code
#2
Re: gif player using java code
Posted 23 July 2009 - 09:43 AM
What problems are you having?
#3
Re: gif player using java code
Posted 24 July 2009 - 10:35 PM
NeoTifa, on 23 Jul, 2009 - 08:43 AM, said:
What problems are you having?
this java gif player plays a gif file that downloads from the internet ,i just want to make changes in this code so that the file plays from my computer i.e i should be able to load a gif file from my computer not the file from the internet
#4
Re: gif player using java code
Posted 24 July 2009 - 10:43 PM
Instead of reading from an URL read from a File
And don't post your question in the topic title it might get truncated
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);
}
for (int ii = 0; ii < names.length; ii++) {
File file = new File(.....file path....);
frames[ii] = ImageIO.read(file);
}
And don't post your question in the topic title it might get truncated
This post has been edited by pbl: 24 July 2009 - 10:45 PM
#5
Re: gif player using java code
Posted 24 July 2009 - 11:04 PM
Your quickest way would be just to delete lines up to
>>BufferedImage[] frames = new BufferedImage[names.length];
then
a. make the arguments to main be called 'names'
b. always use the same output file name
c. Use file URLs and change
>>URL url = new URL(pre + names[ii] + suff);
to
URL url = new URL(names[ii]);
>>BufferedImage[] frames = new BufferedImage[names.length];
then
a. make the arguments to main be called 'names'
b. always use the same output file name
c. Use file URLs and change
>>URL url = new URL(pre + names[ii] + suff);
to
URL url = new URL(names[ii]);
Page 1 of 1

New Topic/Question
Reply



MultiQuote







|