使用 ImageIO.read 时出现问题

发布于 2024-09-29 15:04:47 字数 817 浏览 2 评论 0原文

好的,我有一张正在尝试阅读的图像。问题是 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);

}

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

这是我到目前为止所尝试过的:

  1. 好的,我的环境是 Windows 7。我 使用其中一张图像进行测试 随 Windows 一起提供,并且能够 阅读图像。
  2. image1.jpg 的创建者是 另一个系统。现在确定什么方法了 他们正在使用。
  3. 我尝试将图像转换为 RGB 如此处建议的链接文本 但失败并显示“不是 JPEG 文件:以 0x4d 0x4d 开头”。
  4. 图像扩展名是 .jpg,但是 windows说它是JPEG类型?这 令人困惑。

有人可以帮忙吗?我对此很陌生,不知道如何解决这个问题。

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

Ok I have an Image that I'm trying to read. Problem is that the Image.read(file) returns 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?

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 text
    but 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放飞的风筝 2024-10-06 15:04:47

好吧,自从我更换了笔记本电脑后,我查看了我的旧笔记本电脑,并在 jre/ext/lib 中找到了这个 JAR jai-imageio.jar (我知道这是个坏主意)。我将它移到我的项目/lib 中并且它起作用了!
我猜这个 jai-imageio.jar 包含额外的图像读取器。

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.

司马昭之心 2024-10-06 15:04:47

来自 ImageIO.read()

返回一个 BufferedImage 作为结果
解码提供的文件
自动选择ImageReader
从目前注册的人中。
File 被包装在
图像输入流。如果没有注册
ImageReader 声称能够
读取结果流,null
返回。

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

From the Javadocs for ImageIO.read()

Returns a BufferedImage as the result of
decoding a supplied File with an
ImageReader chosen automatically
from among those currently registered.
The File is wrapped in an
ImageInputStream. If no registered
ImageReader claims to be able to
read the resulting stream, null is
returned.

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

若能看破又如何 2024-10-06 15:04:47

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文