10

I have a 3rd-party lib that produces java.awt.Image object from a video stream. (In fact, it's originally used to decode .h264 file and then, display images decoded in a JFrame).

Now, I want use that lib to capture several images of the stream and save them to hard disk. So, What must I do to save these java.awt.Image to file ?

Hoa Nguyen
  • 13,452
  • 11
  • 45
  • 44

4 Answers4

7

See ImageIO

The type can be "jpg", "png" (Java <1.6) or "gif" (Java 1.6+).

To save a ToolKitImage you can do the following.

BufferedImage bufferedImage= new BufferedImage(toolkitImage.getWidth(), toolkitImage.getHeight(), BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(toolkitImage, 0, 0, null);
ImageIO.write(bufferedImage, "jpg", new File("C:\\myImage.jpg"));
Felix
  • 533
  • 4
  • 11
  • 1
    It generated ClassCastException at runtime, Downcasting is not allowed here: java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage – Hoa Nguyen Nov 13 '12 at 15:19
  • You said you have a java.awt.image Object. ToolkitImage and BufferedImage both inherit from Image. – Felix Nov 13 '12 at 15:20
  • Yes, they're both inherited from Image. That means we can upcast from ToolkitImage or BufferedImage to Image, the vice-versa is possible but not always true, at least in this situation, where we must upcast from ToolkitImage to Image (accepted), and then dowcast from Image to BufferedImage (not accepted) :) – Hoa Nguyen Nov 13 '12 at 15:26
  • I updated the code to first create a BufferedImage from a ToolKitImage and then save it to a file. Note that I couldn't test this (but I think it should work). – Felix Nov 13 '12 at 15:27
6
public void savePic(Image image, String type, String dst){ 
    int width = image.getWidth(this); 
    int height = image.getHeight(this);
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
    Graphics g = bi.getGraphics(); 
    try { 
        g.drawImage(image, 0, 0, null);
        ImageIO.write(bi, type, new File(dst)); 
    } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
    } 
}
chooco13
  • 53
  • 1
  • 9
pkpk1234
  • 87
  • 1
  • 2
2

Please ckech out this tutorial: Writing/Saving an Image

This is a basic task, the ImageIO lib will help you pretty easy with this.

THarms
  • 311
  • 1
  • 4
-3

Something like;

Image img = /* your image */
BufferedImage bi = (BufferedImage)img;
File f = new File("./output.png");
ImageIO.write(bi, "png", f);
lynks
  • 5,599
  • 6
  • 23
  • 42
  • 3
    It generated ClassCastException at runtime, Downcasting is not allowed here: java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage – Hoa Nguyen Nov 13 '12 at 15:18
  • Yeah I thought that cast might be a bit funny, you will have to convert either via `ToolkitImage` as others have stated, or by drawing the `Image` to a `Graphics` object in order to yield a `BufferedImage` – lynks Nov 13 '12 at 15:30
  • Obviously, That's what I tested thank to Alex B and it worked, thank you too :) – Hoa Nguyen Nov 13 '12 at 15:40