使用 Java API 解决方案裁剪 EPS
Contents
[
Hide
Show
]概述
本文讲解如何使用 Java 裁剪 EPS 文件。内容涵盖以下主题:
Java 裁剪 EPS 文件说明
裁剪图像的操作会改变图像的一个或多个边距:左、右、上、下。换句话说,它会从边缘剪切图像的内容。 因此,最终的 EPS 图像大小将始终小于原始图像。由于我们的解决方案不处理内容,而是处理 EPS 文件的头文件,因此文件的体积不会改变。 EPS 图像的边距由 BoundingBox 元数据定义。为了进行裁剪,或者说设置新的边界框,我们需要知道 EPS 图像现有的边界框。 否则,我们可能会将新边界框的值赋给现有边界框之外,从而导致错误。 因此,在 Java 中裁剪 EPS 图像的步骤如下:
- 使用包含 EPS 文件的输入流初始化 PsDocument 对象。
- 使用静态方法 extractEpsBoundingBox 提取图像的现有边界框。
- 为生成的 EPS 文件创建输出流。
- 创建新的边界框。
- 使用静态方法 cropEps 裁剪带有新边界框的 PsDocument 对象。
您可以通过免费在线Crop EPS检查 Aspose.Page EPS Crop 的质量并查看结果,然后使用我们的EPS Viewer Web 应用程序查看生成的 EPS 文件。
在 Java 中裁剪 EPS
在以下 Java 代码片段中 我们获取图像的现有边界框并裁剪 EPS:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
2
3// The path to the documents directory.
4String dataDir = getDataDir();
5
6//Create an input stream for EPS file
7FileInputStream inputEpsStream = new FileInputStream(dataDir + "input.eps");
8
9//Initialize PsDocument object with input stream
10PsDocument doc = new PsDocument(inputEpsStream);
11
12//Get initial bounding box of EPS image
13int [] initialBoundingBox = doc.extractEpsBoundingBox();
14
15//Create output stream for PostScript document
16FileOutputStream outputEpsStream = new FileOutputStream(dataDir + "output_crop.eps");
17
18//Create new bounding box
19//Bounding box is represented by 4 numbers: x0, y0, x, y, where x0 - left margin, y0 - top margin, x - (x0 + width), y - (y0 + height)
20float[] newBoundingBox = new float[] { 260, 300, 480, 432 };
21
22//Crop EPS image and save to the output stream
23//Croping of image is changing of its bounding box so that new values of bounding box will be within initial bounding box, that is
24//initialBoundingBox[0] <= newBoundingBox[0] <= initialBoundingBox[2]
25//initialBoundingBox[1] <= newBoundingBox[1] <= initialBoundingBox[3]
26//initialBoundingBox[0] <= newBoundingBox[2] <= initialBoundingBox[2]
27//initialBoundingBox[1] <= newBoundingBox[3] <= initialBoundingBox[3]
28doc.cropEps(outputEpsStream, newBoundingBox);
请参阅使用 .NET 和 C++ 裁剪 EPS。
初始图像
裁剪后的图像
在我们的裁剪 EPS 网页应用程序上在线评估裁剪 EPS 的效果。您可以在几秒钟内裁剪 EPS 文件并下载结果。
您可以从 GitHub 下载示例和数据文件。