如何将BufferedImage保存为文件

Ank*_*kur 111 java file-io bufferedimage

我正在使用imgscalr Java库来调整图像大小.

resize()方法调用的结果是BufferedImage对象.我现在想把它保存为文件(通常是.jpg).

我怎样才能做到这一点?我想离开BufferedImage- > File但也许这不是正确的做法?

Wer*_*rås 218

File outputfile = new File("image.jpg");
ImageIO.write(bufferedImage, "jpg", outputfile);
Run Code Online (Sandbox Code Playgroud)

  • 代码将覆盖原始图像. (9认同)
  • 用try/catch环绕. (9认同)
  • 另外,请确保outputfile存在.如果没有,write()将(错误地)抛出NullPointerException (7认同)
  • 不要捕获`NullPointerException`,使用`if (outputfile.exists())` (3认同)

Raj*_*oit 22

您可以BufferedImage使用javax.imageio.ImageIO类的write方法保存对象.方法的签名是这样的:

public static boolean write(RenderedImage im, String formatName, File output) throws IOException
Run Code Online (Sandbox Code Playgroud)

imRenderedImage要写的,formatName是包含格式的非正式名称的String(例如png),output是要写入的文件对象.PNG文件格式的方法示例用法如下所示:

ImageIO.write(image, "png", file);
Run Code Online (Sandbox Code Playgroud)


K_7*_*K_7 12

答案在于Java Documentation的编写/保存图像教程.

Image I/O类提供了保存图像以下的方法:

static boolean ImageIO.write(RenderedImage im, String formatName, File output)  throws IOException
Run Code Online (Sandbox Code Playgroud)

该教程解释了这一点

BufferedImage类实现RenderedImage接口.

所以它可以在方法中使用.

例如,

try {
    BufferedImage bi = getMyImage();  // retrieve image
    File outputfile = new File("saved.png");
    ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
    // handle exception
}
Run Code Online (Sandbox Code Playgroud)

write使用try块包围调用很重要,因为根据API,该方法会抛出IOException"如果在写入期间发生错误"

还解释了方法的目标,参数,返回和抛出,更详细:

使用支持给定格式的任意ImageWriter将图像写入文件.如果已存在文件,则其内容将被丢弃.

参数:

im - 要写入的RenderedImage.

formatName - 包含格式的非正式名称的String.

output - 要写入的文件.

返回:

如果找不到合适的作者,则为false.

抛出:

IllegalArgumentException - 如果任何参数为null.

IOException - 如果在写入期间发生错误.

但是,formatName看起来可能仍然模糊不清; 教程清理了一下:

ImageIO.write方法调用实现PNG编写"PNG编写器插件"的代码.由于Image I/O是可扩展的并且可以支持多种格式,因此使用术语插件.

但是以下标准图像格式插件:JPEG,PNG,GIF,BMP和WBMP始终存在.

对于大多数应用程序,使用这些标准插件之一就足够了.它们具有易于获得的优点.

但是,您可以使用其他格式:

Image I/O类提供了一种插入支持可以使用的其他格式的方法,并且存在许多这样的插件.如果您对可以在系统中加载或保存的文件格式感兴趣,可以使用ImageIO类的getReaderFormatNames和getWriterFormatNames方法.这些方法返回一个字符串数组,列出了此JRE支持的所有格式.

String writerNames[] = ImageIO.getWriterFormatNames();

返回的名称数组将包括已安装的任何其他插件,这些名称中的任何一个都可以用作格式名称来选择图像编写器.

有关完整实用的示例,可以参考Oracle的SaveImage.java示例.


Eri*_*ski 9

创建java.awt.image.bufferedImage并将其保存到文件:

import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
public class Main{
    public static void main(String args[]){
        try{
            BufferedImage img = new BufferedImage( 
                500, 500, BufferedImage.TYPE_INT_RGB );

            File f = new File("MyFile.png");
            int r = 5;
            int g = 25;
            int b = 255;
            int col = (r << 16) | (g << 8) | b;
            for(int x = 0; x < 500; x++){
                for(int y = 20; y < 300; y++){
                    img.setRGB(x, y, col);
                }
            }
            ImageIO.write(img, "PNG", f);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

笔记:

  1. 创建一个名为MyFile.png的文件.
  2. 图像是500 x 500像素.
  3. 覆盖现有文件.
  4. 图像的颜色为黑色,顶部有蓝色条纹.