简体   繁体   English

ImageIO.write(img,"jpg",pathtosave) JAVA 不将图像文件保存到选定的文件路径

[英]ImageIO.write(img,"jpg",pathtosave) JAVA not saving Image file to selected filepath

I'm using JVM 14.0.2 in VSCode IDE.我在 VSCode IDE 中使用 JVM 14.0.2。 The purpose of the code is to change the original input image to grayscale image and save the new gray image to the desired location.该代码的目的是将原始输入图像更改为灰度图像并将新的灰度图像保存到所需位置。

The code runs with no exceptions and i tried to print some progress lines(System.out.println("Saving completed...");), those lines printed throughout the program where i plugged in. However, when i go to the selected filepath to search for the saved GrayScale image, i do not see the new image in the directory.代码无一例外地运行,我尝试打印一些进度行(System.out.println(“保存完成...”);),这些行在我插入的整个程序中打印。但是,当我转到选择文件路径来搜索保存的灰度图像,我在目录中没有看到新图像。

I then tried the BlueJ IDE, and the gray image was saved.然后我尝试了 BlueJ IDE,并保存了灰色图像。 Can you check if it's VSCode developing environment issue or my code issue?你能检查一下是VSCode开发环境问题还是我的代码问题? or I need a different class/method to edit images in VSCode?或者我需要一个不同的类/方法来编辑 VSCode 中的图像? Thanks for your help.Let me know if you need more details.感谢您的帮助。如果您需要更多详细信息,请告诉我。

public class GrayImage {
public static void main(String args[]) throws IOException {
    BufferedImage img = null;
    // read image
    try {
        File f = new File("C:\\original.jpg");
        img = ImageIO.read(f);

        // get image width and height
        int width = img.getWidth();
        int height = img.getHeight();
        BufferedImage grayimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        // convert to grayscale
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                Color color = new Color(img.getRGB(x, y));
                int r = (int) color.getRed();
                int g = (int) color.getBlue();
                int b = (int) color.getGreen();
                // calculate average
                int avg = (r + g + b) / 3;
                // replace RGB value with avg
                Color newColor = new Color(avg, avg, avg, color.getAlpha());

                grayimg.setRGB(x, y, newColor.getRGB());
            }
        }
        // write image
        System.out.println("Trying to write the new image...");
        File newf = new File("H:\\gray.jpg");
        ImageIO.write(grayimg, "jpg", newf);
        System.out.println("Finished writing the new image...");
    } catch (IOException e) {
        System.out.println(e);
    }
}// main() ends here

} }

If I understand this problem correctly, the important lesson here is that ImageIO.write(...) returns a boolean , indicating whether it succeeded or not.如果我正确理解这个问题,这里的重要教训是ImageIO.write(...)返回一个boolean ,指示它是否成功。 You should handle situations where the value is false , even if there is no exception.您应该处理值为false ,即使没有例外。 For reference, see the API doc .如需参考,请参阅API 文档

Something like:就像是:

if (!ImageIO.write(grayimg, "JPEG", newf)) {
    System.err.println("Could not store image as JPEG: " + grayimg);
}

Now, for the reason your code does indeed work in one JRE and not in another, is probably related to the image being of type TYPE_INT_ARGB (ie. contains alpha channel).现在,对于原因,你的代码确实是在一个JRE工作,而不是在另一个,可能与类型的图像感TYPE_INT_ARGB (即包含Alpha通道)。 This used to work in Oracle JDK/JREs but support was removed :这曾经在 Oracle JDK/JRE 中工作,但支持已被删除

Previously, the Oracle JDK used proprietary extensions to the widely used IJG JPEG library in providing optional color space support.以前,Oracle JDK 使用广泛使用的 IJG JPEG 库的专有扩展来提供可选的色彩空间支持。 This was used to support PhotoYCC and images with an alpha component on both reading and writing.这用于在读取和写入时支持 PhotoYCC 和带有 alpha 组件的图像。 This optional support has been removed in Oracle JDK 11.此可选支持已在 Oracle JDK 11 中删除。

The fix is easy;修复很容易; as your source is a JPEG file, it probably does not contain an alpha component anyway, so you could change to a different type with no alpha.由于您的源是 JPEG 文件,因此它可能不包含 alpha 组件,因此您可以更改为没有 alpha 的其他类型。 As you want a gray image, I believe the best match would be:由于您想要灰色图像,我相信最佳匹配将是:

BufferedImage grayimg = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

But TYPE_INT_RGB or TYPE_3BYTE_BGR should work too, should you later run into the same problem with color images.但是TYPE_INT_RGBTYPE_3BYTE_BGR应该可以工作,如果您以后遇到与彩色图像相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM