如何将BufferedImage保存为文件

问题描述 投票:103回答:6

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

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

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

java file-io bufferedimage
6个回答
201
投票
File outputfile = new File("image.jpg");
ImageIO.write(bufferedImage, "jpg", outputfile);

20
投票

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

public static boolean write(RenderedImage im, String formatName, File output) throws IOException

这里im是要写的RenderedImageformatName是包含格式的非正式名称的字符串(例如png),output是要写入的文件对象。 PNG文件格式的方法示例用法如下所示:

ImageIO.write(image, "png", file);

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

笔记:

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

9
投票

答案在Java Documentation的Tutorial for Writing/Saving an Image中。

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

static boolean ImageIO.write(RenderedImage im, String formatName, File output)  throws IOException

该教程解释了这一点

BufferedImage类实现RenderedImage接口。

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

例如,

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

write包围try block调用是很重要的,因为根据the API,该方法抛出一个IOException“如果在写入过程中发生错误”

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

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

Parameters:

im - 要写入的RenderedImage。

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

output - 要写入的文件。

Returns:

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

Throws:

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


1
投票
  1. 下载并将imgscalr-lib-x.x.jarimgscalr-lib-x.x-javadoc.jar添加到您的项目库。
  2. 在你的代码中: import static org.imgscalr.Scalr.*; public static BufferedImage resizeBufferedImage(BufferedImage image, Scalr.Method scalrMethod, Scalr.Mode scalrMode, int width, int height) { BufferedImage bi = image; bi = resize( image, scalrMethod, scalrMode, width, height); return bi; } // Save image: ImageIO.write(Scalr.resize(etotBImage, 150), "jpg", new File(myDir));

0
投票

作为一个班轮:

ImageIO.write(Scalr.resize(ImageIO.read(...), 150));
© www.soinside.com 2019 - 2024. All rights reserved.