'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. 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!



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

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.

Solution 4:[4]

This tutorial provided an answer using the apache commons IO library. 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);
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jarnbjo
Solution 2 Mayen
Solution 3 Ravbaker
Solution 4 Ignatius Ojiambo