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


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


JPEG 和 GIF两者都是一种存储图像的图像格式。 JPEG 用途有损压缩算法图像可能会丢失一些数据,而 GIF 使用无损压缩算法,GIF 格式不存在图像数据丢失。 GIF 图像支持动画和透明度。有时需要在我们需要具有指定扩展名的图像文件的地方附加图像。我们有不同扩展名的图像,需要使用指定的扩展名进行转换,如下所示,我们将扩展名为 .jpeg 的图像转换为 .gif,反之亦然。

在Java中,write()方法用于将图像从gif类型格式转换为jpg,使用类ImageIO提供的静态方法write()javax.imageio包

用法:

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异常如果执行过程中出现错误。稍后将在实现部分中展示。

以下实用程序类实现了静态方法,convertImg()它将输入和输出图像路径作为参数。

程序:

要将 .jpeg 转换为 .gif,请在下面的代码中进行以下必要的更改

  • 将“formatType”更改为 GIF。
  • 在“outputImage”中,使用正确的文件扩展名更改图像的名称。
  • 同样,使用正确的文件扩展名更改“inputImage”的名称。

执行:

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.gif”
  • 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 GIF 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 Helper {
    // Method to convert image to JPEG
    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 driver method
    public static void main(String[] args) throws IllegalStateException
    {
        // 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.gif";
        // 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 = Helper.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 GIF Images to JPEG。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。