在 Java 中將 PostScript 轉換為 GIF

PostScript 文件轉換為高品質圖像現在不是一項困難的任務。Aspose.Page 為多種編程語言提供高級代碼 API,以便以編程方式轉換和操作 PS 文件。此外,它還支持企業級 在線 PS 文件轉換器,將 PS 文件轉換為其他流行的文件格式。然而,本指南是為尋找 PostScript Java 函式庫 的 Java 程序員準備的,以 以編程方式將 PostScript 轉換為 GIF。您可以在幾乎所有流行的操作系統上輕鬆打開 GIF 圖像,而無需安裝軟件。因此,讓我們開始吧。

我們將在這篇博客文章中涵蓋以下幾個部分:

  1. PostScript Java 函式庫安裝
  2. 在 Java 中將 PostScript 轉換為 GIF - 代碼示例
  3. PS 文件轉換器 - 在線工具

PostScript Java 函式庫安裝

要安裝 Aspose.Page for Java,您可以 下載 JAR 文件或使用以下 Maven 配置:

 <repositories>
     <repository>
         <id>AsposeJavaAPI</id>
         <name>Aspose Java API</name>
         <url>https://releases.aspose.com/java/repo/</url>
     </repository>
</repositories>
 <dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-page</artifactId>
        <version>23.12</version>
    </dependency>
</dependencies>

此外,您可以訪問此 安裝 指南以了解完整的詳細資訊。

PostScript Java 函式庫

在 Java 中將 PostScript 轉換為 GIF - 代碼示例

現在,您可以為商業軟件開發一個 PS 到 GIF 的製作器。因此,請按照以下步驟操作:

  • 將 ImageFormat 設置為 GIF 並加載源 PostScript 文件。
  • 使用 PostScript 輸入流初始化 PsDocument 類的對象。
  • 如果您希望儘管有小錯誤也能轉換 PostScript 文件,請設置 suppressErrors 的值。
  • 使用 suppressErrors 參數實例化 ImageSaveOptions 類的新對象。
  • 創建 ImageDevice 類的實例。
  • 調用 save 方法將 GIF 文件保存到磁碟。
  • 調用 getImagesBytes 方法以獲取結果圖像的字節。
  • 通過使用輸出圖像路徑初始化 FileOutputStream 類的實例來創建輸出流。

以下代碼示例演示了如何 以編程方式將 PostScript 轉換為 GIF

package com.example;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.aspose.eps.PsDocument;
import com.aspose.eps.device.ImageDevice;
import com.aspose.eps.device.ImageSaveOptions;
public class Main {
// High Code Java API to Convert PostScript to GIF
public static void main(String[] args) throws Exception {
// Set the ImageFormat to GIF.
com.aspose.page.ImageFormat imageFormat = com.aspose.page.ImageFormat.GIF;
// Load the source PostScript file.
FileInputStream psStream = new FileInputStream("/input.ps");
// Initialize an object of the PsDocument class with the PostScript input stream.
PsDocument document = new PsDocument(psStream);
// Set the value of suppressErrors if you want to convert the Postscript file despite minor errors.
boolean suppressErrors = true;
// Instantiate a new object of ImageSaveOptions class with suppressErrors parameter.
ImageSaveOptions options = new ImageSaveOptions(suppressErrors);
// Create an instance of the ImageDevice class.
ImageDevice device = new ImageDevice();
try {
// Call the save method to save the GIF file on the disk.
document.save(device, options);
} finally {
psStream.close();
}
// Invoke the getImagesBytes method to get the resulting images in bytes.
byte[][] imagesBytes = device.getImagesBytes();
int i = 0;
for (byte [] imageBytes : imagesBytes) {
String imagePath = "/PSToGIF" + i + "." + imageFormat.toString().toLowerCase();
// Create output stream by initializing an instance of the FileOutputStream class with the output image path.
FileOutputStream fs = new FileOutputStream(imagePath);
try {
fs.write(imageBytes, 0, imageBytes.length);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
fs.close();
}
i++;
}
//Review errors
if (suppressErrors) {
for (Exception ex : options.getExceptions()) {
System.out.println(ex.getMessage());
}
}
}
}

您可以在下圖中看到輸出:

從圖像中製作 GIF

PS 文件轉換器 - 在線工具

這個 在線 工具提供豐富的 PS 轉換為其他廣泛使用的文件格式。它由 Aspose.Page 提供支持,是一個免費工具。此外,在轉換為其他文件格式時,它保持文本和圖形數據的質量。我們強烈建議您使用這個 PS 文件轉換器 來提高生產力。

PS 文件轉換器

從圖像中製作 GIF - 獲取免費許可證

現在,您可以獲得一個 免費臨時許可證 以超越評估限制嘗試 PostScript Java 函式庫。

結論

總之,我們已經了解了如何 以編程方式將 PostScript 轉換為 GIF。此外,我們實現了開發高效 PS 文件轉換器 的功能。同樣,您可以通過訪問 文檔、GitHub repo 和 API 參考 來進一步探索這個 PostScript Java 函式庫。最後,aspose.com 將持續發佈博客文章。因此,請保持聯繫以獲取更新。您可以在我們的 論壇 上分享您的問題或查詢。

參見