简体   繁体   English

Java ImageIO IIOException:不支持的图像类型?

[英]Java ImageIO IIOException: Unsupported image type?

Working with images in Java for the first time and am getting some bizarro exceptions that aren't documented very well.第一次使用 Java 中的图像,我得到了一些没有很好记录的奇怪异常。 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.此行抛出一个带有Unsupported image type作为异常消息的 IIOException。 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.我检查并重新检查实际上是这一行抛出异常,文件 object 有效,URL 有效,并且image.jpg实际上是有效的 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?这是 Java 7 中加载图像的传统方式,还是一种旧的/弃用的方法? There's just not a lot of info out there about these "Unsupported image type" exceptions, and surely, ImageIO supported JPGs!关于这些“不支持的图像类型”异常的信息并不多,当然,ImageIO 支持 JPG!

Thanks for any help!谢谢你的帮助!

Try to check the encoding of the JPEG.尝试检查 JPEG 的编码。 ImageIO can't read CMYK-encoded jpeg images for example. ImageIO无法读取 CMYK 编码的 jpeg 图像。 AFAIK, ImageIO hasn't been updated for years, so you'd like to try and use the official alternative/extension: JAI ImageIO . AFAIK,ImageIO 多年未更新,因此您想尝试使用官方替代/扩展: JAI ImageIO

Unforutnately, JAI ImageIO needs some native libraries installed into the JRE, which might be unwanted.不幸的是,JAI ImageIO 需要在 JRE 中安装一些本机库,这可能是不需要的。 We do the following:我们执行以下操作:

  • use Apache Sanselan to detect, whether it's a JPEG使用Apache Sanselan检测,是否是 JPEG
  • since Sanselan can't read and write JPEG, use the plain old AWT JPEGCodec : JPEGCodec.createJPEGDecoder(...)由于 Sanselan 无法读写 JPEG,请使用普通的旧 AWT JPEGCodecJPEGCodec.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)将 CMYK 转换为 RGB,然后我们获取读取的BufferedImage的光栅并手动转换它(您可以使用 ICC 配置文件,但手动转换符合我们的需要)

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这是我的一个问题,因为ImageIO不支持所有类型的 JPEG 图像,我在那里陈述了更多关于您收到该消息的原因的发现: Pure Java 替代 JAI ImageIO 用于检测 CMYK 图像

I've unfortunately come across a lot of standard violating JPEG files.不幸的是,我遇到了很多违反标准的 JPEG 文件。 ImageIO is particularly picky and often refuse to load images, which are often loaded and apparently displayed correctly by other software with less strict checks on the file format. ImageIO 特别挑剔,经常拒绝加载图像,这些图像经常被其他软件加载并显示正确,对文件格式的检查不太严格。

It's not very pretty, but one workaround is to use the Oracle VM internal JPEG decoder directly (com.sun.image.codec.jpeg.JPEGCodec), as it seems to tolerate more spec deviations as the ImageIO wrapper:它不是很漂亮,但一种解决方法是直接使用 Oracle VM 内部 JPEG 解码器 (com.sun.image.codec.jpeg.JPEGCodec),因为它似乎可以容忍更多规范偏差作为 ImageIO 包装器:

BufferedImage img = 
    JPEGCodec.createJPEGDecoder(inputStream).decodeAsBufferedImage();

This is of course not an ideal solution, since using implementation specific classes will lock you to a specific VM vendor and may break with newer VM versions, but if you'll only used the software in a controlled environment, it may be better than no solution at all.这当然不是一个理想的解决方案,因为使用特定于实现的类会将您锁定到特定的 VM 供应商,并且可能会因更新的 VM 版本而中断,但是如果您只在受控环境中使用该软件,那可能总比没有好解决方案。

To work with images in a specific format,you need to add the corresponding dependency, such as imageio-jpeg or imageio-tiff:要处理特定格式的图像,您需要添加相应的依赖项,例如 imageio-jpeg 或 imageio-tiff:

<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.3.2</version>
</dependency>

<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-bmp</artifactId>
<version>3.3.2</version>
</dependency>

<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-core</artifactId>
<version>3.3.2</version>
</dependency>

the built-in ImageIO Java API loads the plugins automatically at runtime.内置的 ImageIO Java API 在运行时自动加载插件。

Another option is to use .jar prepared by Werner Randelshofer:另一种选择是使用 Werner Randelshofer 准备的 .jar:

http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/ or Monte Media Library: http://www.randelshofer.ch/monte/ http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/或 Monte 媒体库: http : //www.randelshofer.ch/monte/

It looks quite easy and similar to ImageIO usage and available under CC license.它看起来很简单,类似于 ImageIO 的用法,并且在 CC 许可下可用。

This tutorial provided an answer using the apache commons IO library.教程使用 apache commons IO 库提供了答案。 I found it to be a cleaner implementation.我发现它是一个更简洁的实现。 I included the dependency below我在下面包含了依赖项

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.11.0</version>
</dependency>

Here is the code that worked after adding the dependency这是添加依赖项后有效的代码

    public ResponseEntity<byte[]> getImage(@PathVariable("filename") String filename) {
     byte[] image = new byte[0];
      try {
          image = FileUtils.readFileToByteArray(new File(FILE_PATH_ROOT+filename));
      } catch (IOException e) {
          e.printStackTrace();
      }
      return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(image);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java ImageIO IIOException:不支持的图像类型?ImageIO无法读取CMYK编码的图像 - Java ImageIO IIOException: Unsupported image type?ImageIO can't read CMYK-encoded Images PNG图像数据的Javax.ImageIO.IIOException - Javax.ImageIO.IIOException for PNG image Data 将图像从android连续发送到Java应用程序时出错-javax.imageio.IIOException:假霍夫曼表定义 - error in sending image from android to java app serially -javax.imageio.IIOException: Bogus Huffman table definition javax.imageio.IIOException:无法创建输出流! (在Java中) - javax.imageio.IIOException: Can't create output stream! (in java) ImageIO 不支持的图像类型 - 修复了 TwelveMonkeys 插件不起作用? - ImageIO Unsupported Image Type - TwelveMonkeys Plugin with fix not working? 将文件分配给图像编译错误:javax.imageio.IIOException - Assign file to image compile error: javax.imageio.IIOException Javax ImageIO IIOException显然没有原因 - Javax ImageIO IIOException for apparently no reason ImageIO在读取期间抛出60x45,0.5MB的JPEG图像文件的IIOException - ImageIO throws IIOException for 60x45, 0.5MB - JPEG Image File during Reading Java ImageIO声明图像 - Java ImageIO claims image javax.imageio.IIOException:向 jpg 图像添加文本时缺少霍夫曼代码表条目 - javax.imageio.IIOException: Missing Huffman code table entry while Adding text to an jpg image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM