Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 974
Hoa Nguyen Avatar asked Nov 13 '12 14:11

Hoa Nguyen


People also ask

How do I save a Java file as a JPEG?

Write or save an imagewrite(bufferedImage , "jpg", new File("c:\\test\\image. jpg")); ImageIO. write(bufferedImage , "gif", new File("c:\\test\\image. gif")); ImageIO.

How do you save a BufferedImage file?

Saving a BufferedImage as a JPEG BufferedImage img = ... File f = new File("MyFile. jpg"); ImageIO. write(img, "JPEG", f);

How do I save a PNG file in Java?

The formatName parameter selects the image format in which to save the BufferedImage . try { // retrieve image BufferedImage bi = getMyImage(); File outputfile = new File("saved. png"); ImageIO. write(bi, "png", outputfile); } catch (IOException e) { ... }

What does Java AWT image do?

awt. image. Provides the classes necessary to create an applet and the classes an applet uses to communicate with its applet context.


1 Answers

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(); 
    } 
}
like image 186
pkpk1234 Avatar answered Sep 17 '22 15:09

pkpk1234