【问题标题】:How to convert SVG Path (SVGOMPathElement) to array of points? [duplicate]如何将 SVG 路径 (SVGOMPathElement) 转换为点数组? [复制]
【发布时间】:2014-05-14 08:14:42
【问题描述】:

我正在尝试做的事情:将 SVG 路径转换为 ​​(x,y) 点的数组。

我现在有这个:

public Point[] strokeToPoints(Node n){

    SVGOMPathElement path = (SVGOMPathElement) n;

    System.out.println(path.getAttribute("d")); 

    System.out.println(path.getTotalLength());

    return null;

}

节点 n 始终是从 SVG 文件中提取的路径元素,如下所示:

<path id="kvg:098df-s1" kvg:type="㇒" d="M52.75,10.5c0.11,0.98-0.19,2.67-0.97,3.93C45,25.34,31.75,41.19,14,51.5"/>

线

System.out.println(path.getAttribute("d"));

返回

M52.75,10.5c0.11,0.98-0.19,2.67-0.97,3.93C45,25.34,31.75,41.19,14,51.5

这很好,但是行

System.out.println(path.getTotalLength());

返回

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at org.apache.batik.dom.svg.SVGPathSupport.getTotalLength(SVGPathSupport.java:41)
    at org.apache.batik.dom.svg.SVGOMPathElement.getTotalLength(SVGOMPathElement.java:131)

是什么导致了这个错误?我需要总长度,这样我就可以遍历它,将点收集到一个数组中。

【问题讨论】:

    标签: java svg element shape batik


    【解决方案1】:

    看起来应该是这样的:

    PathLength  pathLenObj = new PathLength((Shape) n);
    float  length = pathLenObj.pathLength;
    

    【讨论】:

    • 感谢您的回复。我听从了您的建议,但不幸收到了错误:org.apache.batik.dom.svg.SVGOMPathElement cannot be cast to java.awt.Shape 您能否进一步说明情况?
    • 对不起。我的错。我没有检查Shape的定义。看起来它可能是一个不供用户使用的内部类。也许你会在 Batik 邮件列表上获得帮助会更好。 xmlgraphics.apache.org/batik/mailing-lists.html
    • 我会这样做的。我在我的问题上取得了一些进展,但遇到了另一个障碍。我已经修改了我的问题。如果您能看一看,我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2013-04-21
    • 2019-04-14
    • 2011-12-06
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    相关资源
    最近更新 更多