Java ImageIO IIOException:不支持的图像类型? [英] Java ImageIO IIOException: Unsupported image type?
问题描述
BufferedImage imgSelected = ImageIO.read(new File(/ abs / url / to /file/image.jpg));
此行将抛出一个IIOException,其中不支持的图像类型作为异常消息。我检查并重新检查,事实上这行抛出异常,File对象有效,URL是有效的,而 image.jpg
实际上是一个有效的JPG,可以在其他图像浏览器中加载完美。
如何获取有关此异常性质的更多信息?这是Java 7中加载图像的传统方法,还是旧的/不赞成的方法?没有很多关于这些不支持的图像类型异常的信息,当然,ImageIO支持JPG!
感谢任何帮助!
尝试检查JPEG的编码。 ImageIO
无法读取CMYK编码的jpeg图像。
AFAIK,ImageIO多年来一直没有更新,所以你想尝试使用官方的替代/扩展: JAI ImageIO 。
JAI ImageIO需要一些安装到JRE中的本机库,这可能是不需要的。
我们执行以下操作:
- 使用 Apache Sanselan 来检测,无论是JPEG
- ,因为Sanselan无法读取和写入JPEG,使用普通的旧AWT
JPEGCodec
:JPEGCodec.createJPEGDecoder(...) - 将CMYK转换为RGB,我们然后获取读取
BufferedImage
的栅格,并手动转换它(您可以使用ICC配置文件,但手动转换符合我们的需要)
这是我的一个问题,导致 ImageIO
不支持所有类型的JPEG图像,而我我再次发现我为什么会得到这个消息:用于检测CMYK图像的JAI ImageIO的纯Java替代品
Working with images in Java for the first time and am getting some bizarro exceptions that aren't documented very well. Here's the line of code that is failing:
BufferedImage imgSelected = ImageIO.read(new File("/abs/url/to/file/image.jpg"));
This line is throwing an IIOException with Unsupported image type as the exception message. I have checked and re-checked that it is in fact this line throwing the exception, that the File object is valid, that the URL is valid, and that the image.jpg
is in fact a valid JPG that loads perfectly fine in other image viewers.
What could I do to get more information about the nature of this exception? Is this the traditional way for loading images in Java 7, or is this an old/deprecating method? There's just not a lot of info out there about these "Unsupported image type" exceptions, and surely, ImageIO supported JPGs!
Thanks for any help!
Try to check the encoding of the JPEG. ImageIO
can't read CMYK-encoded jpeg images for example.
AFAIK, ImageIO hasn't been updated for years, so you'd like to try and use the official alternative/extension: JAI ImageIO.
Unforutnately, JAI ImageIO needs some native libraries installed into the JRE, which might be unwanted. We do the following:
- use Apache Sanselan to detect, whether it's a JPEG
- since Sanselan can't read and write JPEG, use the plain old AWT
JPEGCodec
:JPEGCodec.createJPEGDecoder(...)
- to convert CMYK to RGB, we then get the raster of the read
BufferedImage
and manually convert it (you could use ICC profiles, but the manual conversion fits our needs)
Here's a question of mine that resulted of the fact that ImageIO
doesn't support all types of JPEG images, and I there stated a little more of my findings of why you get that message: Pure Java alternative to JAI ImageIO for detecting CMYK images
这篇关于Java ImageIO IIOException:不支持的图像类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!