Convert BMP file to JPG using Image IO in Java

Just to recap about the previous post, we used JMagick library in Java to convert BMP to JPG. This is the second post of the series, which walks you through on how to convert a bitmap image file (BMP format) to JPG using ImageIO built in library in Java. For this scenario, it is required to accept the input image into a java.awt.image.BufferedImage class, and then pass this image into the write method of Image IO class.

Method 2: Using ImageIO to Convert BMP to JPG – Program Example


The complete code example to convert BMP to JPG using ImageIO library is provided below:
  import java.awt.image.BufferedImage; //This class will read the incoming BMP image
  import javax.imageio.ImageIO; //This class will write the BMP image as JPG
  import java.io.File; //to read input and write output images
  public class imageio_bmp_jpg {
    public static void main(String[] args) throws Exception {
     BufferedImage input_image = null; 
     input_image = ImageIO.read(new File("screendump.bmp")); //read bmp into input_image object
     File outputfile = new File("imageio_converted.jpg"); //create new outputfile object
     ImageIO.write(input_image, "jpg", outputfile); //write JPG output to file 
     
    }
  }
  
 

You can download the Java program from the link below:

There is a definite difference in file size when using ImageIO to convert image formats. The file size is in this case is provided below: (173 Kb against 236 Kb when using JMagick)

JMagick Converted JPG Image Output - Example
ImageIO Converted JPG Image Output - Example
Thus, part 2 of this tutorial is now complete. In the final part, we will discuss how to use Java Advanced Imaging (JAI) library for this requirement.
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:


1 comment: