以编程方式如何使用java获取SVG文档中的形状宽度

时间:2011-06-16 12:10:55

标签: java svg

我想在这个svg文档中获得形状宽度

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 13.0.1, SVG Export Plug-In . SVG Version: 6.00 Build            14948)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Tiny//EN"   "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd">
<svg version="1.1" baseProfile="tiny" id="Layer_1" xmlns="http://www.w3.org/2000/svg"   xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" width="690.91px" height="2317.42px" viewBox="0 0 690.91 2317.42"   xml:space="preserve">
<path d="M502.375,1121.015l-80.08-54.199l-40.039,68.85l-81.06-55.67l45.412-76.17l79.59,54.2l43.949-71.29l78.131,53.22
L502.375,1121.015z M423.756,1312.415l-3.41,11.721c0,7.811,3.41,14.16,9.762,19.529c1.947,1.471,5.369,3.42,9.77,5.37
c10.25,5.859,37.59,8.79,61.029,8.79c12.211,0,20.02-0.48,25.881-0.97c-2.439-2.932-7.811-10.262-16.109-21.49
c-8.301-11.229-13.67-18.061-16.121-20.51c-14.16-13.182-27.34-20.012-39.549-20.012c-7.32,0-12.691,1.461-16.111,3.9
C434.016,1300.695,429.125,1305.095,423.756,1312.415L423.756,1312.415z M593.686,1527.266c-1.961,10.739-6.35,23.431-13.191,37.601
l-18.549,39.061c-19.051,40.521-60.061,78.609-122.561,113.279c-62.01,35.16-122.07,52.729-180.18,52.729l-27.34,0.49h-10.74
c-8.79,0-14.16-0.979-15.63-2.439c-28.32,0-56.64-5.37-84.47-16.11c-34.67-13.188-62.5-32.72-83.011-57.62
c-24.899-30.76-37.6-68.358-37.6-112.789l-0.97-26.37c0-20.021,4.88-46.391,15.13-78.609c11.72-37.601,28.319-76.66,50.3-116.7
c14.64-27.35,22.95-41.021,24.41-41.51l22.46,7.818l-1.95,3.41l-12.7,24.91l-27.34,69.82c-9.77,28.319-14.649,57.62-14.649,88.38
c0,22.46,5.859,45.899,17.09,70.8c9.77,21.48,17.58,33.2,23.93,34.67c3.41,8.301,18.06,17.58,43.94,27.83
c29.3,11.23,58.109,17.09,86.43,17.09c38.57,0,63.96-0.979,73.729-3.42c21.971,0,45.901-3.42,72.26-10.739
c29.791-8.301,57.131-19.529,83.01-34.181c16.111-9.279,36.131-25.39,61.041-47.359c7.811-7.33,18.061-17.09,31.25-29.79
c4.879-7.811,8.301-14.159,10.74-18.55c7.318-14.65,11.229-26.37,11.229-35.65c-0.49-8.3-1.471-16.6-4.881-27.829h-83.5
c-24.9,0-46.391-3.91-63.479-12.21c-5.369-2.44-11.709-6.83-18.551-12.2c-6.352-5.859-10.738-11.229-13.67-16.609
c-9.279-16.109-14.16-34.66-14.16-56.641c0-24.9,2.932-45.41,8.789-62.01c1.951-5.37,4.883-11.722,9.271-18.062
c9.281-19.539,17.09-34.18,23.932-43.459c20.51-28.811,41.988-43.461,64.939-43.461c21,0,40.039,11.721,58.109,34.67
c12.199,16.11,23.92,37.602,34.658,64.94c3.91,11.72,7.82,23.439,11.723,34.67c1.959,3.42,3.42,6.351,4.398,8.79
c3.898,9.28,7.318,19.53,9.762,31.74h93.26v89.84h-83.49c0,13.67-1.951,29.79-6.35,47.85
C599.055,1509.686,597.096,1517.495,593.686,1527.266z"/>
</svg>

这个文档只包含一个形状(路径),我想得到它的宽度而不是文档宽度本身

非常感谢

2 个答案:

答案 0 :(得分:0)

要提取形状坐标,请使用以下代码:

String path = input.replaceAll(".+<path d=\"(.+?)\" ?/>.+", "$1");

然后你可以尝试拆分逗号:

String[] parts = path.split(",");

我不知道如何解释数据,但您可以尝试使用minX,minY,maxX和maxY。搜索最大值和最小值后,您可以获得宽度和高度:

float width = maxX - minX;
float height = maxY - maxY;

这是一般性的,但我应该看看Bakit SVG Toolkit

答案 1 :(得分:0)

你必须使用Java吗?

您可以通过在SVG中添加以下内容来获得答案:

<script type="text/ecmascript">
<![CDATA[
    function getWidth(evt)
    {
        var bb = evt.target.getBBox();
        alert(bb.width - bb.x);
    }
]]>
</script>

然后将onmousedown="getWidth(evt)"添加到路径元素中。然后,如果您在Web浏览器中打开SVG并单击图像,则会发现其宽度为691.466。

否则你必须编写自己的程序来解释路径的坐标,这不是直截了当的。

简言之:

  • 在* M * x y,移动到坐标x y
  • 在* l * x y,移至+ x,+ y
  • 在* h * x,移至+ x,+ 0
  • 在* v * y,移至0,+ y
  • 在* c * x1 y1 x2 y2 x3 y3,移至+ x3,+ y3

然而,对于Bézier曲线(用c表示),某些部分可能会超出坐标。

有关详细信息,请参阅http://www.w3.org/TR/SVG/paths.html#PathDataMovetoCommands