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)
- Major JPedal Java PDF Library SDK update in April - April 19, 2016
- Announcing the schedule for the second free NetBeans UK Day (at Greenwich University) on 31st May - April 11, 2016
- A timely lesson on why it is a bad idea to edit a PDF file directly - March 22, 2016
- Tickets now available for free UK NetBeans Day - March 17, 2016
- How does your team celebrate a successful win? - March 17, 2016