[英]How to get the image size (width/height) of an SVG image with batik
How can I get the size (width/height) of an SVG image using batik (1.7)?如何使用蜡染 (1.7) 获取 SVG 图像的大小(宽度/高度)?
String s = "https://openclipart.org/download/228858";
InputStream is = new URL(s).openStream();
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = f.newDocumentBuilder();
Document doc = builder.parse(is);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc);
SVGGraphics2D svg = new SVGGraphics2D(ctx,false);
Dimension d = svg.getSVGCanvasSize();
Rectangle r = svg.getClipBounds();
System.out.println(svg.toString()); //org.apache.batik.svggen.SVGGraphics2D[font=java.awt.Font[family=Dialog,name=sanserif,style=plain,size=12],color=java.awt.Color[r=0,g=0,b=0]]
System.out.println("Dimension null? "+(d==null)); //true
System.out.println("Rectangle null? "+(r==null)); //true
The example can be directly executed and is downloading a image from open clipart.org.该示例可以直接执行,并且正在从打开的clipart.org 下载图像。 Alternatively to a absolute size I'm also interested in the aspect ratio of the image.除了绝对尺寸,我还对图像的纵横比感兴趣。
try this code , by the way the SAXparser raises an error in case of the svg image that you passed because the attribute r should be defined for circle element i made my testes on svg samples that come with Batik Batik's samples folder试试这段代码,顺便说一下,SAXparser 会在您传递的 svg 图像的情况下引发错误,因为应该为圆形元素定义属性 r 我在 Batik Batik 的示例文件夹附带的 svg 示例上进行了测试
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(
XMLResourceDescriptor.getXMLParserClassName());
File file = new File("C:/resources/chessboard.svg");
InputStream is = new FileInputStream(file);
Document document = factory.createDocument(
file.toURI().toURL().toString(), is);
UserAgent agent = new UserAgentAdapter();
DocumentLoader loader= new DocumentLoader(agent);
BridgeContext context = new BridgeContext(agent, loader);
context.setDynamic(true);
GVTBuilder builder= new GVTBuilder();
GraphicsNode root= builder.build(context, document);
System.out.println(root.getPrimitiveBounds().getWidth());
System.out.println(root.getPrimitiveBounds().getHeight());
getPrimitiveBounds(): Returns the bounds of the area covered by this node's primitive paint. getPrimitiveBounds():返回此节点的图元绘制所覆盖区域的边界。 This is the painted region of fill and stroke but does not account for clipping, masking or filtering这是填充和描边的绘制区域,但不考虑剪切、遮罩或过滤
To get the SVG image dimensions and ratio it is also sufficient to check viewBox
attribute of an SVG image.要获得 SVG 图像的尺寸和比例,检查 SVG 图像的viewBox
属性也足够了。
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(
XMLResourceDescriptor.getXMLParserClassName());
File file = new File("C:/resources/chessboard.svg");
InputStream is = new FileInputStream(file);
Document document = factory.createDocument(
file.toURI().toURL().toString(), is);
String viewBox = document.getDocumentElement().getAttribute("viewBox");
String[] viewBoxValues = viewBox.split(" ");
if (viewBoxValues.length > 3) {
width = Integer.parseInt(viewBoxValues[2]);
height = Integer.parseInt(viewBoxValues[3]);
}
In this code we check viewBox
attribute and if it is present and has 4 values, we take 3rd (width) and 4th (height).在这段代码中,我们检查viewBox
属性,如果它存在并且有 4 个值,我们取第 3 个(宽度)和第 4 个(高度)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.