How to convert image format using Java
- Details
- Written by Nam Ha Minh
- Last Updated on 10 August 2019   |   Print Email
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:
- How to add watermark for images using Java
- How to resize images using Java
- How to draw image with automatic scaling in Java
- How to capture screenshot programmatically in Java
- How to draw text vertically with Java Graphics2D
- How to Create Zoomable User Interface Java Programs with Piccolo2D Framework
- Drawing lines examples with Java Graphics2D
- Drawing Rectangles Examples with Java Graphics2D
- Using JFreechart to draw line chart with CategoryDataset
- Using JFreechart to draw XY line chart with XYDataset
Comments
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.
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.
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.
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.