2014-11-20 83 views
0

在参考this postJava和SVG使用img标签

我创建一个图表引擎,我所拥有的一切设置,其中一个GET请求返回SVG。我使用JFreeSVG(与蜡染非常相似)。我可以使用iframe和对象HTML标记呈现SVG。我想用一个img标签来创建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 
xmlns="http://www.w3.org/2000/svg" 
xmlns:xlink="http://www.w3.org/1999/xlink" 
xmlns:jfreesvg="http://www.jfree.org/jfreesvg/svg" width="423" height="452" text-rendering="auto" shape-rendering="auto"> 
<rect x="0" y="0" width="423" height="452" style="fill: rgb(255,255,255); fill-opacity: 1.0" transform="matrix(1,0,0,1,0,0)" clip-path="url(#4989619974245clip-0)"/> 
<g transform="matrix(1,0,0,1,0,0)"> 
    <text x="8" y="446.14" style="fill: rgb(0,0,0); fill-opacity: 1.0; font-family: Tahoma; font-size: 9px; " clip-path="url(#4989619974245clip-0)">November 19, 2014</text> 
<g style="fill: url(#4989619974245gp0); fill-opacity: 1.0; stroke: none" transform="matrix(1,0,0,1,0,0)" clip-path="url(#4989619974245clip-1)"> 
<rect x="5" y="8" width="386.62" height="412.27" style="stroke-width: 1.0;stroke: rgb(204,204,204);stroke-opacity: 1.0;; fill: none" transform="matrix(1,0,0,1,0,0)" clip-path="url(#4989619974245clip-0)"/> 
</svg> 

回答

0

最好的解决方法是通过使用URI图像为Base64。 Base64是期望的,因为其与浏览器兼容性(IE9)

 //g2.getSVGEleemnt returns a SVG in string eg. <svg> ... </svg> 
     String imageString = g2.getSVGElement(); 
     //encode it in Base64 
     byte[] imageData = Base64.encodeBase64(imageString.getBytes()); 
     //indicate mime type && base64 
     String header = "data:image/svg+xml;base64,"; 
     byte[] headerData = header.getBytes(); 

的一旦你,你必须流的顺序的标题,然后该图像数据。把你的数据或你的电话,获取数据在img标签和中提琴!

<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaH........">