将 java.awt.Image 保存到磁盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13363021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:26:36  来源:igfitidea点击:

Save java.awt.Image to disk

javah.264decoder

提问by nxhoaf

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).

我有一个从视频流生成 java.awt.Image 对象的第 3 方库。(实际上,它最初用于解码 .h264 文件,然后显示在 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 ?

现在,我想使用该库来捕获流的多个图像并将它们保存到硬盘。那么,我必须怎么做才能将这些 java.awt.Image 保存到文件中?

回答by Felix

See ImageIO

ImageIO

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

类型可以是“jpg”、“png”(Java <1.6)或“gif”(Java 1.6+)。

To save a ToolKitImage you can do the following.

要保存 ToolKitImage,您可以执行以下操作。

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

回答by THarms

Please ckech out this tutorial: Writing/Saving an Image

请查看本教程:编写/保存图像

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

这是一项基本任务,ImageIO 库将帮助您轻松完成此任务。

回答by pkpk1234

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

回答by lynks

Something like;

就像是;

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