How to Convert PNG to JPEG Image File in Java


In this example we will show how to convert PNG to JPEG image file in Java.

Source Code

package com.beginner.examples;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ConvertPNGToJPG {

    public static void main(String[] args) {

        try {
            BufferedImage bufferedImage1 = ImageIO.read(new File("E:tmptest.png"));
            BufferedImage bufferedImage2 = new BufferedImage(bufferedImage1.getWidth(), bufferedImage1.getHeight(), BufferedImage.TYPE_INT_RGB);
            bufferedImage2.createGraphics().drawImage(bufferedImage1, 0, 0, Color.WHITE, null);
            ImageIO.write(bufferedImage2, "jpg", new File("E:tmptest.jpg"));
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments