Convert BMP file to JPG using JAI in Java

In the final part of the BMP / JPEG conversion in Java examples, we will provide the code to convert between these formats using Java Advanced Imaging API. To work with this example code, you need to have a copy of the following JAR files:

  • jai_core.jar 
  • jai_codec.jar 

Using JAI to convert BMP to JPG in Java – Example

Full working code example in JAI is provided below:
public class jai_bmp_jpg {
  public static void main(String[] args) throws Exception {
      String filename="screendump.bmp";
      PlanarImage inputfile = JAI.create("fileload", filename); //Read input image 
      JAI.create("filestore",inputfile,"jai_output_image.jpg","JPEG");//write output in JPG Format      

Just two lines of code in JAI, and the output is converted to JPG instantly. The file size in case of BMP to JPG conversion using JAI is same as the ImageIO example. You can refer to the file size in Part 2 of this tutorial. (check the links below at the bottom of this post)

That completes a quick tutorial to convert BMP to JPEG 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 JPG in Java?

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


1 comment:

  1. Is there a way to convert the image and store it in a temporary variable without writing it to disk?