After extensive testing of various solutions, I came up with an error for the com.sun.imageio.plugins.jpeg.JPEGImageReader
class (the class used to decode JPEG files with the ImageIO
class).
If I do this (Oracle recommended, as this should work with all JVMs and many other threads in SO):
BufferedImage bi = ImageIO.read(new FileInputStream("t.jpg"));
then I get a red image.
On the other hand, if I do:
JPEGImageDecoder jpegDec = JPEGCodec.createJPEGDecoder(new FileInputStream("t.jpg")); BufferedImage bi = jpegDec.decodeAsBufferedImage();
then I get the image correctly decoded. (note that com.sun.image.codec.jpeg.JPEGCodec
is a special Sun / Oracle JVM class and therefore this solution is not portable).
Finally, another approach that I was trying to use uses Toolkit.getDefaultToolkit().createImage()
. The image is loaded asynchronously (a small flaw, in my opinion), but at least it can load the image correctly. I am not 100% sure that this latest solution is portable across all platforms / JVMs.
source share