简体   繁体   中英

Save java.awt.Image to disk

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 ?

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"));
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(); 
    } 
}

Please ckech out this tutorial: Writing/Saving an Image

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

Something like;

Image img = /* your image */
BufferedImage bi = (BufferedImage)img;
File f = new File("./output.png");
ImageIO.write(bi, "png", f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM