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

Unable to convert a SVG image to PNG using batik rasterizer

我正在尝试使用 batik 光栅器 (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)

感谢任何帮助。

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