使用apache蜡染加载,更新和转码svg文档时出现奇怪的CSS异常

时间:2013-03-13 11:56:14

标签: java svg java-2d batik

我正在通过以下代码行将svg文件加载到我的应用程序中

                                     parser = XMLResourceDescriptor.getXMLParserClassName();

                                     factory = new SAXSVGDocumentFactory(parser);

                                    // ua = new UserAgentAdapter();
                                    // loader = new DocumentLoader(ua);
                                    // ctx = new BridgeContext(ua, loader);
                                    // ctx.setDynamicState(BridgeContext.DYNAMIC);

                                    doc = factory.createSVGDocument(svg.toURI().toString());
                                    generator = new SVGGraphics2D(doc);

以前使用SVG生成器生成文档,它可以在所有SVG浏览器中查看,并且不包含任何错误。

现在我正在更新文件

//在这里执行画布和图像本身的高度/宽度计算                 尺寸d = calculateImageSize(true,true);

            System.out.println(" 400 DPI Original width " + d.getWidth()
                            + " height " + d.getHeight());

            generator.setSVGCanvasSize(d);

            Element root = doc.getDocumentElement();

            //root.setAttributeNS(null, "viewBox", ("0 0 " + (d.getWidth() + " " + d
                    // .getHeight())));

            NodeList imageList = root.getElementsByTagName("image");

             Node imageNode = imageList.item(0);

             Element image = (Element) imageNode;

             image.setAttributeNS(null, "width", String.valueOf(d.getWidth()));
             image.setAttributeNS(null, "height", String.valueOf(d.getHeight()));

            updateCanvas();

以下是updateCanvas实现

public void updateCanvas(){

            Element root = doc.getDocumentElement();
            generator.getRoot(root);
            canvas.setSVGDocument(doc);

    }

现在,当我尝试使用以下方法保存此更新的文档时

public void savePngImage(String pngFile,boolean is400DPI)                         抛出IOException {

            OutputStream ostream = null;

            try {

                    System.gc();

                    ostream = new FileOutputStream(pngFile);

                    transCoder = new PNGTranscoder();

                    if (is400DPI) {
                            transCoder.addTranscodingHint(
                                            ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER,
                                            new Float(0.0635f));
                    } else {
                            transCoder.addTranscodingHint(
                                            ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER,
                                            new Float(0.3528f));
                    }

                    transCoder.addTranscodingHint(PNGTranscoder.KEY_INDEXED,
                                    new Integer(5));
                    transCoder.addTranscodingHint(ImageTranscoder.KEY_MEDIA, "screen");

                    TranscoderInput input = new TranscoderInput(canvas.getSVGDocument());
                    TranscoderOutput output = new TranscoderOutput(ostream);
                    // perform Transcoding
                    transCoder.transcode(input, output);

            } catch (TranscoderException te) {
                    te.printStackTrace();
                    throw new IOException(te.getMessage());
            } finally {
                    ostream.flush();
                    ostream.close();
            }
    }
每次运行

,程序抛出随机异常,

* * CSSEngine:exception property.syntax.error:org.w3c.dom.DOMException:" o"标识符不是" shape-rendering"的有效值。属性。

AttrValue:自动

* CSSEngine:exception property.syntax.error:org.w3c.dom.DOMException:""标识符不是" shape-rendering"的有效值。属性。

AttrValue:自动

例外:org.w3c.dom.DOMException中 org.w3c.dom.DOMException:""标识符不是" shape-rendering"的有效值。属性。         at org.apache.batik.css.engine.value.AbstractValueFactory.createInvalidIdentifierDOMException(AbstractValueFactory.java:50)         at org.apache.batik.css.engine.value.IdentifierManager.createValue(IdentifierManager.java:48)         在org.apache.batik.css.engine.CSSEngine $ 1.property(CSSEngine.java:744)         在org.apache.batik.css.engine.CSSEngine.getCascadedStyleMap(CSSEngine.java:768)         在org.apache.batik.css.engine.CSSEngine.getComputedStyle(CSSEngine.java:876)         在org.apache.batik.bridge.CSSUtilities.getComputedStyle(CSSUtilities.java:82)         在org.apache.batik.bridge.CSSUtilities.convertVisibility(CSSUtilities.java:579)         在org.apache.batik.bridge.SVGSVGElementBridge.createGraphicsNode(SVGSVGElementBridge.java:142)         在org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:76)         at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)         在com.mmg.app.svg.TiledImageTranscoder.transcode(TiledImageTranscoder.java:61)         at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)         at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)         在com.mmg.app.svg.ShoeComponentPNGCreator.savePngImage(ShoeComponentPNGCreator.java:601)         at com.mmg.app.svg.ShoeComponentPNGCreator.create72DPIOriginalImage(ShoeComponentPNGCreator.java:305)         在com.mmg.app.svg.ShoeComponentPNGCreator.processStyleImaging(ShoeComponentPNGCreator.java:226)         在com.mmg.app.svg.ShoeComponentPNGCreator.traverseFolder(ShoeComponentPNGCreator.java:186)         在com.mmg.app.svg.ShoeComponentPNGCreator.main(ShoeComponentPNGCreator.java:662)

* CSSEngine:异常.... org.w3c.dom.DOMException:file:/ F:/workspace/MonoGeneiousShoe/src/svg/72DPI/Style-5/Left_Style-5.svg: 属性" shape-rendering"表示无效的CSS值(" auto")。 原始信息: ""标识符不是" shape-rendering"的有效值。属性。         在org.apache.batik.css.engine.CSSEngine.getCascadedStyleMap(CSSEngine.java:784)         在org.apache.batik.css.engine.CSSEngine.getComputedStyle(CSSEngine.java:876)         在org.apache.batik.bridge.CSSUtilities.getComputedStyle(CSSUtilities.java:82)         在org.apache.batik.bridge.CSSUtilities.convertVisibility(CSSUtilities.java:579)         在org.apache.batik.bridge.SVGSVGElementBridge.createGraphicsNode(SVGSVGElementBridge.java:142)         在org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:76)         at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)         在com.mmg.app.svg.TiledImageTranscoder.transcode(TiledImageTranscoder.java:61)         at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)         at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)         在com.mmg.app.svg.ShoeComponentPNGCreator.savePngImage(ShoeComponentPNGCreator.java:601)         at com.mmg.app.svg.ShoeComponentPNGCreator.create72DPIOriginalImage(ShoeComponentPNGCreator.java:305)         在com.mmg.app.svg.ShoeComponentPNGCreator.processStyleImaging(ShoeComponentPNGCreator.java:226)         在com.mmg.app.svg.ShoeComponentPNGCreator.traverseFolder(ShoeComponentPNGCreator.java:186)         在com.mmg.app.svg.ShoeComponentPNGCreator.main(ShoeComponentPNGCreator.java:662)

例外:org.w3c.dom.DOMException中 org.w3c.dom.DOMException:" o"标识符不是" shape-rendering"的有效值。属性。         at org.apache.batik.css.engine.value.AbstractValueFactory.createInvalidIdentifierDOMException(AbstractValueFactory.java:50)         at org.apache.batik.css.engine.value.IdentifierManager.createValue(IdentifierManager.java:48)         在org.apache.batik.css.engine.CSSEngine $ 1.property(CSSEngine.java:744)         在org.apache.batik.css.engine.CSSEngine.getCascadedStyleMap(CSSEngine.java:768)         在org.apache.batik.css.engine.CSSEngine.getComputedStyle(CSSEngine.java:876)         在org.apache.batik.bridge.CSSUtilities.getComputedStyle(CSSUtilities.java:82)         在org.apache.batik.bridge.CSSUtilities.convertVisibility(CSSUtilities.java:579)         在org.apache.batik.bridge.SVGSVGElementBridge.createGraphicsNode(SVGSVGElementBridge.java:142)         在org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:76)         在org.apache.batik.swing.svg.GVTTreeBuilder.run(GVTTreeBuilder.java:96) 错误:null 附上例外: 文件:/ F:/workspace/MonoGeneiousShoe/src/svg/72DPI/Style-5/Left_Style-5.svg: 属性" shape-rendering"表示无效的CSS值(" auto")。 原始信息: ""标识符不是" shape-rendering"的有效值。属性。

* CSSEngine:异常.... org.w3c.dom.DOMException:file:/ F:/workspace/MonoGeneiousShoe/src/svg/72DPI/Style-5/Left_Style-5.svg: 属性" shape-rendering"表示无效的CSS值(" auto")。 原始信息: " o"标识符不是" shape-rendering"的有效值。属性。         在org.apache.batik.css.engine.CSSEngine.getCascadedStyleMap(CSSEngine.java:784)         在org.apache.batik.css.engine.CSSEngine.getComputedStyle(CSSEngine.java:876)         在org.apache.batik.bridge.CSSUtilities.getComputedStyle(CSSUtilities.java:82)         在org.apache.batik.bridge.CSSUtilities.convertVisibility(CSSUtilities.java:579)         在org.apache.batik.bridge.SVGSVGElementBridge.createGraphicsNode(SVGSVGElementBridge.java:142)         在org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:76)         在org.apache.batik.swing.svg.GVTTreeBuilder.run(GVTTreeBuilder.java:96) 错误:null 附上例外: 文件:/ F:/workspace/MonoGeneiousShoe/src/svg/72DPI/Style-5/Left_Style-5.svg: 属性" shape-rendering"表示无效的CSS值(" auto")。 原始信息: " o"标识符不是" shape-rendering"的有效值。属性。

* CSSEngine:exception property.syntax.error:org.w3c.dom.DOMException:" lormal"标识符不是" font-style"的有效值。属性。

AttrValue:正常

例外:org.w3c.dom.DOMException中 org.w3c.dom.DOMException:" lormal"标识符不是" font-style"的有效值。属性。         at org.apache.batik.css.engine.value.AbstractValueFactory.createInvalidIdentifierDOMException(AbstractValueFactory.java:50)         at org.apache.batik.css.engine.value.IdentifierManager.createValue(IdentifierManager.java:48)         在org.apache.batik.css.engine.CSSEngine $ 1.property(CSSEngine.java:744)         在org.apache.batik.css.engine.CSSEngine.getCascadedStyleMap(CSSEngine.java:768)         在org.apache.batik.css.engine.CSSEngine.getComputedStyle(CSSEngine.java:876)         在org.apache.batik.bridge.CSSUtilities.getComputedStyle(CSSUtilities.java:82)         在org.apache.batik.bridge.CSSUtilities.convertVisibility(CSSUtilities.java:579)         在org.apache.batik.bridge.SVGSVGElementBridge.createGraphicsNode(SVGSVGElementBridge.java:142)         在org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:76)         at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)         在com.mmg.app.svg.TiledImageTranscoder.transcode(TiledImageTranscoder.java:61)         at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)         at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)         在com.mmg.app.svg.ShoeComponentPNGCreator.savePngImage(ShoeComponentPNGCreator.java:601)         at com.mmg.app.svg.ShoeComponentPNGCreator.create72DPIOriginalImage(ShoeComponentPNGCreator.java:305)         在com.mmg.app.svg.ShoeComponentPNGCreator.processStyleImaging(ShoeComponentPNGCreator.java:226)         在com.mmg.app.svg.ShoeComponentPNGCreator.traverseFolder(ShoeComponentPNGCreator.java:186)         在com.mmg.app.svg.ShoeComponentPNGCreator.main(ShoeComponentPNGCreator.java:662)

* CSSEngine:异常.... org.w3c.dom.DOMException:file:/ F:/workspace/MonoGeneiousShoe/src/svg/72DPI/Style-5/Left_Style-5.svg: 属性" font-style"表示无效的CSS值("正常")。 原始信息: " lormal"标识符不是" font-style"的有效值。属性。         在org.apache.batik.css.engine.CSSEngine.getCascadedStyleMap(CSSEngine.java:784)         在org.apache.batik.css.engine.CSSEngine.getComputedStyle(CSSEngine.java:876)         在org.apache.batik.bridge.CSSUtilities.getComputedStyle(CSSUtilities.java:82)         在org.apache.batik.bridge.CSSUtilities.convertVisibility(CSSUtilities.java:579)         在org.apache.batik.bridge.SVGSVGElementBridge.createGraphicsNode(SVGSVGElementBridge.java:142)         在org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:76)         at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)         在com.mmg.app.svg.TiledImageTranscoder.transcode(TiledImageTranscoder.java:61)         at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)         at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)         在com.mmg.app.svg.ShoeComponentPNGCreator.savePngImage(ShoeComponentPNGCreator.java:601)         at com.mmg.app.svg.ShoeComponentPNGCreator.create72DPIOriginalImage(ShoeComponentPNGCreator.java:305)         在com.mmg.app.svg.ShoeComponentPNGCreator.processStyleImaging(ShoeComponentPNGCreator.java:226)         在com.mmg.app.svg.ShoeComponentPNGCreator.traverseFolder(ShoeComponentPNGCreator.java:186)         在com.mmg.app.svg.ShoeComponentPNGCreator.main(ShoeComponentPNGCreator.java:662) 错误:null 附上例外: 文件:/ F:/workspace/MonoGeneiousShoe/src/svg/72DPI/Style-5/Left_Style-5.svg: 属性" font-style"表示无效的CSS值("正常")。 原始信息: " lormal"标识符不是" font-style"的有效值。属性。 显示java.lang.NullPointerException         在com.mmg.app.svg.TiledImageTranscoder.transcode(TiledImageTranscoder.java:69)         at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)         at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)         在com.mmg.app.svg.ShoeComponentPNGCreator.savePngImage(ShoeComponentPNGCreator.java:601)         at com.mmg.app.svg.ShoeComponentPNGCreator.create72DPIOriginalImage(ShoeComponentPNGCreator.java:305)         在com.mmg.app.svg.ShoeComponentPNGCreator.processStyleImaging(ShoeComponentPNGCreator.java:226)         在com.mmg.app.svg.ShoeComponentPNGCreator.traverseFolder(ShoeComponentPNGCreator.java:186)         在com.mmg.app.svg.ShoeComponentPNGCreator.main(ShoeComponentPNGCreator.java:662)

有时它表示高度为0,有时它表示笔画宽度不是有效属性等。

我认为这是与CSS引擎相关的问题。我在这做错了什么?

谢谢 Mihir Parekh

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。问题是多个线程同时更新画布,这就是为什么异常。解决方案是遵循代码,

UpdateManager updateManager = canvas.getUpdateManager();

                    updateManager.getUpdateRunnableQueue().invokeLater(
                            new Runnable() {
                                @Override
                                public void run() {
      // my update code here
}

谢谢