Convert PNG to JPEG image file in Java

by Subramanian 2012-09-13 11:11:20

package com.mkyong;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ConvertImageFile {

public static void main(String[] args) {

BufferedImage bufferedImage;

try {

//read image file
bufferedImage = ImageIO.read(new File("c:\\javanullpointer.png"));

// create a blank, RGB, same width and height, and a white background
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);

// write to jpeg file
ImageIO.write(newBufferedImage, "jpg", new File("c:\\javanullpointer.jpg"));

System.out.println("Done");

} catch (IOException e) {

e.printStackTrace();

}

}

}

Tagged in:

769
like
0
dislike
0
mail
flag

You must LOGIN to add comments