用 Java 将 SVG 转换为 JPG

在本文中,您将学习如何使用 Aspose.HTML for Java 将 SVG 转换为 JPG,以及如何应用 ImageSaveOptions。您可以轻松使用 Java 示例将 SVG 转换为 JPG,此处详细介绍了将 SVG 转换为 PNG、 BMP、GIF 和 TIFF 图像的方法。只需在 ImageSaveOptions 中设置所需的 ImageFormat 即可!

将 SVG 转换为任何受支持的图像格式都必须遵循以下步骤:

要指定输出图像格式,请使用 ImageSaveOptions 类。ImageSaveOptions(format) 构造函数用指定的格式初始化选项对象。你可以将图像格式设置为JPGPNGBMPTIFFGIF。默认的 ImageFormat为 PNG。

用几行代码将 SVG 转换为 JPG

将 SVG 转换为 JPG 可生成光栅图像,便于共享、查看或通过电子邮件发送。 Converter 类的静态方法主要用于将 SVG 代码转换为各种格式的最简单方法。只需几行代码,您就可以在 Java 应用程序中将 SVG 转换为 JPG!

在示例中,我们使用了 Converter 类中的convertSVG(content,baseUri,options,outputPath)方法,该方法需要四个参数:包含要转换的 SVG 代码的字符串、输入 SVG 文件的基本文件夹、ImageSaveOptions 类的实例以及保存转换后图像的输出文件路径:

1// Convert SVG to JPG in a few lines using Java
2
3// Prepare SVG code
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";
7
8// Invoke the convertSVG() method to convert SVG to image
9Converter.convertSVG(code, ".", new ImageSaveOptions(ImageFormat.Jpeg), "output.jpg");

将 SVG 转换为 JPG

SVG 文件是网站图形的最佳选择,但并非所有网络浏览器都支持 SVG 文件。将 SVG 转换为 JPG 可确保图像在任何网络浏览器上正常加载,从而获得更好的用户体验。此外,JPG 图像易于共享、通过电子邮件发送、嵌入报告或演示文稿等。在下面的 Java 示例中,我们将逐步说明如何使用默认保存选项将 SVG 转换为 JPG:

  1. 加载 SVG 文件。您可以从文件、SVG 代码或 URL 加载 SVG。在下面的示例中,我们准备好 SVG 代码,从头开始创建 SVG,并使用 SVGDocument() 构造函数初始化 SVGDocumet 实例。
  2. 使用 ImageSaveOptions() 构造函数创建一个新的 ImageSaveOptions 对象。
  3. 调用 Converter 类中的一个 convertSVG() 方法,将 SVG 保存为 JPG 图像。在示例中,我们使用了 convertSVG(document, options, outputPath) 方法。

下面的 Java 代码片段展示了如何使用 Aspose.HTML for Java 将 SVG 转换为 JPG:

 1// Convert SVG to JPG 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.Jpeg);
16
17// Convert SVG to JPG
18Converter.convertSVG(document, options, "output.jpg");

保存选项 – ImageSaveOptions 类

ImageSaveOptions 类提供的方法可让您完全控制各种参数,并改进将 SVG 转换为图像文件格式的过程。您可以指定 image formatpage sizemarginscompression levelmedia type 等。

MethodDescription
setCompression(value)Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW.
getCssGets 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.

您可以从 GitHub 下载完整的示例和数据文件。

要了解有关 ImageSaveOptions 的更多信息,请阅读 Fine-Tuning Converters 一文。

使用 ImageSaveOptions 将 SVG 转换为 JPG

例如,如果您需要将 SVG 文档渲染为 JPG 文件格式并自定义保存选项,下面的示例演示了如何简单地实现这一点:

  1. 使用 SVGDocument 类加载 SVG 文件。
  2. 创建一个新的 ImageSaveOptions 对象,并指定所需的保存选项。在下面的示例中,我们为生成的 JPG 图像应用自定义页面大小、设置平滑模式和背景颜色:
    • Use methods of the Page class to configure the output page.
    • 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.
  3. 使用 Converter 类的 convertSVG(sourcePath, options, outputPath) 方法将 SVG 保存为 JPG 图像。
 1// Convert SVG to JPG in Java with custom settings
 2
 3// Prepare SVG code and save it to a file
 4String code =
 5        "<svg width='450' height='450' xmlns='http://www.w3.org/2000/svg'>" +
 6                "    <g fill='RoyalBlue'>" +
 7                "        <rect x='100' y='100' rx='25' ry='25' width='200' height='56' />" +
 8                "        <rect x='100' y='100' rx='25' ry='25' width='200' height='56' transform ='rotate(90 200 128)' />" +
 9                "        <rect x='100' y='100' rx='25' ry='25' width='200' height='56' transform ='rotate(-45 200 128)' />" +
10                "        <rect x='100' y='100' rx='25' ry='25' width='200' height='56' transform ='rotate(45 200 128)' />" +
11                "    </g>" +
12                "    <circle cx='200' cy='128' r='28' stroke='pink' stroke-width='50'  stroke-dasharray='3 13' fill='Orange' />" +
13                "    <circle cx='200' cy='128' r='5' />" +
14                "</svg>";
15
16try (java.io.FileWriter fileWriter = new java.io.FileWriter("flower.svg")) {
17    fileWriter.write(code);
18}
19
20// Initialize ImageSaveOptions and set up smoothing mode, page size, and background color
21ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
22PageSetup pageSetup = new PageSetup();
23options.setSmoothingMode(SmoothingMode.HighQuality);
24Page anyPage = new Page();
25anyPage.setSize(new Size(Length.fromPixels(200), Length.fromPixels(200)));
26pageSetup.setAnyPage(anyPage);
27options.setPageSetup(pageSetup);
28options.setBackgroundColor(Color.getAliceBlue());
29
30// Call the convertSVG() method to convert the "flower.svg" file to a JPEG image
31Converter.convertSVG("flower.svg", options, "flower.jpg");

使用我们的在线 SVG 到 JPG 转换器 查看 SVG 到 JPG 转换的质量。上传、转换文件并在几秒钟内获得结果。现在就免费试用我们强大的 SVG 转 JPG 转换器吧!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.