我尝试将svg转换为PNG。svg文档以Inputstream的形式来自服务器。
首先,我使用以下命令将svg流转换为字节数组:
byte[] streamBytes = IOUtils.toByteArray(svgStream);然后,我使用以下代码将字节转换为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;
}但是我收到了"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)注意:如果我将svgstream保存在磁盘上,并使用下面的代码转换输入和uri构造函数,那么它就可以工作了。但在我的例子中,我不想保存在磁盘上。
TranscoderInput input = new TranscoderInput(new File("c:/a.svg").toURI().toString());发布于 2011-11-18 18:31:47
我找到问题了。
我每次都检查svgstream是否正常。为了查看它是否正常,我每次都创建一个SVG文件,其中的代码包含在我的注释中。从逻辑上讲,它消耗了流。最后没有真正的流。它导致了异常。感谢所有人..
https://stackoverflow.com/questions/8167977
复制相似问题