从带有元素的另一个svg图像创建带有嵌入的base64字符串的SVG图像

我有一个SVG文件,其中包含几个元素(例如路径,圆形,矩形等)。

我想将该SVG文件转换为具有嵌入式base64数据而不是多个元素的SVG。蜡染有可能做到这一点吗?

我正在从事的项目仅需要使用Java库。

回答:

我曾经使用一种技术将SVG图像嵌入Blogger帖子中,这可能对此有用。基本上,这是一个两步过程:

  1. 您可以序列化要嵌入的SVG,并对其进行URL编码。
  2. 然后,您可以在SVG use元素的xlink:href属性中将URL编码的字符串用作数据URI。

这是我与蜡染纸一起测试过的有效示例。假设您要嵌入以下SVG文档circle.svg:

<?xml version="1.0" standalone="no"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"

"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="4in" height="4in" id="the_svg"

viewBox="0 0 4 4" version="1.1"

xmlns="http://www.w3.org/2000/svg">

<circle cx="1" cy="1" r="1" fill="blue" stroke="none" id="the_circle"/>

</svg>

您可以通过将其路径传递到以下小型Rhino脚本进行URL编码:

#!/usr/bin/env rhino

print(escape(readFile(arguments[0])))

当然,如果要使用Java以编程方式执行此操作,则需要Java特定的方法来序列化SVG文档并对该字符串进行URL编码。

这使您可以将文档作为URL编码的字符串:

%3C%3Fxml%20version%3D%221.0%22%20standalone%3D%22no%22%3F%3E%0A%3C%21DOCTYPE%20svg%20PUBLIC%20%22-//W3C//DTD%20SVG%201.1//EN%22%20%0A%20%20%22http%3A//www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22%3E%0A%3Csvg%20width%3D%224in%22%20height%3D%224in%22%20id%3D%22the_svg%22%0A%20%20%20%20%20viewBox%3D%220%200%204%204%22%20version%3D%221.1%22%0A%20%20%20%20%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%3E%0A%09%3Ccircle%20cx%3D%221%22%20cy%3D%221%22%20r%3D%221%22%20fill%3D%22blue%22%20stroke%3D%22none%22%20id%3D%22the_circle%22/%3E%0A%3C/svg%3E%0A%0A

然后,您可以通过在数据URI中使用它来嵌入此文档,如下所示:

data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20standalone%3D%22no%22%3F%3E%0A%3C%21DOCTYPE%20svg%20PUBLIC%20%22-//W3C//DTD%20SVG%201.1//EN%22%20%0A%20%20%22http%3A//www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22%3E%0A%3Csvg%20width%3D%224in%22%20height%3D%224in%22%20id%3D%22the_svg%22%0A%20%20%20%20%20viewBox%3D%220%200%204%204%22%20version%3D%221.1%22%0A%20%20%20%20%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%3E%0A%09%3Ccircle%20cx%3D%221%22%20cy%3D%221%22%20r%3D%221%22%20fill%3D%22blue%22%20stroke%3D%22none%22%20id%3D%22the_circle%22/%3E%0A%3C/svg%3E%0A%0A

例如,以下HTML文档使用对象标记和数据URI嵌入SVG文档:

<html>

<head>

</head>

<body>

<object data="data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20standalone%3D%22no%22%3F%3E%0A%3C%21DOCTYPE%20svg%20PUBLIC%20%22-//W3C//DTD%20SVG%201.1//EN%22%20%0A%20%20%22http%3A//www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22%3E%0A%3Csvg%20width%3D%224in%22%20height%3D%224in%22%20id%3D%22the_svg%22%0A%20%20%20%20%20viewBox%3D%220%200%204%204%22%20version%3D%221.1%22%0A%20%20%20%20%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%3E%0A%09%3Ccircle%20cx%3D%221%22%20cy%3D%221%22%20r%3D%221%22%20fill%3D%22blue%22%20stroke%3D%22none%22%20id%3D%22the_circle%22/%3E%0A%3C/svg%3E%0A%0A" width="400" height="400"></object>

</body>

</html>

您可以使用SVG’use’元素的xlink:href属性执行相同的操作,但需注意:引用完整文档是非法的。相反,您需要按其ID引用文档中的元素,该元素将被深层克隆到SVG宿主文档中。在此示例中,SVG文档根元素的ID为“

the_svg”(请注意URI末尾的hash标签)。

<?xml version="1.0" standalone="no"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"

"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="4in" height="4in" id="the_svg"

viewBox="0 0 4 4" version="1.1"

xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

<use x="0" y="0" width="4" height="4" xlink:href="data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20standalone%3D%22no%22%3F%3E%0A%3C%21DOCTYPE%20svg%20PUBLIC%20%22-//W3C//DTD%20SVG%201.1//EN%22%20%0A%20%20%22http%3A//www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22%3E%0A%3Csvg%20width%3D%224in%22%20height%3D%224in%22%20id%3D%22the_svg%22%0A%20%20%20%20%20viewBox%3D%220%200%204%204%22%20version%3D%221.1%22%0A%20%20%20%20%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%3E%0A%09%3Ccircle%20cx%3D%221%22%20cy%3D%221%22%20r%3D%221%22%20fill%3D%22blue%22%20stroke%3D%22none%22%20id%3D%22the_circle%22/%3E%0A%3C/svg%3E%0A%0A#the_svg"/>

</svg>

仅供参考,这在Batik 1.7(在Squiggle浏览器中测试)中效果很好,但不适用于Chromium或Firefox。

以上是 从带有元素的另一个svg图像创建带有嵌入的base64字符串的SVG图像 的全部内容, 来源链接: utcz.com/qa/400304.html

回到顶部