Can Java Image objects be paused or frames extracted?

I have animated GIF's in my game.

I'd like to be able to display one of the frames in the GIF, done in code, without making individual GIF files for the desired image / single frame.

Since I already have the image loaded (in the animation) can I make a new Image Object from one of the animation frames?

If not a new image object, can I tell an Image object to pause()  ?
beavoidAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:

I tested this one from
http://www.velocityreviews.com/forums/t147024-accessing-animated-gifs-with-imageio.html
and extracted all  frames of
http://www.iconbazaar.com/images/animated.gif
into separate gif files with this code:

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.*;

public class ExtractingFrames {
public static void main(String[] args) throws IOException {
Object input = new File("animated.gif");
// or Object input = new FileInputStream("animated.gif");
ImageInputStream stream = ImageIO.createImageInputStream(input);
Iterator readers = ImageIO.getImageReaders(stream);
if (!readers.hasNext())
throw new RuntimeException("no image reader found");
ImageReader reader = (ImageReader) readers.next();
reader.setInput(stream); // don't omit this line!
int n = reader.getNumImages(true); // don't use false!
System.out.println("numImages = " + n);
for (int i = 0; i < n; i++) {
BufferedImage image = reader.read(i);
 File outputfile = new File("images\\saved" + i + ".gif");
    ImageIO.write(image, "gif", outputfile);
System.out.println("image[" + i + "] = " + image);
}
stream.close();
}
}

Open in new window

0
 
for_yanCommented:
Get frammes from animated GIF:
http://www.java2s.com/Tutorial/Java/0419__PDF/GetFramefromAnimatedGif.htm

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.codec.GifImage;

public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    document.add(Image.getInstance("animated.gif"));
    GifImage img = new GifImage("animated.gif");
    int frames = img.getFrameCount();
    document.add(new Paragraph("There are " + frames + " frames in the animated gif file."));
    for (int i = 0; i < frames;) {
      ++i;
      document.add(img.getImage(i));
    }
    document.close();
  }
}

Open in new window

0
 
for_yanCommented:
See the code above for how to make individual images without writing them to files - they are of course forst extracted
and then written to files.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
beavoidAuthor Commented:
How can I change the last for loop to return the k-th image as an Image object, not as a bufferedImage ?

Can I "cast" a bufferedimage  to an Image Object?

I don't need to write it to an external file, I need it loaded in to an Image Object right away


// in last for loop above
if (i==k)    return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());

?

Will that be, as if, extracting a paused image?
0
 
for_yanCommented:
This gives example from buffered image to image drawing
http://www.javalobby.org/articles/ultimate-image/
0
 
beavoidAuthor Commented:
It looks like that is only relevant for BufferredImage objects. not plain Image objects

Mine are regular awt Image objects

Is there a bufferedimage extraction capability from an Image object?
and also, then buffered image back to Image ?

Cause, then I can use that code, no problem
0
 
for_yanCommented:

I think this converts Image to BufferedImage:
http://www.java2s.com/Code/Java/2D-Graphics-GUI/ConvertjavaawtimageBufferedImagetojavaawtImage.htm

and this is drawing image from BufferedImage from that link above:
   BufferedImage loadImg = ImageUtil.loadImage("C:/Images/duke.gif");   
        frame.setBounds(0, 0, loadImg.getWidth(), loadImg.getHeight());   
        // Set the panel visible and add it to the frame   
        frame.setVisible(true);   
        // Get the surfaces Graphics object   
        Graphics2D g = (Graphics2D)frame.getRootPane().getGraphics();   
        // Now draw the image   
        g.drawImage(loadImg, null, 0, 0);   

Open in new window


So from animated gif you can either write it to a file or
darrw it on java component, doesn't that cover your needs?



0
 
beavoidAuthor Commented:
for the last 3 lines, I can just use the getgraphics from my Image object, I guess.

Game over, no worrying about buffered image conversion or components?
0
 
for_yanCommented:
I think so, irt seems you should be able to do it both ways
0
 
beavoidAuthor Commented:
Could you please give me a bit of text on that page to find the conversion of bufferedimage to image? - section? (ultimate image thing)
0
 
for_yanCommented:

> give me a bit of text on that page

Sorry, don't understand whivh page you mean.
Please, explain.
0
 
beavoidAuthor Commented:
I can't find the exact code lines that do the conversions.

May I have a hint as to its place ( If I can search for text in that area, it would be good)
0
 
for_yanCommented:

the code in
ID:36951481
allows you to show image on your java component.
Try how it works.
I didn't try but hope it does work.
So you can write graphics file form BufferedImage, you can show it ion Java component.
What other needs do you have?
0
 
for_yanCommented:
BufferedImage is actually subclass of Image.
Image is abstract class. So you cannot talke about conversion of BufferedImgae to Image.
BufferedImage IS Image
see http://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html
0
 
beavoidAuthor Commented:
thx
0
 
for_yanCommented:
You are always welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.