Convert BMP File to PNG in Java Example - Part 3

We have so far covered JMagick (part 1) and Image IO (Part 2) based BMP to PNG conversion in Java with suitable examples. In the last part of this tutorial series, we will use JAI (Java Advanced Imaging) API and explain how to convert BMP to PNG in Java. You need to download a copy of the JAI libraries for working with the examples provided in this part.

How to convert BMP to PNG using JAI – Java Example


Download Required JAR Files

In order to work with the examples provided in this tutorial, you need to download the following JAR files and have them in your classpath. 

  • jai_core.jar
  • jai_codec.jar

These JAR files are supplied with the JAI package that you can download from Oracle website.

Complete Program

The complete program to achieve the conversion with JAI API is provided below
import javax.media.jai.*; //JAI classes to support conversion of BMP to PNG
public class jai_bmp_png {
  public static void main(String[] args) throws Exception {
      String filename="Convert_to_PNG.bmp";
      PlanarImage bmpinputfile = JAI.create("fileload", filename); //Read BMP input image
      JAI.create("filestore",bmpinputfile,"jai_output_image.png","PNG");//Output image in PNG format      
  }
}

Just two lines of code in JAI, and the output is converted to PNG instantly. Strangely, the file size in case of using JAI to convert, is more than the size generated by ImageIO. The file size was the same in the case of BMP to JPG conversion. Looks like JAI uses a different algorithm for PNG conversion, which could possibly explain the difference to the file size. The file size in case of JAI is found to be 128 Kb. (More than JMagick / Image IO). It is shown in the figure below:

JAI Converted PNG Image Output - Example
JAI Converted PNG Image Output - Example
Download Source Code

You can download the source code for this tutorial from the link below
That completes a quick tutorial to convert BMP to PNG using all three popular Java imaging APIs; JMagick, ImageIO and JAI. We will see some more image conversion examples in the next post. In the meantime, if you have a question, you can post it in the comments section.
This Series:  How to convert BMP file to PNG format in Java?

Keywords: JAI BMP to PNG, JMagick BMP to PNG, ImageIO BMP to PNG, Java Image conversion Example, Java BMP to PNG Example

Tutorial Parts:
Part 1 – Convert BMP to PNG in Java using JMagick
Part 2 – Convert BMP to PNG in Java using ImageIO
Part 3 -  Convert BMP to PNG in Java using JAI

No comments:

Post a Comment