📜  将 PNG 图像转换为 JPEG 的Java程序

📅  最后修改于: 2022-05-13 01:54:53.169000             🧑  作者: Mango

将 PNG 图像转换为 JPEG 的Java程序

PNG 和 JPG格式用于图像插图。这两种格式都用于为某些类型的图像提供良好的兼容性,例如 PNG 对线条图和图标图形效果更好,而 JPG 对照片效果很好。然而,对于媒体和图片的使用和存储,两者相对于彼此是可相互转换的。

在Java,write() 方法用于将图像从 gif 类型的格式转换为 jpg,使用javax.imageio 包下 ImageIO 类提供的静态方法 write()。下面的实用程序类实现了一个称为converImg()的静态方法,它以输入和输出图像路径作为参数并格式化输出图像。

句法:

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

参数: write() 方法接受 3 个参数,即图像、格式名称和输出。

  1. Image:输入图像作为RenderedImage接口的子类,如BufferedImage。要从输入图像文件中获取BufferedImage对象,我们可以使用ImageIO类也提供的read(InputStream)。
  2. formatName:指定输出图像的格式类型。
  3. output指定输出图像将写入的 OutputStream。

返回类型:布尔值,如果可以找到 ImageWriter 并成功执行转换,则返回 true,否则返回 false。



异常:如果在执行过程中发生错误,它将抛出 IOException。它在稍后的实现部分中显示。

执行:

例子

Java
// Java Program to Convert PNG Image to JPEG Image
 
// Importing BufferdImage class from java.awt package
// to describe an image with accessible buffer of image
import java.awt.image.BufferedImage;
// Importing all input output classes
import java.io.*;
// Importing an interface
// to determine the setting of IIOParam object
import javax.imageio.ImageIO;
 
// Class 1
// helper class
class HelperClass {
 
    // Method
    // To convert image format
    public static boolean convertImg(String inputImgPath,
                                     String outputImgPath,
                                     String formatType)
 
        throws IOException
    {
 
        // Creating an object  of FileInputStream to read
        FileInputStream inputStream
            = new FileInputStream(inputImgPath);
 
        // Creating an object  of FileOutputStream to write
        FileOutputStream outputStream
            = new FileOutputStream(outputImgPath);
 
        // Reading the  input image from file
        BufferedImage inputImage
            = ImageIO.read(inputStream);
 
        // Writing to the output image in specified format
        boolean result = ImageIO.write(
            inputImage, formatType, outputStream);
 
        // Closing the streams in order to avoid read write
        // operations
        outputStream.close();
        inputStream.close();
 
        return result;
    }
}
 
// Class 2
// Main class
public class GFG {
 
    // Main class
    public static void main(String[] args)
    {
 
        // Here, the local directories from machine
        //  is passed as in strings
 
        // Creating a string to store the path of image
        // to be converted
        String inputImage
            = "/Users/mayanksolanki/Desktop/demoImage.png";
 
        // Creating a string to
        // store path of converted image
        String outputImage
            = "/Users/mayanksolanki/Desktop/demoImage.jpeg";
        // Creating another string that will be
        // store format of converted image
 
        // Simply creating  creating just to hold the format
        // type
        String formatType = "JPEG";
 
        // Try block to check for exceptions
        try {
            // result will store boolean value whether image
            // is converted successfully or not
 
            boolean result = HelperClass.convertImg(
                inputImage, outputImage, formatType);
 
            if (result) {
 
                // Display message when image is converted
                // successfully
                System.out.println(
                    "Image converted to jpeg successfully.");
            }
            else {
 
                // Display message when image is not
                // converted successfully
                System.out.println(
                    "Could not convert image.");
            }
        }
 
        // Catch block to handle the exceptions
        catch (IOException ex) {
 
            // Display message when exception is thrown
            System.out.println(
                "Error during converting image.");
 
            // Print the line number
            // where the exception occured
            ex.printStackTrace();
        }
    }
}


输出:

情况一:抛出错误时



情况二:编译成功但运行时抛出异常(无法正常运行)

案例三:编译成功并成功运行

Image converted to jpeg successfully.

执行后会显示Image转换为jpeg成功,我们可以在console上找到,文件中新建了一个jpeg图片