通过 Java 转到 GIF
构建您自己的 Java 应用程序以使用服务器端 API 合并 GIF 文件。
如何使用 Java 合并 GIF 文件
如果您想让您的创造力尽情发挥,那么现在就是使用图像拼贴的时候了。即使源文件格式不同,此功能也允许您合并照片和图像。吸引人们注意您的作品集的一个好方法是使用带有重复图像或图案的照片壁纸。在这种情况下,可以在水平和垂直方向上组合图像。如果您需要演示处理文件的结果,您可以轻松合并两个图像:应用照片效果之前和之后。为了合并 GIF 文件,我们将使用 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 合并 GIF 的步骤
你需要 aspose-imaging-version-jdk16.jar 在您自己的环境中尝试以下工作流程。
- 使用 Image.load 方法加载 GIF 文件
- 将图像组合成新的图像
- 将裁剪后的图像以 Aspose.Imaging 支持的格式保存到光盘
系统要求
所有主要操作系统都支持 Java 的 Aspose.Imaging。只需确保您具有以下先决条件。
- 已安装 JDK 1.6 或更高版本。
合并 GIF 图像 - Java
import com.aspose.imaging.IMultipageImage; | |
import com.aspose.imaging.Image; | |
import com.aspose.imaging.RasterImage; | |
import com.aspose.imaging.VectorImage; | |
import com.aspose.imaging.fileformats.tiff.TiffFrame; | |
import com.aspose.imaging.fileformats.tiff.TiffImage; | |
import com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat; | |
import com.aspose.imaging.imageoptions.PdfOptions; | |
import com.aspose.imaging.imageoptions.TiffOptions; | |
import java.io.File; | |
import java.io.FilenameFilter; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
// Test directory should have source images to merge | |
String TestDirectory = "D:\\Many2One\\"; | |
// Source file mask, could vary depending on image format (i.e. *.png, *.jpg, *.dicom, *., *.tiff, *.gif etc) | |
final String fileMask = ".png"; | |
List<Image> singles = new ArrayList<Image>(); | |
List<Image> images = new ArrayList<Image>(); | |
try | |
{ | |
int maxWidth = 0; | |
int maxHeight = 0; | |
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 file : files) | |
{ | |
Image image = Image.load(file.getAbsolutePath()); | |
if (image.getWidth() > maxWidth) | |
{ | |
maxWidth = image.getWidth(); | |
} | |
if (image.getHeight() > maxHeight) | |
{ | |
maxHeight = image.getHeight(); | |
} | |
if (image instanceof VectorImage) | |
{ | |
throw new UnsupportedOperationException("Vector images is not supported by these code snippet. Please look at vector to booklet example to get the implementation idea."); | |
} | |
if (image instanceof IMultipageImage) | |
{ | |
Collections.addAll(singles, ((IMultipageImage) image).getPages()); | |
} | |
else | |
{ | |
singles.add(image); | |
} | |
images.add(image); | |
} | |
if (singles.isEmpty()) | |
{ | |
return; | |
} | |
String outputPath1 = TestDirectory + "multiframe.tiff"; | |
String outputPath2 = TestDirectory + "multipage.pdf"; | |
try (TiffImage tiffImage = new TiffImage(new TiffFrame((RasterImage) singles.get(0)))) | |
{ | |
for (int i = 1; i < singles.size(); i++) | |
{ | |
tiffImage.addPage((RasterImage) singles.get(i)); | |
} | |
// Save as muti frame tiff | |
tiffImage.save(outputPath1, new TiffOptions(TiffExpectedFormat.TiffJpegRgb)); | |
// Worry about Pdf ? That's it! | |
tiffImage.save(outputPath2, new PdfOptions()); | |
} | |
} | |
finally | |
{ | |
singles.clear(); | |
for (Image image : images) | |
{ | |
image.close(); | |
} | |
images.clear(); | |
} |
关于 Java API 的 Aspose.Imaging
Aspose.Imaging API 是一种图像处理解决方案,用于在应用程序中创建、修改、绘制或转换图像(照片)。它提供:跨平台的图像处理,包括但不限于各种图像格式之间的转换(包括统一的多页或多帧图像处理)、绘图等修改、使用图形基元、转换(调整大小、裁剪、翻转和旋转) 、二值化、灰度、调整)、高级图像处理功能(过滤、抖动、遮罩、去偏斜)和内存优化策略。它是一个独立的库,不依赖任何软件进行图像操作。可以在项目中使用原生 API 轻松添加高性能图像转换功能。这些是 100% 私有的本地 API,图像在您的服务器上处理。通过在线应用合并 GIF
通过访问我们的 Live Demos 网站 合并 GIF 文档。 现场演示有以下好处
GIF 什么是 GIF 文件格式
GIF 或图形交换格式是一种高度压缩的图像。 GIF 由 Unisys 拥有,使用不会降低图像质量的 LZW 压缩算法。对于每个图像,GIF 通常允许每个像素最多 8 位,并且整个图像最多允许 256 种颜色。与 JPEG 图像相比,JPEG 图像可以显示多达 1600 万种颜色,并且相当接近人眼的极限。早在互联网出现时,GIF 仍然是最佳选择,因为它们需要低带宽并且与消耗纯色区域的图形兼容。动画 GIF 将大量图像或帧组合到一个文件中,并按顺序显示它们以生成动画剪辑或短视频。每帧的颜色限制最多为 256 种,并且可能最不适合再现具有颜色渐变的其他图像和照片。
阅读更多其他支持的合并格式
使用 Java,可以轻松合并不同的格式,包括。