One of the best features about Java is the amount of low-level complexity it removes, allowing you to focus on developing the application. However, this does sometimes hide some important issues.
We found one of these with the ImageIO class which offers a whole series of methods to save an image as a Tiff, a PNG or a JPEG. All the complexity is hidden and you can save out the image in one line of code (magic). Unfortunately, it also hides an interesting feature…
ImageIO will happily save all kinds of images, including images with a transparency (ARGB). And it will save it as a JPEG, even though JPEGs do not really support transparency. The problem comes when you try to view the JPEG. Java understands this hybrid image, so it will load back correctly, but very few other tools do.
Because it has 4 bands (RGB are three and then transparency makes the fourth), most JPEG tools assume it must be in the CMYK colorspace and interpret the colors as CMYK. CMYK works completely differently to RGB so the image comes out looking wrong (often with a nasty red tint). The solution is to turn the image back to an RGB image (which is a ‘proper’ JPEG) and save this – if you need to do this, here is some example code.
So while Java makes life much easier most of the time, there are some interesting gotchas out there…
Latest posts by Mark Stephens (see all)
- IDRsolutions releases JPedal 6 - April 27, 2015
- Announcing JDeli – a new Image library for Java - April 21, 2015
- Report on NetBeans Day Greece 2015 - April 18, 2015
- My First Visit to London Book Fair - April 14, 2015
- Sneak preview of upcoming enhancements to our PDF to HTML5/SVG converter - April 13, 2015