當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。