无法使用蜡染光栅化器将SVG图像转换为PNG

时间:2015-12-07 17:50:00

标签: java xml svg batik

我正在尝试使用batik rasterizer(batik-transcoder 1.8)库将图像从SVG转换为PNG,但即使是简单的转换也会引发异常。这是一个不起作用的示例代码:

private static void convertSVGToImage() throws TranscoderException, IOException {
    String svg_URI_input = Paths.get("caution.svg").toUri().toURL().toString();
    TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);
    OutputStream png_ostream = new FileOutputStream("caution.png");
    TranscoderOutput output_png_image = new TranscoderOutput(png_ostream);
    PNGTranscoder my_converter = new PNGTranscoder();
    my_converter.transcode(input_svg_image, output_png_image);
    png_ostream.flush();
    png_ostream.close();
}

caution.svg是这个文件:http://dev.w3.org/SVG/tools/svgweb/samples/svg-files/caution.svg

堆栈跟踪是:

org.xml.sax.SAXNotRecognizedException: unrecognized feature http://xml.org/sax/features/external-general-entities
    at org.gjt.xpp.sax2.Driver.setFeature(Driver.java:178)
    at org.gjt.xpp.jaxp11.SAXParserImpl.setFeatures(SAXParserImpl.java:149)
    at org.gjt.xpp.jaxp11.SAXParserImpl.<init>(SAXParserImpl.java:132)
    at org.gjt.xpp.jaxp11.SAXParserFactoryImpl.newSAXParserImpl(SAXParserFactoryImpl.java:114)
    at org.gjt.xpp.jaxp11.SAXParserFactoryImpl.setFeature(SAXParserFactoryImpl.java:142)
    at org.apache.batik.dom.util.SAXDocumentFactory.<clinit>(Unknown Source)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.createDocumentFactory(Unknown Source)
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)
    at br.com.supera.Main.convertSVGToImage(Main.java:45)
    at br.com.supera.Main.main(Main.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Exception in thread "main" org.apache.batik.transcoder.TranscoderException: null
Enclosed Exception:
not supported setting property http://xml.org/sax/properties/lexical-handler
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)
    at br.com.supera.Main.convertSVGToImage(Main.java:45)
    at br.com.supera.Main.main(Main.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我只是简单地使用batik 1.7.1来解决它。