mynethome.de

25. Oktober 2018 um 13:23 Uhr

JPG with OpenJDK

Some articles in the web (e.g. on stack overflow) claim that OpenJDK does not have support to write JPG images.

Looking into it, I foudn that there is a JPEGImageWriter and a com.sun.imageio.plugins.jpeg.JPEG included with OpenJDK 11 (looking at version 11.0.1).

But this code does not produce an JPG as expeced:

BufferedImage bufferedImage = generateImage(new Date());

String fileName = "SampleFile";

if(!ImageIO.write(bufferedImage, "jpeg", new File(fileName + ".jpg"))){
    System.out.println("No writer for JPG found!");
}

It rather claims „No writer for JPG found!“ :(

Debugging a bit shows, that the JPEG ImageWriter ist available but not loaded due to the test image data I create in the generateImage() method above.

There, BufferedImage is created using

BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)

But,  the type TYPE_INT_ARGB is not supported with JEPG and thus, the JPEGWriter not selected as a valid writer.

Changing the type to

BufferedImage.TYPE_3BYTE_BGR

fixes this and I’m able to write JPG with OpenJDK – hooray! ;-)

Thus, the claim that OpenJDK is not able to write JPG is not true (anymore), one just need to be aware to the implications the various ImageTypes have.

That also mean that I’ve been wrong last night when I repeated what I read about OpenJDK being incapable of writing JPG in Binärgewitter Podcast, Episode 214 (yet to be released, will update the link as soon as possible). Sorry for that!

Have fun and keep coding! ;-)

Einen Kommentar schreiben


Gravatar unterstützt

mynethome.de wird erstellt mit WordPress
Beiträge (RSS) und Kommentare (RSS)

(c) 2005 - 2015 Markus Schlichting