问题
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
?
回答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.
来源:https://stackoverflow.com/questions/11552838/reading-jpegs-in-java