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)
- 3 Reasons why I am returning to Business of Software Conference in 2016 - February 9, 2016
- PDF and Acrobat Forms Converters from IDRsolutions - February 2, 2016
- Updates on Java PDF Viewer roadmap from IDRsolutions - January 26, 2016
- 5 changes we are considering in 2016 for IDRsolutions - January 7, 2016
- Understanding the PDF File Format: Document and Page level - December 29, 2015