我有一个图像文件(jpg 等)和一些 svg 绘图(从站点复制的 svg 标签,作为 Java 字符串)。 svg 绘图与图像文件具有相同的分辨率。我想将 svg 绘图放在图像之上并将其另存为一个文件。我并不引以为豪但行之有效的方法是:
- 使用 Batik 的 JPEGTranscoder 将 svg 转码为具有此 svg 绘图和白色背景的图像,保存此图像
- 通过对每个像素执行低级操作,将带有 svg 绘图的图像放在我的图像文件之上
我希望能够一步将 svg 绘图放在我的图像之上。
最佳答案
使用 SVG 模式可以解决您的问题。
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="200" height="200">
<defs>
<pattern id="image" x="0" y="0" patternUnits="userSpaceOnUse" height="200" width="200">
<image x="0" y="0" width="200" height="200"
xlink:href="http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png"/>
</pattern>
</defs>
<rect width="200" height="200" fill="url(#image)"/>
<circle cx="100" cy="100" r="50"/>
</svg>
fiddle 可用 here .
我通过 batik 光栅化器拉取了上面的 SVG,它被正确地光栅化了。
更新
如评论中所述,您也可以直接将图像包含在 SVG 中,而无需使用图案。
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="200" height="200">
<image x="0" y="0" width="200" height="200"
xlink:href="http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png"/>
<circle cx="100" cy="100" r="50"/>
</svg>
fiddle 可用 here .
关于java - Batik - 将 SVG 置于图像之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24413133/