通过 Java 转到 PNG
构建您自己的 Java 应用程序以使用服务器端 API 合并 PNG 文件。
如何使用 Java 合并 PNG 文件
如果您想让您的创造力尽情发挥,那么现在就是使用图像拼贴的时候了。即使源文件格式不同,此功能也允许您合并照片和图像。吸引人们注意您的作品集的一个好方法是使用带有重复图像或图案的照片壁纸。在这种情况下,可以在水平和垂直方向上组合图像。如果您需要演示处理文件的结果,您可以轻松合并两个图像:应用照片效果之前和之后。为了合并 PNG 文件,我们将使用 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 合并 PNG 的步骤
你需要 aspose-imaging-version-jdk16.jar 在您自己的环境中尝试以下工作流程。
- 使用 Image.load 方法加载 PNG 文件
- 将图像组合成新的图像
- 将裁剪后的图像以 Aspose.Imaging 支持的格式保存到光盘
系统要求
所有主要操作系统都支持 Java 的 Aspose.Imaging。只需确保您具有以下先决条件。
- 已安装 JDK 1.6 或更高版本。
合并 PNG 图像 - 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,图像在您的服务器上处理。通过在线应用合并 PNG
通过访问我们的 Live Demos 网站 合并 PNG 文档。 现场演示有以下好处
PNG 什么是 PNG 文件格式
PNG,便携式网络图形,是指一种使用无损压缩的光栅图像文件格式。此文件格式是作为图形交换格式 (GIF) 的替代品而创建的,没有版权限制。但是,PNG 文件格式不支持动画。 PNG文件格式支持无损图像压缩,使其在用户中很受欢迎。随着时间的推移,PNG 已经发展成为最常用的图像文件格式之一。几乎所有操作系统都支持打开 PNG 文件。例如,Microsoft Windows 查看器能够打开 PNG 文件,因为操作系统默认支持作为安装的一部分。
阅读更多其他支持的合并格式
使用 Java,可以轻松合并不同的格式,包括。