[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Can Java Image objects be paused or frames extracted?

Posted on 2011-10-10
16
Medium Priority
?
406 Views
Last Modified: 2012-05-12
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()  ?
0
Comment
Question by:beavoid
  • 10
  • 6
16 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36946595
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
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36946648

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
 
LVL 47

Expert Comment

by:for_yan
ID: 36946659
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:beavoid
ID: 36949449
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36949805
This gives example from buffered image to image drawing
http://www.javalobby.org/articles/ultimate-image/
0
 

Author Comment

by:beavoid
ID: 36951049
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36951481

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
 

Author Comment

by:beavoid
ID: 36951825
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36951843
I think so, irt seems you should be able to do it both ways
0
 

Author Comment

by:beavoid
ID: 36974509
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36974522

> give me a bit of text on that page

Sorry, don't understand whivh page you mean.
Please, explain.
0
 

Author Comment

by:beavoid
ID: 36974853
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36974865

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
 
LVL 47

Expert Comment

by:for_yan
ID: 36974874
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
 

Author Closing Comment

by:beavoid
ID: 37088968
thx
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37088975
You are always welcome.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question