gpt4 book ai didi

javax.imageio.IIOException :Can't create an ImageInputStream

转载 作者:行者123 更新时间:2023-11-30 02:56:17 28 4
gpt4 key购买 nike

我正在尝试获取“图像”文件夹中所有图像的高度和宽度。不幸的是,我不断收到“无法创建 ImageInputStream!”错误。我该如何解决?

javax.imageio.IIOException: Can't create an ImageInputStream!
at javax.imageio.ImageIO.read(ImageIO.java:1280)
at test.Test1.main(Test1.java:34)

这是我的完整代码

public class Test1 {
public static void main(String args[]) throws IOException{

try {
File f = new File(System.getProperty("user.dir") + System.getProperty("file.separator")+ "Images");
BufferedImage image = ImageIO.read(f);
int height = image.getHeight();
int width = image.getWidth();
System.out.println("Width : "+ width);
System.out.println("Height : "+ height);

} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}

最佳答案

那是因为您不是在打开文件,而是在打开目录。

File directory = new File(System.getProperty("user.dir") 
+ System.getProperty("file.separator")+ "Images";

for(File file: directory.listFiles()) {
BufferedImage image = ImageIO.read(file);
}

关于javax.imageio.IIOException :Can't create an ImageInputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37151907/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com