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 ?
Write or save an imagewrite(bufferedImage , "jpg", new File("c:\\test\\image. jpg")); ImageIO. write(bufferedImage , "gif", new File("c:\\test\\image. gif")); ImageIO.
Saving a BufferedImage as a JPEG BufferedImage img = ... File f = new File("MyFile. jpg"); ImageIO. write(img, "JPEG", f);
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) { ... }
awt. image. Provides the classes necessary to create an applet and the classes an applet uses to communicate with its applet context.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With