如何将SVG路径(SVGOMPathElement)转换为点数组? [英] How to convert SVG Path (SVGOMPathElement) to array of points?

查看:565
本文介绍了如何将SVG路径(SVGOMPathElement)转换为点数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行的操作:将SVG路径转换为(x,y)点的数组.

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

我现在有这个:

public Point[] strokeToPoints(Node n){

    SVGOMPathElement path = (SVGOMPathElement) n;

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

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

    return null;

}

节点n始终是从SVG文件中提取的路径元素,看起来像这样:

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

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)

是什么导致此错误?我需要总长度,以便遍历整个长度,将点收集到一个数组中.

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

推荐答案

看起来应该像这样:

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

这篇关于如何将SVG路径(SVGOMPathElement)转换为点数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆