In Java, to convert an image from one type of format to another, use the static method write()provided by the class ImageIO under javax.imageio package. The signature of this method is as follows:

boolean write(RenderedImage image, String formatName, OutputStream output)

This method accepts three parameters:

The write() method returns true if it can find an ImageWriter and performs the conversion successfully, and return false if it couldn’t find any ImageWriter for the specified format. It throws an IOException exception if an error occurs during writing.

To demonstrate, the following utility class implements a static method, convertFormat(), which takes an input image path, an output image path, and format name of the output image:

package net.codejava.graphic;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageConverter {

	/**
	 * Converts an image to another format
	 * 
	 * @param inputImagePath Path of the source image
	 * @param outputImagePath Path of the destination image
	 * @param formatName the format to be converted to, one of: jpeg, png, 
	 * bmp, wbmp, and gif
	 * @return true if successful, false otherwise
	 * @throws IOException if errors occur during writing
	 */
	public static boolean convertFormat(String inputImagePath,
			String outputImagePath, String formatName) throws IOException {
		FileInputStream inputStream = new FileInputStream(inputImagePath);
		FileOutputStream outputStream = new FileOutputStream(outputImagePath);
		
		// reads input image from file
		BufferedImage inputImage = ImageIO.read(inputStream);
		
		// writes to the output image in specified format
		boolean result = ImageIO.write(inputImage, formatName, outputStream);
		
		// needs to close the streams
		outputStream.close();
		inputStream.close();
		
		return result;
	}
} 

 

And here is the code that uses the utility class above:

package net.codejava.graphic;

import java.io.IOException;

public class TestImageConverter {

	public static void main(String[] args) {
		String inputImage = "D:/Photo/Pic1.jpg";
		String oututImage = "D:/Photo/Pic1.png";
		String formatName = "PNG";
		try {
			boolean result = ImageConverter.convertFormat(inputImage,
					oututImage, formatName);
			if (result) {
				System.out.println("Image converted successfully.");
			} else {
				System.out.println("Could not convert image.");
			}
		} catch (IOException ex) {
			System.out.println("Error during converting image.");
			ex.printStackTrace();
		}
	}
} 

 

Other Java Graphics Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (ImageConverter.java)ImageConverter.java[utility class]1 kB
Download this file (TestImageConverter.java)TestImageConverter.java[test class]0.6 kB