In this article I will show you how to read BMP images in Java using ImageIO, JAI and JDeli.
We previously used ImageIO to read BMP files, but over time became increasingly dissatisfied as we discovered more and more issues. Eventually we wrote our own BMP Decoder which fixes those issues and is now available as part of JDeli.
What is BMP?
A BMP, bitmap image file, is a file format which stores images. It supports 2d images in monochrome (one colour) and colour. The BMP file format supports 2 file extensions including .bmp and .dib (Device Independant Bitmap). It was primarily designed for Window platforms although it also works on other platforms.
How to read BMP images in Java?
Here are examples of how BMP images can be read in Java.
File bmpFile = new File("bmpImage.bmp"); BufferedImage image = ImageIO.read(bmpFile);
RenderedImage image = JAI.create("fileload", "test1.bmp");
You can easily replace ImageIO (and get much better support for BMP files) by just changing ImageIO.read to JDeli.read:
//Read Image (can also be OutputStream or byte array) File bmpFile = new File("bmpImage.bmp"); BufferedImage image = JDeli.read(bmpFile);
Or you can use the JDeli BmpDecoder directly:
BmpDecoder decoder = new BmpDecoder(); BufferedImage image = decoder.read(rawImageBytes);
Why use JDeli?
JDeli offers a range of advantages over ImageIO and alternatives, including:
- prevents heap related JVM crashes
- implements unsupported image formats
- reduce output file size
- improve read/write performance
- supports threading
- superior image scaling algorithms
This article is part of our series on reading and writing image files in Java.