简体   繁体   English

蜡染-将SVG放在图像顶部

[英]Batik - put SVG on top of image

I have an image file (jpg, etc.) and some svg drawings (svg tag copied from the site, as Java String). 我有一个图像文件(jpg等)和一些svg绘图(从站点复制的svg标记,如Java String)。 The svg drawing is of the same resolution as the image file. svg绘图与图像文件具有相同的分辨率。 I want to put svg drawings on top of the image and save it as one file. 我想将svg绘图放在图像上方,并将其另存为一个文件。 My approach, of which I'm not proud of, but works, is to: 我的方法引以为傲,但我并不为此感到骄傲,但可以起作用:

  • use Batik's JPEGTranscoder to transcode svg into image with this svg drawings and white background, save this image 使用蜡染的JPEGTranscoder将svg转换为具有该svg图纸和白色背景的图像,然后保存该图像
  • put the image with svg drawings on top of my image file by perfoming low level operations on each pixel 通过对每个像素执行低级操作,将带有svg图纸的图像放在我的图像文件顶部

I would like to be able to put the svg drawings on top of my image in one step. 我希望能够一步将svg图纸放在图像上。

Using an SVG pattern would solve your problem. 使用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 available here . 小提琴在这里提供

I pulled the SVG above through the batik rasterizer, and it was correctly rasterized. 我通过蜡染光栅化器将SVG拉到上方,并且光栅化正确。

Update 更新资料
As noted in the comments, you could just as well include the image directly in your SVG, without the use of a pattern. 如评论中所述,您也可以将图像直接包含在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 available here . 小提琴在这里提供

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM