views:

425

answers:

2

I'm using ImageIO.read to process uploaded image files. The code is similar to

BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(image.getContents()));

I've managed to solve most of the issues, but this one has left me clueless. The uploaded image has a JCS_YCCK profile, as defined in com.sun.imageio.plugins.jpeg.JPEG, which is not supported by com.sun.imageio.plugins.jpeg.JPEGImageReader. This leads to a nice stack trace similar to:

Caused by: javax.imageio.IIOException: Unsupported Image Type
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:910)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:885)
    at javax.imageio.ImageIO.read(ImageIO.java:1422)
    at javax.imageio.ImageIO.read(ImageIO.java:1326)
    at com.example.ImageWriter.resizeEmbeddableImageInPlace(ImageWriter.java:231)

How can I process this type of JPEG using Java's ImageIO?


Update: I've tried Commons-Sanselan, indicated by an answer, but unfortunately it does not support JPEG files:

org.apache.sanselan.ImageReadException: Sanselan cannot read or write JPEG images.
    at org.apache.sanselan.formats.jpeg.JpegImageParser.getBufferedImage(JpegImageParser.java:90)
    at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1264)
    at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1231)
+1  A: 

I don't know for ImageIO, but you could use the Commons Sanselan library, which offers easy ways to access all sorts of images.

Valentin Rocher
Thanks! I'll take a look.
Robert Munteanu
Sanselan explicitly states that JPEG images are not supported.
Robert Munteanu
A: 

One possible solution is to use the Java Advanced Imaging Image IO extensions. When properly installed, the conversion works out of the box.

The problem is that it does not play well with Maven, so I've asked Using Java Advanced Imaging with Maven. If that works out, this answer will be accepted.

Robert Munteanu