我正在开发一个Web项目,该项目涉及动态生成的美国地图,该地图基于一组数据着色不同的状态.
这个SVG文件给了我一张很好的美国空白地图,很容易改变每个州的颜色.困难在于IE浏览器不支持SVG,所以为了让我使用svg提供的方便语法,我需要将它转换为JPG.
理想情况下,我只想使用GD2库,但也可以使用ImageMagick.我完全不知道如何做到这一点.
将考虑允许我动态改变美国地图上的状态颜色的任何解决方案.关键是它可以很容易地改变颜色并且它是跨浏览器的.请给我PHP/Apache解决方案.
我尝试将svg转换为PNG.svg文件来自服务器Inputstream.
首先,我将svg流转换为字节数组:
byte[] streamBytes = IOUtils.toByteArray(svgStream);
Run Code Online (Sandbox Code Playgroud)
然后我OutputStream用以下代码将字节转换为(PNG).
private ByteArrayOutputStream svgToPng(byte[] streamBytes)
throws TranscoderException, IOException {
PNGTranscoder t = new PNGTranscoder();
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(streamBytes));
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(ostream);
t.transcode(input, output);
ostream.flush();
// ostream.close();
return ostream;
}
Run Code Online (Sandbox Code Playgroud)
但我通过" t.transcode(input, output);" 得到空指针异常
org.apache.batik.transcoder.TranscoderException: null
Enclosed Exception:
Premature end of file.
graphdata : null
at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:136)
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
Run Code Online (Sandbox Code Playgroud)
注意:如果我将svgstream保存在磁盘上并使用以下transcoderinput和uri构造函数,那么它可以工作.但在我的情况下,我不想保存在磁盘上.
TranscoderInput input = new TranscoderInput(new File("c:/a.svg").toURI().toString());
Run Code Online (Sandbox Code Playgroud) 是否有一些Java库用于在代码中将.svg转换为.png或.jpg?有没有人有这方面的经验?