Can I use SVG Salamander to rasterize SVG in PNG files? (and how can I do this?)

Regarding the issue, I saw that SVG-Salamander is small enough for my projects. But I do not know if I can use it for this either one or the other.

Any idea?

If I can’t, do you know any other small PNG SVG rasterizer in java?

thanks for all.

I am using this code:

public static void main(String[] args) throws IOException, SVGException { // TODO Auto-generated method stub File f = new File("./src/game_scheme.svg"); SVGUniverse svgUniverse = new SVGUniverse(); SVGDiagram diagram = svgUniverse.getDiagram(svgUniverse.loadSVG(f.toURL())); BufferedImage bi = new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB); Graphics2D ig2 = bi.createGraphics(); diagram.render(ig2); ImageIO.write(bi, "PNG", new File("./yourImageName.png")); } 

But the images are not smooth :(, any idea?

+7
source share
2 answers

Yes it is possible. All you have to do is load the SVG document using the SVG Salamander, create the BufferedImage that you want to record, create the Graphics2D context from your BufferedImage, and then call the render () method on SVGDiagram to draw on your image.

You can simplify the process using SVGIcon. It will handle all Salamander internal elements for you, and you can treat your SVG document as a regular Swing icon.

If you plan to convert a large number of SVG files to images on the command line, there is also an Ant task that allows you to convert SVGs to images from an Ant script.

Docs on using SVG Salamander: http://svgsalamander.java.net/docs/use.html

Simple demo using SVG Salamander with Swing: http://svgsalamander.java.net/docs/exampleCode/SVGIODemo.html

+3
source

If you see jagged edges, you can fix this by adding a graphic rendering tooltip.

Also, if you have finished your graphics context, it is recommended that you call the dispose () command.

 Graphics2D ig2 = bi.createGraphics(); ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); diagram.render(ig2); ig2.dispose(); ImageIO.write(bi, "PNG", new File("./yourImageName.png")); 
+3
source

All Articles