用Java读取SVG路径 [英] Reading SVG path in Java

查看:212
本文介绍了用Java读取SVG路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取Java中的文本文件,在这里我使用扫描仪类来读取SVG格式的文本.我看到了推荐蜡染的答案.我想知道如何仅在SVG格式的路径数据上使用蜡染.我想评估路径并提取出可以追踪出该路径的点.我可以使用蜡染布执行此操作,而不必在蜡染布中评估整个文件吗?

I'm reading text files in java where I use the scanner class to read text in SVG format. I have seen answers that recommend Batik. I would like to know how to use Batik only on the path data in SVG format. I want to evaluate the path and extract points that would trace out the path. Can I do this using Batik without having to evaluate the entire file in Batik?

我试图创建自己的路径解析器,但是我没有足够的时间或技能来创建.我正在通过使用< and />拆分SVG格式的标签来评估标签,并且我坚持评估路径的d属性.这是我需要评估的路径的示例:

I was trying to create my own path parser but I don't have enough time or skill to do so. I am evaluating tags in SVG format by splitting them using < and /> and I am stuck on evaluating the d attribute of paths. Here's an example of a path that I need to evaluate:

<html>
<body>

<svg width="10000" height="1000">
<path id="square" fill="#0000FF" 
d="M351.3,251 l-3.1-2.2c-0.3-0.2-0.3-0.5-0.1-0.8l2.2-3.1c0.2-0.3,0.5-0.3,0.8-0.1l3.1,2.2
c0.3,0.2,0.3,0.5,0.1,0.8l-2.2,3.1C355,251.1,354.6,251.2,354.3,251z"/>

</body>
</html>

我需要沿着这条路的点.

I need the points along this path.

推荐答案

用蜡染布解析的路径看起来很简单:

The path parsing with Batik looks fairly simple:

似乎您只需要创建一个实现PathHandler接口的类即可.

It appears like you just have to create a class that implements the PathHandler interface.

class MyPathHandler implements PathHandler
{
  void  startPath() { ... }
  void  movetoAbs(float x, float y)
  ... etc...
  void  endPath() { ... }
}

然后做:

  PathParser parser = new PathParser();
  parser.setPathHandler(new MyPathHandler());
  parser.parse("M351.3,251 l-3.1-2.2 ... snip ... C355,251.1,354.6,251.2,354.3,251z");
  parser.doParse();

每当PathParser遇到M命令等时,都会调用moveToAbs()方法.

The moveToAbs() method will be called whenever the PathParser encounters an M command etc.

如果这行不通,那么大约一分钟的搜索时间,我就能在Java或JS中找到至少四个或五个SVG路径解析代码实例.

If that doesn't work then I was able to find at least four or five instances of SVG path parsing code in Java or JS in about one minute of googling.

至于采样部分.这要复杂得多.您将需要代码来测量二次方和三次方贝塞尔曲线和圆弧的长度.后者可能涉及将弧(A/a)转换为中心参数化(规范中有关如何执行此操作的信息).做到这一切并非无关紧要.如果可以的话,将在Batik,Firefox,Chrome,Android等系统中为它提供代码.也许那里已经有人在构建代码了.

As for the sampling part. That's a lot trickier. You'll need code to measure the length of quadratic and cubic beziers, and arcs. The latter will probably involve converting the arcs (A/a) to centre parameterization (theres information in the spec on how to do that). It's not trivial to do all this. There'll be code for it, if you can find it, in Batik, Firefox, Chrome, Android etc. Perhaps there is also code out there that someone has already built.

如果在浏览器中进行操作将很容易.对此已经有疑问和答案了.

It would be easy if you were doing it in a browser. There have already been questions asked and answered here on that.

这篇关于用Java读取SVG路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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