用Java扩展SVG [英] Scaling an SVG in Java

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

问题描述

我正在尝试缩放图像,修改它,然后输出到另一种图像格式。到目前为止,我一直在使用 apache batik 库。对于简单的转换,这很容易。对于裁剪svg,这很容易。

I'm attempting to scale an image, modify it, and then output to another image format. So far, I've been using the apache batik library. For simple conversion, this is easy. For clipping the svg, this is easy.

但是,我似乎无法弄清楚如何缩放到svg创建的完整图像。也就是说,我可以将感兴趣的区域指定为边界矩形,然后缩放在边界矩形上工作,但我不知道如何缩放svg的图像。

However, I can't seem to figure out how to scale to the full image created by the svg. That is, I can specify the area of interest as a bounding rectangle, and then scaling works over the bounding rectangle, but I do NOT know how to scale over the image of the svg.

这是我到目前为止:

...
//set the output width and height  
transcoder.addTranscodingHint( PNGTranscoder.KEY_WIDTH, new Float( newSize.width ) );
transcoder.addTranscodingHint( PNGTranscoder.KEY_HEIGHT, new Float( newSize.height ) );

//set the aoi for scaling. Unsure what to do here.
transcoder.addTranscodingHint( PNGTranscoder.KEY_AOI, new Rectangle( 0, 0, 100, 100 ) );
...


推荐答案

如果你不喜欢设置KEY_AOI转码提示,然后根< svg> 元素上的 viewBox =属性将是用来确定感兴趣的领域。如果您要转码的文档没有 viewBox =属性,那么 width =并且将使用 height =属性,因此AOI将为(0,0,宽度,高度)。

If you don't set the KEY_AOI transcoding hint, then the viewBox="" attribute on the root <svg> element will be used to determine the area of interest. If the document you are transcoding doesn't have have a viewBox="" attribute, then the width="" and height="" attributes will be used, so the AOI will be (0, 0, width, height).

如果没有设置这些,并且您事先不知道文档的坐标系中图形的位置,那么您可以计算根< svg> <的边界框/ code>元素并将其用作AOI。您可以先为您的文档启动DOM,然后调用<文件元素上的code> getBBox()

And if none of these are set, and you don't know in advance where within the document's coordinate system the graphics are, then you could compute the bounding box of the root <svg> element and use that as the AOI. You could do this by first "booting the DOM" for your document, and then calling getBBox() on the document element.

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

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