gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:48:45 26 4
gpt4 key购买 nike

我正在通过以下代码行将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的实现

公共无效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:异常property.syntax.error:org.w3c.dom.DOMException:“ o”标识符对于“ shape-rendering”属性不是有效值。

属性值:自动

* CSSEngine:异常property.syntax.error:org.w3c.dom.DOMException:“”标识符对于“ shape-rendering”属性不是有效值。

属性值:自动

异常:org.w3c.dom.DOMException
org.w3c.dom.DOMException:“”标识符不是“ shape-rendering”属性的有效值。
        在org.apache.batik.css.engine.value.AbstractValueFactory.createInvalidIdentifierDOMException(AbstractValueFactory.java:50)
        在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.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)
        在com.mmg.app.svg.TiledImageTranscoder.transcode(TiledImageTranscoder.java:61)
        在org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
        在org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
        在com.mmg.app.svg.ShoeComponentPNGCreator.savePngImage(ShoeComponentPNGCreator.java:601)
        在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:文件:/ 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)
        在org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)
        在com.mmg.app.svg.TiledImageTranscoder.transcode(TiledImageTranscoder.java:61)
        在org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
        在org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
        在com.mmg.app.svg.ShoeComponentPNGCreator.savePngImage(ShoeComponentPNGCreator.java:601)
        在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”属性的有效值。
        在org.apache.batik.css.engine.value.AbstractValueFactory.createInvalidIdentifierDOMException(AbstractValueFactory.java:50)
        在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)
错误:空
随附的异常:
文件:/ F:/workspace/MonoGeneiousShoe/src/svg/72DPI/Style-5/Left_Style-5.svg:
属性“ shape-rendering”表示无效的CSS值(“ auto”)。
原始信息:
“”标识符不是“ shape-rendering”属性的有效值。

* CSSEngine:例外。
org.w3c.dom.DOMException:文件:/ 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)
错误:空
随附的异常:
文件:/ F:/workspace/MonoGeneiousShoe/src/svg/72DPI/Style-5/Left_Style-5.svg:
属性“ shape-rendering”表示无效的CSS值(“ auto”)。
原始信息:
“ o”标识符不是“ shape-rendering”属性的有效值。

* CSSEngine:异常property.syntax.error:org.w3c.dom.DOMException:“原始”标识符不是“字体样式”属性的有效值。

属性值:正常

异常:org.w3c.dom.DOMException
org.w3c.dom.DOMException:“原始”标识符不是“字体样式”属性的有效值。
        在org.apache.batik.css.engine.value.AbstractValueFactory.createInvalidIdentifierDOMException(AbstractValueFactory.java:50)
        在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.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)
        在com.mmg.app.svg.TiledImageTranscoder.transcode(TiledImageTranscoder.java:61)
        在org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
        在org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
        在com.mmg.app.svg.ShoeComponentPNGCreator.savePngImage(ShoeComponentPNGCreator.java:601)
        在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:文件:/ F:/workspace/MonoGeneiousShoe/src/svg/72DPI/Style-5/Left_Style-5.svg:
属性“字体样式”表示无效的CSS值(“正常”)。
原始信息:
“标准”标识符对于“字体样式”属性不是有效值。
        在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.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)
        在com.mmg.app.svg.TiledImageTranscoder.transcode(TiledImageTranscoder.java:61)
        在org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
        在org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
        在com.mmg.app.svg.ShoeComponentPNGCreator.savePngImage(ShoeComponentPNGCreator.java:601)
        在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)
错误:空
随附的异常:
文件:/ F:/workspace/MonoGeneiousShoe/src/svg/72DPI/Style-5/Left_Style-5.svg:
属性“字体样式”表示无效的CSS值(“正常”)。
原始信息:
“标准”标识符对于“字体样式”属性不是有效值。
java.lang.NullPointerException
        在com.mmg.app.svg.TiledImageTranscoder.transcode(TiledImageTranscoder.java:69)
        在org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
        在org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
        在com.mmg.app.svg.ShoeComponentPNGCreator.savePngImage(ShoeComponentPNGCreator.java:601)
        在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)

有时它说height为0,有时它说stroke-width是无效的属性,等等。

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

谢谢
米希尔·帕雷克(Mihir Parekh)

最佳答案

我找到了解决方案。问题是多个线程在画布上同时更新,这就是异常的原因。解决方案是遵循以下代码,

UpdateManager updateManager = canvas.getUpdateManager();

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


谢谢

关于java - 使用apache batik 加载,更新和转码svg文档时出现奇怪的CSS异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15384640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com