How to convert SVG Path (SVGOMPathElement) to array of points? [duplicate]

梦想与她 提交于 2019-12-23 03:07:43

问题


What I'm trying to do: convert an SVG path to an array of (x,y) points.

I have this at the moment:

public Point[] strokeToPoints(Node n){

    SVGOMPathElement path = (SVGOMPathElement) n;

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

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

    return null;

}

Node n is always a path element extracted from an SVG file which looks something like this:

<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"/>

The line

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

returns

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

which is fine, but the line

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

returns

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)

What's causing this error? I need the total length so I can traverse it collecting the points to an array.


回答1:


It looks like it should be something like:

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


来源:https://stackoverflow.com/questions/22821740/how-to-convert-svg-path-svgompathelement-to-array-of-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!