Created
December 10, 2013 13:05
-
-
Save eclecticlogic/7890297 to your computer and use it in GitHub Desktop.
Java code to convert an SVG into a BufferedImage (PNG, JPEG etc.) using Apache Batik
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public BufferedImage createImageFromSVG(String svg) { | |
Reader reader = new BufferedReader(new StringReader(svg)); | |
TranscoderInput svgImage = new TranscoderInput(reader); | |
BufferedImageTranscoder transcoder = new BufferedImageTranscoder(); | |
transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, (float) component.getWidth()); | |
transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, (float) component.getHeight()); | |
try { | |
transcoder.transcode(svgImage, null); | |
} catch (TranscoderException e) { | |
throw Throwables.propagate(e); | |
} | |
return transcoder.getImage(); | |
} |
You can also convert svg to png format without the use of Batik Transcoder. Follow below link:
https://nupur28ag.blogspot.in/
BufferedImage input_image = null;
input_image = ImageIO.read(new File("Convert_to_PNG.svg")); //read svginto input_image object
File outputfile = new File("imageio_png_output.png"); //create new outputfile object
ImageIO.write(input_image, "PNG", outputfile);
You can also convert svg to png format without the use of Batik Transcoder. Follow below link:
Nop. It doesn't work. ImageIO#read does not support SVG format.
You can find the supported formats following this link https://docs.oracle.com/javase/tutorial/2d/images/loadimage.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
while I'm using your BufferedImageTranscoder, I encountered this problem; The element type "link" must be terminated by the matching end-tag ""