使用 ImageIO.read 时出现问题
好的,我有一张正在尝试阅读的图像。问题是 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。没有抛出任何错误!这是怎么回事?
这是我到目前为止所尝试过的:
- 好的,我的环境是 Windows 7。我 使用其中一张图像进行测试 随 Windows 一起提供,并且能够 阅读图像。
- image1.jpg 的创建者是 另一个系统。现在确定什么方法了 他们正在使用。
- 我尝试将图像转换为 RGB 如此处建议的链接文本 但失败并显示“不是 JPEG 文件:以 0x4d 0x4d 开头”。
- 图像扩展名是 .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:
- Ok my environment is Windows 7. I
tested with one of those images that
comes with Windows and its able to
read the image. - The image1.jpg was created by
another system. Now sure what method
they are using. - I tried converting the image into
RGB as suggested here link text
but it fails with "Not a JPEG file: starts with 0x4d 0x4d". - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,自从我更换了笔记本电脑后,我查看了我的旧笔记本电脑,并在 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.
来自
ImageIO.read()
尝试创建一个 ImageInputStream,然后将其传递到
ImageIO.read()
方法,而不是发送文件本身。From the Javadocs for
ImageIO.read()
Try creating an
ImageInputStream
, then pass that onto theImageIO.read()
method, instead of sending the file itself.太棒了,我遇到了同样的问题,其中它在 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.