【问题标题】:How to test if BufferedImage using ImageIO picks up a 'null'如何测试使用 ImageIO 的 BufferedImage 是否拾取“null”
【发布时间】:2017-11-14 03:53:27
【问题描述】:

我正在尝试设置我的程序(使用 netbeans 的 java)如果它找到它正在寻找的图像做一件事,如果它没有找到另一件事......

这是我到目前为止所得到的,但它从未完成 else 语句。我相信这是因为“图像”在技术上不是空的,因为它仍然对应于输入的文件名,但是我不确定如何设置 java 以根据目录中是否找不到文件名来执行某些操作。

public void displayImage(String strfilename, JLabel JLlabel) {

    try {
        JLabel label = JLlabel;

        String FileName = strfilename;
        BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
        if(image!=null){
        ImageIcon icon = new ImageIcon(image);

        label.setIcon(icon);}
        else{
        BufferedImage image2 = ImageIO.read(new File("NOIMAGE.jpg"));
        ImageIcon icon2 = new ImageIcon(image2);
        label.setIcon(icon2);
        }
    } catch (IOException ioe) {
    }
}

如果有人可以帮助我,我将非常感激

【问题讨论】:

  • ImageIO.read 如果文件无法读取/不存在,则抛出 IOException。就个人而言,根据您的示例,我会使用 File#exists 而不是依赖 ImageIO.read

标签: java file bufferedimage javax.imageio


【解决方案1】:

您正在接受异常。您不需要 if{}else{} 循环,因为您已经有一个 try{}catch{}。

String FileName = file;
try {
    BufferedImage image = ImageIO.read(new File(FileName + ".jpg"));
    // Code for when the image is found
} catch (IOException ex) {
    // Code for when the image is not found
}

编辑: 正如@haraldK 指出的那样,您可以拥有一个存在但不可读的文件,在这种情况下将抛出 NullPointerException。

你可以在 catch 子句中处理它们。

public void displayImage(String strfilename, JLabel label) {
    try {
        BufferedImage image = ImageIO.read(new File(strfilename + ".jpg"));
        ImageIcon icon = new ImageIcon(image); // Can throw NullPointerException if the file is found but is unreadable
        label.setIcon(icon);

    } catch (IOException | NullPointerException ex) {
        ImageIcon icon = new ImageIcon("NOIMAGE.jpg");
        label.setIcon(icon);
    }
    // You're probably going to have to pack or validate your container here
}

值得注意的是,这不会检查 NOIMAGE 的异常,您可能需要添加它。

这比仅仅调用 File.exists() 更好,因为它还处理存在但不可读的文件(文本文件等)。

【讨论】:

  • 你可能仍然需要if/else,因为如果ImageIO.read() 无法读取图像(未知格式),它会返回null
  • 我明白你在说什么,但在这种情况下,文件扩展名被硬编码为 .jpg。
  • 如果假设文件扩展名 == 文件格式,那就没问题了。但是如果我将文本文件命名为 .jpg 会怎样? ImageIO.read() 不会抛出异常,而是返回 null。永远不要相信用户输入。 ;-)
  • 啊,这是一个公平的观点。但是我刚刚测试了一下,如果我用现有的文本文件调用 ImageIO.read(),它不会继续使用空图像,而是会抛出与没有文件相同的异常(不能读取输入文件!)。因此,似乎任何无法读取的输入都会被捕获。
  • 是的,我的错。我不知道为什么我认为在文件名末尾添加 .jpg 可以解决任何问题。更新了答案。
【解决方案2】:

您可以在处理此方法之前检查文件是否存在,我也删除了一些不必要的代码。

public void displayImage(String strfilename, JLabel JLlabel)  {
// declare only one reference you don't need two references 

BufferedImage image=null;

if(!isImageExist(strfilename)){
// assign the NOIMAGE if image not found
 image = ImageIO.read(new File("NOIMAGE.jpg"));    
} else {

    try {
        // assign the image if found
        image = ImageIO.read(new File(strfilename + ".jpg"));

    } catch (IOException ioe) {
      ioe.printStackTrace();
   }
}
     ImageIcon icon = new ImageIcon(image);
    //setting the image once instead of repeating the code in if and else blocks also you don't need to add another reference to JLlabel because you are already got it from a parameter 
     JLlabel.setIcon(icon);
}
private boolean isImageExist(String imageName) {
return new File(imageName.jpg).exist();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多