Scaling an SVG in Java

元气小坏坏 提交于 2019-12-04 17:36:06

问题


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.

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.

This is what I have so far:

...
//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 ) );
...

回答1:


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).

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.



来源:https://stackoverflow.com/questions/2255691/scaling-an-svg-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!