通過 Java 轉到 WEBP
構建您自己的 Java 應用程序以使用服務器端 API 合併 WEBP 文件。
如何使用 Java 合併 WEBP 文件
如果您想讓您的創造力盡情發揮,那麼現在就是使用圖像拼貼的時候了。即使源文件格式不同,此功能也允許您合併照片和圖像。吸引人們注意您的作品集的一個好方法是使用帶有重複圖像或圖案的照片壁紙。在這種情況下,可以在水平和垂直方向上組合圖像。如果您需要演示處理文件的結果,您可以輕鬆合併兩個圖像:應用照片效果之前和之後。為了合併 WEBP 文件,我們將使用 Aspose.Imaging for Java API 是一個功能豐富、功能強大 易於使用的 Java 平台圖像處理和轉換 API。您可以直接從 Maven 並通過將以下配置添加到 pom.xml 將其安裝在基於 Maven 的項目中。
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging</artifactId>
<version>version of aspose-imaging API</version>
<classifier>jdk16</classifier>
</dependency>
通過 Java 合併 WEBP 的步驟
你需要 aspose-imaging-version-jdk16.jar 在您自己的環境中嘗試以下工作流程。
- 使用 Image.load 方法加載 WEBP 文件
- 將圖像組合成新的圖像
- 將裁剪後的圖像以 Aspose.Imaging 支持的格式保存到光盤
系統要求
所有主要操作系統都支持 Java 的 Aspose.Imaging。只需確保您具有以下先決條件。
- 已安裝 JDK 1.6 或更高版本。
合併 WEBP 圖像 - Java
import com.aspose.imaging.Color; | |
import com.aspose.imaging.Graphics; | |
import com.aspose.imaging.Image; | |
import com.aspose.imaging.RectangleF; | |
import com.aspose.imaging.fileformats.png.PngColorType; | |
import com.aspose.imaging.imageoptions.PngOptions; | |
import com.aspose.imaging.sources.StreamSource; | |
import java.io.File; | |
import java.io.FilenameFilter; | |
import java.util.ArrayList; | |
import java.util.List; | |
enum MergeStyle | |
{ | |
Horizontal, | |
Vertical | |
} | |
// Test directory should have source images to merge | |
String TestDirectory = "D:\\Many2One\\"; | |
MergeStyle mStyle = MergeStyle.Horizontal; // or MergeStyle.Vertical | |
List<Image> images = new ArrayList<Image>(); | |
try | |
{ | |
int totalWidth = 0; | |
int totalHeight = 0; | |
int maxWidth = 0; | |
int maxHeight = 0; | |
final String fileMask = ".png"; | |
File[] files = new File(TestDirectory).listFiles(new FilenameFilter() | |
{ | |
@Override | |
public boolean accept(File dir, String name) | |
{ | |
return name.endsWith(fileMask); | |
} | |
}); | |
if (files == null) | |
{ | |
return; | |
} | |
for (File fileName : files) | |
{ | |
try (Image image = Image.load(fileName.getAbsolutePath())) | |
{ | |
totalWidth += image.getWidth(); | |
if (image.getWidth() > maxWidth) | |
{ | |
maxWidth = image.getWidth(); | |
} | |
totalHeight += image.getHeight(); | |
if (image.getHeight() > maxHeight) | |
{ | |
maxHeight = image.getHeight(); | |
} | |
images.add(image); | |
} | |
} | |
if (images.isEmpty()) | |
{ | |
return; | |
} | |
String mergeStyle; | |
int targetWidth, targetHeight; | |
if (mStyle == MergeStyle.Horizontal) | |
{ | |
targetWidth = totalWidth; | |
targetHeight = maxHeight; | |
} | |
else if (mStyle == MergeStyle.Vertical) | |
{ | |
targetWidth = maxWidth; | |
targetHeight = totalHeight; | |
} | |
else | |
{ | |
return; // other merge styles are not supported | |
} | |
String outputPath = TestDirectory + "output\\"; | |
File dir = new File(outputPath); | |
assert dir.exists() || dir.mkdirs(); | |
outputPath = outputPath + "merged_" + mStyle.name() + ".png"); | |
PngOptions pngOptions = new PngOptions(); | |
pngOptions.setColorType(PngColorType.TruecolorWithAlpha); | |
pngOptions.setSource(new StreamSource()); | |
try (Image image = Image.create(pngOptions, targetWidth, targetHeight)) | |
{ | |
image.setBackgroundColor(Color.getWhite()); | |
final Graphics graphics = new Graphics(image); | |
float x = 0, y = 0; | |
for (Image it : images) | |
{ | |
graphics.drawImage(it, new RectangleF(x, y, it.getWidth(), it.getHeight())); | |
if (mStyle == MergeStyle.Horizontal) | |
{ | |
x += it.getWidth(); | |
} | |
if (mStyle == MergeStyle.Vertical) | |
{ | |
y += it.getHeight(); | |
} | |
} | |
image.save(outputPath); | |
} | |
} | |
finally | |
{ | |
for (Image image : images) | |
{ | |
image.close(); | |
} | |
images.clear(); | |
} |
關於 Java API 的 Aspose.Imaging
Aspose.Imaging API 是一種圖像處理解決方案,用於在應用程序中創建、修改、繪製或轉換圖像(照片)。它提供:跨平台的圖像處理,包括但不限於各種圖像格式之間的轉換(包括統一的多頁或多幀圖像處理)、繪圖等修改、使用圖形基元、轉換(調整大小、裁剪、翻轉和旋轉) 、二值化、灰度、調整)、高級圖像處理功能(過濾、抖動、遮罩、去偏斜)和內存優化策略。它是一個獨立的庫,不依賴任何軟件進行圖像操作。可以在項目中使用原生 API 輕鬆添加高性能圖像轉換功能。這些是 100% 私有的本地 API,圖像在您的服務器上處理。通過在線應用合併 WEBP
通過訪問我們的 Live Demos 網站 合併 WEBP 文檔。 現場演示有以下好處
WEBP 什麼是 WEBP 文件格式
WebP 是 Google 推出的一種現代光柵 Web 圖像文件格式,它基於無損和有損壓縮。它提供相同的圖像質量,同時大大減小了圖像尺寸。由於大多數網頁使用圖像作為數據的有效表示,因此在網頁中使用 WebP 圖像會導致網頁加載速度更快。根據谷歌的說法,WebP 無損圖像的大小比 PNG 小 26%,而 WebP 有損圖像比同類 JPEG 圖像小 25-34%。基於 WebP 和其他圖像文件格式之間的結構相似性 (SSIM) 索引比較圖像。 WebP 是 WebM 多媒體容器格式的姊妹項目。
閱讀更多其他支持的合併格式
使用 Java,可以輕鬆合併不同的格式,包括。