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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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