用 Java 将 SVG 转换为 PNG
每种图像格式都有其独特的优缺点。SVG 非常适合创建可放大或缩小而不会降低质量的图形。但有时,你需要将 SVG 文件转换为其他图像格式,如 PNG、JPG、BMP、TIFF 或 GIF。总的来说,SVG 转换对开发人员和设计师很有帮助,因为他们必须为各种目的使用不同的图像格式。此外,了解如何在不同格式之间进行转换还有助于确保更多的受众可以访问和使用你的图形和图像。
Aspose.HTML for Java 库提供了多种将 SVG 转换为图像的功能,如JPG、PNG、BMP、TIFF和GIF。将 SVG 转换为任何受支持的图像格式都必须遵循以下步骤:
- 打开 SVG 文件
- 创建 ImageSaveOptions 对象并指定图像格式。
- 使用 Converter 类将 SVG 转换为所选图像格式。
要指定输出图像格式,请使用
ImageSaveOptions 类。ImageSaveOptions(format) 构造函数用指定的格式初始化选项对象。可以将图像格式设置为 JPG、PNG、BMP、GIF 或 TIFF。默认的 ImageFormat 是 PNG。
在本文中,您将学习如何使用 Aspose.HTML for Java 将 SVG 转换为 PNG,以及如何应用
ImageSaveOptions。您可以轻松使用 Java 示例将 SVG 转换为 PNG,此处详细介绍了将
SVG 转换为 JPG、 BMP、GIF 和 TIFF 图像的方法。只需在 ImageSaveOptions 中设置所需的 ImageFormat 即可!
只需一行代码即可将 SVG 转换为 PNG
通过将 SVG 转换为 PNG,您可以获得一种光栅图像,这种图像可以方便地共享、查看、通过电子邮件发送,并且可以在不降低质量的情况下进行压缩。 Converter 类的静态方法主要用于将 SVG 代码转换为各种格式的最简单方法。只需一行代码,您就可以在 Java 应用程序中将 SVG 转换为 PNG!
1// Convert SVG to PNG in one line using Java
2
3// Invoke the convertSVG() method for SVG to PNG conversion
4Converter.convertSVG("shapes.svg", new ImageSaveOptions(ImageFormat.Png), "convert-with-single-line.png");将 SVG 转换为 PNG
在下面的 Java 示例中,我们将逐步说明如何使用默认保存选项将 SVG 转换为 PNG:
- 加载 SVG 文件。您可以从文件、SVG 代码或 URL 加载 SVG。在下面的示例中,我们准备了 SVG 代码,从头开始创建 SVG,并将其直接传递给
convertSVG()方法。 - 使用 ImageSaveOptions() 构造函数创建一个新的 ImageSaveOptions 对象。默认情况下,格式属性为 PNG。
- 调用
Converter 类中的一个
convertSVG()方法将 SVG 保存为 PNG 图像。在示例中,我们使用了 convertSVG(document,options,outputPath) 方法,该方法需要三个参数:源 SVG 文档、ImageSaveOptions 类实例和保存转换后图像的输出文件路径。
下面的 Java 代码片段展示了如何使用 Aspose.HTML for Java 将 SVG 转换为 PNG。
1// Convert SVG to PNG using Java
2
3// Prepare SVG code and save it to a file
4String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
5 "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
6 "</svg>\n";
7try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
8 fileWriter.write(code);
9}
10
11// Initialize an SVG document from the SVG file
12SVGDocument document = new SVGDocument("document.svg");
13
14// Initialize ImageSaveOptions
15ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
16
17// Convert SVG to PNG
18Converter.convertSVG(document, options, "output.png");您可以从 GitHub 下载完整的示例和数据文件。
保存选项 – ImageSaveOptions 类
ImageSaveOptions 类提供的方法可让您完全控制各种参数,并改进将 SVG 转换为图像文件格式的过程。您可以指定 image format、 page size、 margins、 compression level、 media type 等。
| Method | Description |
|---|---|
| setCompression(value) | Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW. |
| getCss | Gets a CssOptions object which is used for configuration of CSS properties processing. |
| setFormat(value) | Sets ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default this property is ImageFormat.Png. |
| setBackgroundColor(value) | Sets Color which will fill background of every page. Default value is Color.Transparent(Color.getTransparent()). |
| setPageSetup(value) | Gets a page setup object is used for configuration output page-set. |
| setHorizontalResolution(value) | Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
| setVerticalResolution(value) | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
| setSmoothingMode(value) | Sets the rendering quality for this image. |
| getText() | Gets a TextOptions object which is used for configuration of text rendering. |
使用 ImageSaveOptions 将 SVG 转换为 PNG
例如,如果您需要将 SVG 文档渲染为 PNG 文件格式并自定义保存选项,下面的示例演示了如何简单地实现这一点:
- 使用 SVGDocument 类加载 SVG 文件。
- 创建一个新的
ImageSaveOptions 对象,并指定所需的保存选项。在下面的示例中,我们将应用自定义分辨率,为生成的 PNG 图像设置平滑模式和背景颜色:
- Use the
setHorizontalResolution()andsetVerticalResolution()methods to set the horizontal and vertical resolution of the image to 200. - Use the
setBackgroundColor()method to set the background color for every page. - Use the
setSmoothingMode()method to set the quality of the image smoothing to high.
- Use the
- 使用
Converter 类的
convertSVG(document, options, savePath)方法将 SVG 保存为 PNG 图像。
1// Convert SVG to PNG in Java with custom settings
2
3// Create an instance of the ImageSaveOptions class. Set up the SmoothingMode, resolutions, and background color
4ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
5options.setHorizontalResolution(Resolution.to_Resolution(200));
6options.setVerticalResolution(Resolution.to_Resolution(200));
7options.setBackgroundColor(Color.getAliceBlue());
8options.setSmoothingMode(SmoothingMode.HighQuality);
9
10// Initialize an SVG document from a file
11final SVGDocument document = new SVGDocument("flower1.svg");
12
13// Convert SVG to PNG
14Converter.convertSVG(document, options, "flower-options.png");结论
Aspose.HTML for Java 库为将 SVG 转换为各种光栅图像格式(包括 PNG、JPG、BMP、TIFF 和 GIF)提供了强大而高效的解决方案。使用 Converter和 ImageSaveOptions类可以转换 SVG 并精确定制图像格式、分辨率、压缩率和背景颜色等输出属性。这种控制水平可以优化输出质量,并根据特定项目的需要调整结果,确保转换后的图像既符合功能要求,又符合美学要求。
只要按照技术步骤–加载 SVG 文档、配置 ImageSaveOptions和调用 convertSVG() 方法–就能实现简单、快速和高质量的格式转换。
Aspose.HTML 提供一款免费的在线 SVG 到 PNG 转换器,可将 SVG 转换为 PNG,而且质量高、简单快捷。该工具可帮助您创建与各种平台和设备兼容的高质量图像和图形。为什么不试试呢?只需上传、转换文件,几秒钟就能得到结果!
