当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java PNG Images转JPEG用法及代码示例


PNG 和 JPG 格式用于图像插图。这两种格式都用于提供与某些类型图像的良好兼容性,例如 PNG 更适合线条图和图标图形,而 JPG 更适合照片。然而,两者在媒体和图片的使用和存储方面可以相互转换。

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

用法:

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

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

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

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

异常:它会抛出IO异常如果执行过程中出现错误。稍后将在实现部分中展示。

执行:

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/

示例

Java


// Java Program to Convert PNG Image to JPEG Image
// Importing BufferedImage 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 occurred
            ex.printStackTrace();
        }
    }
}

输出:

情况1:抛出错误时

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

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

Image converted to jpeg successfully.

执行后会显示图像已成功转换为jpeg,我们可以在控制台上看到并在文件中创建了一个新的jpeg图像



相关用法


注:本文由纯净天空筛选整理自sam_2200大神的英文原创作品 Java Program to Convert PNG Images to JPEG。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。