5

I'm attempting to get a JPEG into a BufferedImage to display on a JPanel . However, javax.imageio.ImageIO.read() seems to be very fussy about the JPEGs it processes, often throwing an IIOException. The same JPEGs open fine in pretty much any image reader you'd care to name.

I've looked at Apache's Sanselan and JAI. But Sanselan can't process JPEGs and JAI isn't available for 64-bit Windows platforms and doesn't seem to be maintained (the last update was in 2006). A previous answer on StackOverflow suggested com.sun.image.codec.jpeg.JPEGCodec, but this was deprecated in Java 5 and has disappeared in 7.

Are these my only options? Are there really no Java libraries capable of robustly reading JPEGs into a BufferedImage?

Community
  • 1
  • 1
Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
  • Try using another picture to test it. – Jason Jul 19 '12 at 01:54
  • 1
    @Jason: Another picture is not the point. Some pictures do indeed load fine. The problem is that several don't. When using pictures from diverse sources, not every picture is going to be whatever `ImageIO.read()`'s definition of a "perfect" JPEG is. – Chinmay Kanchi Jul 19 '12 at 02:08
  • 1
    Can you link to an example image? Have you identified any variation from the [nominal standard](http://en.wikipedia.org/wiki/JPEG)? – trashgod Jul 19 '12 at 02:17
  • May be you can get answer from this post:http://stackoverflow.com/questions/2999528/is-there-a-100-java-alternative-to-imageio-for-reading-jpeg-files – Jason Jul 19 '12 at 02:20
  • The one linked to in http://stackoverflow.com/questions/2999528/is-there-a-100-java-alternative-to-imageio-for-reading-jpeg-files is an example. This is a known issue with ImageIO (see link). I'm looking specifically for an alternative library. – Chinmay Kanchi Jul 19 '12 at 02:33

1 Answers1

1

Legacy Toolkit methods such as createImage and getImage are known to be more lenient than ImageIO.

Werner Randelshofer also wrote a Service Provider to read CMYK JPEGs with ImageIO.

By combining both approaches (try every possible ImageReader and then fallback to Toolkit) you will be able to handle a reasonable number of JPEG Images.

Reading JPEGs with CMYK profile may be a interesting read.

Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118
  • This seems like it could work. I won't have time to work on this project for a while, so I'm going to accept this answer. It really does suck that there isn't a good library for hassle-free image processing in Java, not even a third-party one. Surely people have needed something like this before... – Chinmay Kanchi Jul 20 '12 at 18:03