在这个简短的教程中,您将学习如何在 Java 中调整 SVG 图像的大小。它提供了详细的步骤、可运行的示例代码以及环境配置步骤。您可以在一些 API 调用的帮助下在 Java 中调整 SVG 的大小,也可以自定义输出 SVG。
在 Java 中调整 SVG 图像大小的步骤
- 设置您的应用程序以使用存储库中的 Aspose.Imaging
- 将源 SVG 文件加载到要在 Java 中调整大小的 Image 类对象中
- 调用 Image.resize() 方法,使用新的大小和调整大小类型为 CenterToCenter
- 保存根据要求调整大小的输出 SVG
这些步骤旨在更改 Java 中 SVG 图像的大小。配置细节和编程步骤顺序是共享的,可以在任何支持 Java 的环境中遵循。您还可以在将调整大小的 SVG 图像保存到磁盘之前对其进行自定义。
在 Java 中调整 SVG 文件大小的代码
import com.aspose.imaging.Image; | |
import com.aspose.imaging.License; | |
import com.aspose.imaging.ResizeType; | |
public class ResizeSvgImageInJava { | |
public static void main(String[] args) // Main function to resize SVG image in Java | |
{ | |
// Instantiate a license to avoid watermark in the output resized SVG file | |
License licForSvgResizing = new License(); | |
licForSvgResizing.setLicense("Aspose.Imaging.lic"); | |
// Load the source SVG image file that needs to be resized | |
Image img = Image.load("Circle.svg"); | |
// Set the new size of the SVG image along with the resize type | |
img.resize(img.getWidth() * 2, img.getHeight() * 2, ResizeType.CenterToCenter); | |
// Save the resized SVG image on the disk | |
img.save("ResizedSvg.svg"); | |
System.out.println("Done"); | |
} | |
} |
您可以使用 Image.Save() 函数中的 SvgOptions 类对象,通过调用 setColorType()、setCompress()、setTextAsShapes() 等一种或多种方法来自定义调整大小的 SVG。同样,您可以使用 ISvgResourceKeeperCallback getCallback() 来获取或设置字体存储选项,
在这个快速教程中,我们学习了在 Java 中增加 SVG 图像的大小。如果要更改其他类型的图像大小,请参阅 如何使用Java调整图像大小 上的文章。