Java Program to Convert PNG Images to JPEG
Last Updated :
13 Jun, 2022
PNG and JPG formats are used for image illustrations. Both the formats are used to provide good compatibilities with certain types of images like PNG works better with line drawings and icon graphics whereas JPG works well with photographs. However, both are interconvertible with respect to each other for usage and storage of media and pictures.
In Java, write() method is used to convert an image from gif type of format to jpg, use the static method write() provided by the class ImageIO under javax.imageio package. Following utility class implements a static method known as converImg(), which takes input and output image path as parameters and format output image.
Syntax:
boolean write(RenderedImage image, String formatName, OutputStream output)
Parameters: write() method accepts 3 parameters namely image, formatName and output.
- Image: The input image as a subclass of the RenderedImage interface, 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 type of the output image.
- output specifies an OutputStream to which the output image will be written.
Return Type: Boolean value where returning true if it can find an ImageWriter and perform conversion successfully else it will return false.
Exceptions: It will throw IOException if an error occurs during execution. It is shown later on in the implementation part.
Implementation:
Note: For input and output images path, Input and input images been passed are present on the desktop of the machine
- Input image with name “demoImage.png”
- Output Image with name “demoImage.jpeg”
Code is present at the directory mentioned below
- /Users/mayanksolanki/Desktop/Job/Coding/GFG/Folder/
Example
Java
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
class HelperClass {
public static boolean convertImg(String inputImgPath,
String outputImgPath,
String formatType)
throws IOException
{
FileInputStream inputStream
= new FileInputStream(inputImgPath);
FileOutputStream outputStream
= new FileOutputStream(outputImgPath);
BufferedImage inputImage
= ImageIO.read(inputStream);
boolean result = ImageIO.write(
inputImage, formatType, outputStream);
outputStream.close();
inputStream.close();
return result;
}
}
public class GFG {
public static void main(String[] args)
{
String inputImage
= "/Users/mayanksolanki/Desktop/demoImage.png" ;
String outputImage
= "/Users/mayanksolanki/Desktop/demoImage.jpeg" ;
String formatType = "JPEG" ;
try {
boolean result = HelperClass.convertImg(
inputImage, outputImage, formatType);
if (result) {
System.out.println(
"Image converted to jpeg successfully." );
}
else {
System.out.println(
"Could not convert image." );
}
}
catch (IOException ex) {
System.out.println(
"Error during converting image." );
ex.printStackTrace();
}
}
}
|
Output:
Case 1: When an error is thrown
Case 2: Successful compilation but exception is thrown at runtime(failed to run properly)
Case 3: Successful compilation and successfully run
Image converted to jpeg successfully.
When executed it will show Image converted to jpeg successfully, we can find that on console and a new jpeg image created in the file
Share your thoughts in the comments
Please Login to comment...