Convert BMP file to JPG using JMagick in Java

In this tutorial, we will explain how to convert a bitmap image file (BMP format) to JPG format using Java. There are quite a number of options available to do this conversion. We will use three different methods to do the conversion. You can follow any one of these depending on your needs. The approaches we will be using are documented below:

  • Use JMagick in Java
  • Use ImageIO built in library in Java
  • Use Java Advanced Imaging library to convert (JAI)

The example code for each of the scenarios will be provided in this series. The input BMP image we will be using is a 3.7 MB image, with properties as provided below:

Convert BMP to JPG in Java - Image Size Before Conversion
Convert BMP to JPG in Java - Image Size Before Conversion

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


Using JMagick to Convert BMP to JPG in Java – Example


First download jmagick.jar and jmagick.dll from the provider website. This is a open source library that can accept a BMP image and convert that into JPG. The step by step code to do the conversion in JMagick is provided below: (Note that we are not setting any quality factor at the moment, we just want to do the conversion and check the size)

  import magick.ImageInfo; //This class will be used to accept the input BMP file
  //import magick.Magick; //
  import magick.MagickImage; //This class will convert the image to JPG format
  public class jmagick_bmp_jpg {
    public static void main(String[] args) throws Exception {
      
     String inputfileName = "screendump.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_converted_image.jpg"; //Output File name
     magick_converter.setFileName(outputfile); //set output file format
     magick_converter.writeImage(info); //do the conversion
     
    }
  }
  
 
You can download the complete code from the link below:

The output image size in case of JMagick based conversion is 236Kb.

JMagick Converted JPG Image Output - Example
JMagick Converted JPG Image Output - Example
This ends Part 1 of our tutorial. In the next part, we will discuss how to convert BMP to JPG in Java by using ImageIO library.
This Series: How to convert BMP file to JPG in Java?

Keywords: Convert BMP to JPEG, Convert BMP to JPG, JAI, ImageIO, JMagick, Java Example

Tutorials:


2 comments:

  1. Nice and well-explained tutorial. Can you also please tell me how to do the other way round, meaning from JPG to BMP (RGB with each 8 bit)?

    Many thanks in advance!

    ReplyDelete
  2. @Anonymous,

    Thanks very much. Can you try the same code by changing input and output files, I guess JMagick is flexible in conversion process. Let me know what error you are getting in the process?

    ReplyDelete