Convert BMP File to PNG in Java Example - Part 1

In the last set of tutorials, we examined how to convert an image file from BMP to JPEG format using different Java libraries. Specifically we explained this conversion in JMagick, ImageIO and Java Advanced Imaging APIs (JAI). In this example, we will discuss how to convert BMP image file to PNG format in Java. As along the last series, we will use the following APIs to explain the conversion:

  • JMagick
  • ImageIO
  • Java Advanced Imaging library
We would like to use a slightly complex BMP with high color information for this post.  The input BMP image we will be using is a 1.94 MB image, with properties as provided below:

Convert BMP to PNG using Java – Input Image Size
Convert BMP to PNG using Java – Input Image Size

With a standard conversion with all these three libraries, let us examine the output.

How to convert BMP to PNG using JMagick – Java Example


Download Required JAR Files

As a first step to the conversion process, download jmagick.jar and jmagick.dll from the provider website.

Complete Program

JMagick open source library has built in methods to quickly convert BMP to PNG. The complete code example to achieve this conversion is provided below:
import magick.ImageInfo; //This class will be used to accept the input BMP file
import magick.MagickImage; //This class will convert BMP to PNG format
public class jmagick_bmp_png {
  public static void main(String[] args) throws Exception {
       
      String inputfileName = "Convert_to_PNG.bmp"; //Input BMP file
      ImageInfo info = new ImageInfo(inputfileName); //Get BMP file into ImageInfo object
      MagickImage magick_converter = new MagickImage(info); //Create MagickImage object
   
      String outputfile = "jmagick_png_img_output.png"; //Output File name
      magick_converter.setFileName(outputfile); //set output file format
      magick_converter.writeImage(info); //do the conversion
      
  }
}

The output image size in case of JMagick based conversion is 123 Kb which can be seen from the figure below:

JMagick Converted PNG Image Output - Example
JMagick Converted PNG Image Output - Example
Download Code Example

You can download the complete code from the link below:

That completes Part 1 of this tutorial. In the next part, we will discuss how to do the conversion using ImageIO component of Java.

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