Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将BuffereImage保存为文件_Java_File Io_Bufferedimage - Fatal编程技术网

Java 如何将BuffereImage保存为文件

Java 如何将BuffereImage保存为文件,java,file-io,bufferedimage,Java,File Io,Bufferedimage,我正在使用Java库调整图像的大小 resize()方法调用的结果是BuffereImage对象。现在我想将其保存为一个文件(通常是.jpg) 我该怎么做?我想从BufferedImage->文件,但这可能不是正确的方法?您可以使用类的write方法保存BufferedImage对象。该方法的签名如下所示: File outputfile = new File("image.jpg"); ImageIO.write(bufferedImage, "jpg", outputfile); publ

我正在使用Java库调整图像的大小

resize()方法调用的结果是BuffereImage对象。现在我想将其保存为一个文件(通常是.jpg)


我该怎么做?我想从
BufferedImage
->
文件
,但这可能不是正确的方法?

您可以使用类的write方法保存
BufferedImage
对象。该方法的签名如下所示:

File outputfile = new File("image.jpg");
ImageIO.write(bufferedImage, "jpg", outputfile);
public static boolean write(RenderedImage im, String formatName, File output) throws IOException
此处
im
是要写入的
renderimage
formatName
是包含格式非正式名称的字符串(例如png),而
output
是要写入的文件对象。PNG文件格式方法的示例用法如下所示:

ImageIO.write(image, "png", file);
作为一个班轮:

ImageIO.write(Scalr.resize(ImageIO.read(...), 150));
  • 下载并将和添加到项目库中
  • 在代码中:

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

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

    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();
            }
        }
    }
    
    import java.io.*;
    导入java.awt.image.*;
    导入javax.imageio.*;
    公共班机{
    公共静态void main(字符串参数[]){
    试一试{
    BuffereImage img=新的BuffereImage(
    500,500,BufferedImage.TYPE_INT_RGB);
    文件f=新文件(“MyFile.png”);
    int r=5;
    int g=25;
    int b=255;
    
    int col=(r答案在Java文档的

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

    static boolean ImageIO.write(RenderedImage im, String formatName, File output)  throws IOException
    
    本教程解释了这一点

    BuffereImage类实现RenderImage接口

    因此,它可以用于该方法中

    比如说,

    try {
        BufferedImage bi = getMyImage();  // retrieve image
        File outputfile = new File("saved.png");
        ImageIO.write(bi, "png", outputfile);
    } catch (IOException e) {
        // handle exception
    }
    
    write
    调用周围加上一个字符是很重要的,因为根据,如果在写入过程中发生错误,该方法将抛出一个
    IOException

    还详细说明了方法的目标、参数、返回和抛出:

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

    参数: im-要写入的渲染图像

    formatName—包含格式非正式名称的字符串

    输出-要写入的文件

    返回: 如果未找到合适的写入程序,则为false

    抛出: IllegalArgumentException-如果任何参数为null

    IOException-如果在写入过程中发生错误

    但是,
    formatName
    可能仍然显得相当模糊和模棱两可;本教程稍微澄清了一点:

    ImageIO.write方法调用实现PNG的代码编写“PNG编写器插件”。之所以使用术语插件,是因为图像I/O是可扩展的,可以支持多种格式

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

    对于大多数应用程序来说,使用这些标准插件中的一个就足够了。它们的优点是容易获得

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

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

    String writerNames[]=ImageIO.getWritePerformatNames();

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


    对于完整而实用的示例,可以参考Oracle的

    Scalr是无法解释的。此外,请确保outputfile存在。如果它不存在,write()将(错误地)抛出一个带有try/catch的NullPointerException。不要捕获
    NullPointerException
    ,请使用
    If(outputfile.exists())