问题
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