BufferedImage is one of the most useful Java abstractions. It hides all the complexity of different types of images whilst allowing access to the underlying data. Under the hood, a BufferedImage can be many types of image. This is the list of types visible in an IDE.
The differences between the types of BufferedImage are:-
- Range of Colours (Binary will give back and white only, RGB will provide 24-bit color)
- Opacity (only available in ARGB – the A is an 8 bit Alpha channel).
- Amount of memory used (ARGB uses the most as 4 bytes per pixel).
- Size of the saved file.
A BufferedImage generally uses the RGB, Gray or Binary Color Spaces. Data can be in other formats, and the raw raster data can be loaded and accessed for CMYK and YCCK, but the Colors will not display correctly. BufferedImage provides lots of methods to access the raw image data, including the Raster, pixel and ColorModel used.
All BufferedImages work the same, so the same code can be used to manipulate a BufferedImage. A Graphics2D drawing surface can be obtained directly from an instance of BufferedImage with getGraphics() and used to draw shapes, text and other images over the original image. All are converted to the Image type used. So a Binary Image will only show black and white and any colors used will be converted.
BufferedImages can be converted into other supported ColorSpaces types by using a ColorConvertOp. Java can be used for Image processing either via manipulating the BufferedImage directly or the Graphics2D object.
So BufferedImage offers a really flexible abstraction which makes it very easy to make use of images in Java. Java developers no longer need to worry about image types and formats.
Java includes support to load and save images in various formats using ImageIO, and other libraries such as Apache Imaging and our JDeli library also offer this feature.
Next time we will talk more about ImageIO and other Image libraries.
Are you a Java Developer working with Image files?
// Read an image
BufferedImage bufferedImage = JDeli.read(dicomImageFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(heicImageFile);
// Write an image
JDeli.write(bufferedImage, "heic", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpegImageFile);
// Write an image
JDeli.write(bufferedImage, "jpeg", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(jpeg2000ImageFile);
// Write an image
JDeli.write(bufferedImage, "jpx", outputStreamOrFile);
// Write an image
JDeli.write(bufferedImage, "pdf", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(pngImageFile);
// Write an image
JDeli.write(bufferedImage, "png", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(tiffImageFile);
// Write an image
JDeli.write(bufferedImage, "tiff", outputStreamOrFile);
// Read an image
BufferedImage bufferedImage = JDeli.read(webpImageFile);
// Write an image
JDeli.write(bufferedImage, "webp", outputStreamOrFile);
Why do developers choose JDeli over free alternatives?
- Works with newer image formats such as AVIF, HEIC, JPEG XL, WEBP
- Better support than alternatives for JPEG, PNG, TIFF.
- Prevent JVM crashes caused by native code in other image libraries
- Better performance than other popular Java image libraries