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)
- Pre-JavaOne sales and Marketing trip to San Francisco in October - September 3, 2015
- 3 Shows you should attend in 2015 – 3. NetBeans Days - September 1, 2015
- 3 Shows you should attend in 2015 – 2. DevFest (Istanbul) - August 25, 2015
- 3 Shows you should attend in 2015 – 1. JavaOne (San Francisco) - August 18, 2015
- NetCat for NetBeans IDE explained in less than 5 minutes - July 16, 2015