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:

  • image: specifies the input image as a subclass of the RenderedImageinterface, such as BufferedImage. To obtain a BufferedImage object from the input image file, we can use the read(InputStream) which is also provided by the ImageIO class.
  • formatName: specifies format name of the output image , which can be: JPG, JPEG, PNG, BMP, WBMP and GIF.
  • output: specifies an OutputStream to which the output image will be written.

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

Add comment

   


Comments 

#13HalfNoLife2021-07-23 09:02
Thanks a lot! This was very usefull for me!
Quote
#12Yogesh K2021-05-12 11:42
Hi,
Thanks You for sharing example.

I have tried to convert pdf to image. I am having 2 different flows in single application which will create 2 different PDF.
I have used same code to covert that PDF to JPG file.


When I tried with few requests, it works fine. If I tried with Thousands of request in each flow then some times it will throw ConcurrentModificationException.

Can You please help me on that.
Quote
#11Masehullah Hami2021-01-28 21:20
This is Masehullah Hami Software Engineer,
I follow all of your tutorials but I cannot see any
thing about JavaFX and all the code are for Swing,
So if it is possible make some tutorials about JavaFx
as well.
THX Bro.
Quote
#10Jitu2020-12-08 00:46
Hello,

I was searching to convert the tiff and tiff graphics in gif format. the code which you have given here, it's not working.
can you please provide the ready code?

Thanks in advnace.
Quote
#9Raghavender2020-09-18 07:15
Hi

i have a base64 encode stram of tiff image and need to convert to pngor jpg to disply in html.

Can u help how we can do this.
Quote