Java ImageIO IIOException: Unsupported image type?
I've unfortunately come across a lot of standard violating JPEG files. 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.
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:
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.
To work with images in a specific format,you need to add the corresponding dependency, such as imageio-jpeg or 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.
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
Another option is to use .jar prepared by Werner Randelshofer:
http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/ or Monte Media Library: http://www.randelshofer.ch/monte/
It looks quite easy and similar to ImageIO usage and available under CC license.