Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading JPEGs in Java

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?

like image 244
Chinmay Kanchi Avatar asked Nov 14 '22 01:11

Chinmay Kanchi


1 Answers

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.

like image 137
Anthony Accioly Avatar answered Dec 09 '22 19:12

Anthony Accioly