I use Batik to work with SVG images. In particular, I have a scene with several shapes, and I need to be able to convert each shape into a separate BufferedImage. For this, I use the following code:
SVGDocument document = null; // Load the document String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); File file = new File(inPath); try { document = (SVGDocument) f.createDocument(file.toURL().toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // Build the tree and get the document dimensions UserAgentAdapter userAgentAdapter = new UserAgentAdapter(); BridgeContext bridgeContext = new BridgeContext(userAgentAdapter); GVTBuilder builder = new GVTBuilder(); GraphicsNode graphicsNode = builder.build(bridgeContext, document); CanvasGraphicsNode canvasGraphicsNode = (CanvasGraphicsNode) graphicsNode.getRoot().getChildren().get(0); if(canvasGraphicsNode.getChildren().get(i) instanceof ShapeNode) { currentNode = (ShapeNode) canvasGraphicsNode.getChildren().get(i); convertNodeToImage (currentNode); }
This is pretty standard. I run Batik and make it parse the SVG file. Here's the conversion of the node to an image function:
Rectangle2D bounds; BufferedImage bufferedImage; Graphics2D g2d;
This is great for rectangles and straight lines, but it does not work for splines. For splines, the boundaries are greater than the resulting spline. I think this is because the getBounds function includes breakpoints in the calculation of boundaries. I need to find the boundaries of the spline only, that is, if the spline were stroked, I would like to find the boundaries of this stroke. I tried all the getBounds () functions (getSensativeBounds, getGeometryBounds ...) and they all give me the same result. So I wonder if I missed something? Is this a mistake in Batik? Or if there is a workaround?
The workaround I was thinking about is getting a list of the vertices of the form and calculating the borders manually. However, I could not find how to get a list of vertices.
Any help would be greatly appreciated.
source share