java 使用 ImageIO.read 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4047667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:32:50  来源:igfitidea点击:

Problem using ImageIO.read

javaimageswingimage-processingapplet

提问by Marquinio

Ok I have an Image that I'm trying to read. Problem is that the Image.read(file) returns NULL.

好的,我有一张我正在尝试阅读的图片。问题是 Image.read(file) 返回 NULL。

File file = new File("C:\images\image1.jpg");
if(file.exists()){
    System.out.println("Image file exists.");
    BufferedImage originalImage = ImageIO.read(file);

}

So image exists but ImageIO.read(file) returns NULL. No thrown errors nothing!!! Whats going on?

所以图像存在但 ImageIO.read(file) 返回 NULL。没有抛出错误什么都没有!!!这是怎么回事?

This is what I have tried so far:

这是我迄今为止尝试过的:

  1. Ok my environment is Windows 7. I tested with one of those images that comes with Windows and its able to read the image.
  2. The image1.jpg was created by another system. Now sure what method they are using.
  3. I tried converting the image into RGB as suggested here link textbut it fails with "Not a JPEG file: starts with 0x4d 0x4d".
  4. The image extension is .jpg, but windows says its a JPEG type? This is confusing.
  1. 好的,我的环境是 Windows 7。我用 Windows 附带的那些图像之一进行了测试,它能够读取图像。
  2. image1.jpg 是由另一个系统创建的。现在确定他们正在使用什么方法。
  3. 我尝试按照此处链接文本的建议将图像转换为 RGB, 但失败并显示“不是 JPEG 文件:以 0x4d 0x4d 开头”。
  4. 图像扩展名为 .jpg,但 windows 说它是 JPEG 类型?这令人困惑。

Can someone help with this? I'm new to this, not sure how to fix this.

有人可以帮忙吗?我是新手,不知道如何解决这个问题。

Ok I just figured out that ImageIO.getImageReaders(stream) returns an empty Iterator. This means that it couldn't find a suitable reader? How am I supposed to read this image?

好的,我刚刚发现 ImageIO.getImageReaders(stream) 返回一个空的迭代器。这意味着它找不到合适的阅读器?我应该如何阅读这张图片?

采纳答案by Marquinio

Ok since I switched laptops, I looked at my old laptop and found this JAR jai-imageio.jar in the jre/ext/lib (I know bad idea). I moved it to my project/lib and it worked! I guess this jai-imageio.jar contains additional image readers.

好的,因为我换了笔记本电脑,我看了看我的旧笔记本电脑,在 jre/ext/lib 中找到了这个 JAR jai-imageio.jar(我知道这个主意不好)。我把它移到了我的项目/库中,它起作用了!我猜这个 jai-imageio.jar 包含额外的图像阅读器。

回答by Jason

From the Javadocs for ImageIO.read()

来自 Javadocs ImageIO.read()

Returns a BufferedImageas the result of decoding a supplied File with an ImageReaderchosen automatically from among those currently registered. The Fileis wrapped in an ImageInputStream. If no registered ImageReaderclaims to be able to read the resulting stream, nullis returned.

返回 aBufferedImage作为解码提供的 File 的结果,并ImageReader从当前注册的文件中自动选择一个 。该File被包裹在一个 ImageInputStream。如果没有注册的 ImageReader声明能够读取结果流,null则返回。

Try creating an ImageInputStream, then pass that onto the ImageIO.read()method, instead of sending the file itself.

尝试创建一个ImageInputStream,然后将其传递给ImageIO.read()方法,而不是发送文件本身。

回答by Sanjeev

Awesome, I had the same problem, wherein it was supporting 24 image formats in eclipse but was supporting only 12 image formats in command prompt with maven. Once I placed jai_imageio.jar in maven's test path, maven has begun to support 24 image formats as well.

太棒了,我遇到了同样的问题,它在 eclipse 中支持 24 种图像格式,但在 maven 的命令提示符中仅支持 12 种图像格式。一旦我将 jai_imageio.jar 放在 maven 的测试路径中,maven 也开始支持 24 种图像格式。