Java ImageIO IIOException:不支持的图像类型? [英] Java ImageIO IIOException: Unsupported image type?

查看:54
本文介绍了Java ImageIO IIOException:不支持的图像类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次在 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"));

这一行抛出了一个 IIOException,其中 Unsupported image type 作为异常消息.我已经检查并重新检查,实际上是这一行引发了异常,File 对象有效,URL 有效,并且 image.jpg 实际上是有效的 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.

我该怎么做才能获得有关此异常性质的更多信息?这是在 Java 7 中加载图像的传统方式,还是旧的/弃用的方法?关于这些不支持的图像类型"异常的信息并不多,当然,ImageIO 支持 JPG!

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!

感谢您的帮助!

推荐答案

尝试检查 JPEG 的编码.例如,ImageIO 无法读取 CMYK 编码的 jpeg 图像.AFAIK,ImageIO 多年未更新,因此您想尝试使用官方替代/扩展:JAI ImageIO.

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.

不幸的是,JAI ImageIO 需要在 JRE 中安装一些本机库,这可能是不需要的.我们执行以下操作:

Unforutnately, JAI ImageIO needs some native libraries installed into the JRE, which might be unwanted. We do the following:

  • 使用 Apache Sanselan 检测是否是 JPEG
  • 由于 Sanselan 无法读写 JPEG,请使用普通的旧 AWT JPEGCodec:JPEGCodec.createJPEGDecoder(...)
  • 要将 CMYK 转换为 RGB,我们然后获取读取的光栅 BufferedImage 并手动转换它(您可以使用 ICC 配置文件,但手动转换符合我们的需要)
  • 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)

这是我的一个问题,这是由于 ImageIO 不支持所有类型的 JPEG 图像而导致的,我在那里陈述了我对您收到该消息的原因的更多发现:用于检测 CMYK 图像的 JAI ImageIO 的纯 Java 替代方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆