In this article I will show you how to write BMP images in Java using ImageIO, and JDeli.
We previously used ImageIO to write BMPfiles, but over time became increasingly dissatisfied as we discovered more and more issues. Eventually we wrote our own BMP Encoder which fixes those issues and is now available as part of JDeli.
What is a BMP file?
A BMP is an image file format, commonly known as a bitmap image, with the extension .bmp or .dib. It was originally created by Microsoft for computers running Windows, and it specifies the color of the pixels in a way that allows them to be displayed on any device.
ImageIO.write(bufferedImage, "BMP", new File("/path/to/outputFile.bmp"));
Javadoc (included in JDK)
You can easily replace ImageIO (and get much better support for BMP files) by just changing ImageIO.write to JDeli.write.
//Write Image (can also be OutputStream) File myNewBmpFile=new File("ImageAsBmp.bmp"); JDeli.write(myBufferedImage, "bmp", myNewBmpFile);
Or you can use the JDeli BmpEncoder directly:
BmpEncoder encoder = new BmpEncoder(); encoder.write(bufferedImage, outputStream);
The JDeli library supports Bilevel, Grayscale and RGB images.
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.
IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.