EN
Java - convert SVG string to BufferedImage
5
points
In this article, we want to show how to convert SVG string to BufferedImage in Java.

Program.java
file:
package com.example;
import java.awt.image.BufferedImage;
import java.io.IOException;
import org.apache.batik.transcoder.TranscoderException;
public class Program {
public static void main(String[] args) throws TranscoderException, IOException {
String inputImage = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 512 448\"><path d=\"M470.894 227.913c.006 6.07-2.473 12.355-7.36 17.242L293.889 414.8s-14.877 14.952-22.775 22.776c-7.898 7.823-21.89 8.26-30.225 0S112.577 309.269 48.465 245.157c-9.198-9.198-9.863-23.345-1.49-31.717l22.777-22.777c8.372-8.373 22.517-7.706 31.715 1.492L218.523 309.21V27.87c0-13.008 9.53-23.48 21.371-23.48h32.211c11.84 0 21.373 10.472 21.373 23.48v281.339l117.055-117.057c9.198-9.198 23.344-9.863 31.717-1.49l22.775 22.775c3.925 3.925 5.864 9.117 5.87 14.475z\" /></svg>";
BufferedImage outputImage = SvgUtils.toImage(inputImage, 400, 400);
// ...
}
}
Hint: to save
BufferedImage
as PNG file read this article.
Common utils
SvgUtils.java
file:
package com.example;
import java.awt.image.BufferedImage;
import java.io.StringReader;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.image.ImageTranscoder;
public class SvgUtils {
private SvgUtils() {
// Nothing here ...
}
public static BufferedImage toImage(String inputSvg, int outputWidth, int outputHeight) throws TranscoderException {
BufferedImageTranscoder imageTranscoder = new BufferedImageTranscoder();
imageTranscoder.addTranscodingHint(ImageTranscoder.KEY_WIDTH, (float) outputWidth);
imageTranscoder.addTranscodingHint(ImageTranscoder.KEY_HEIGHT, (float) outputHeight);
try (StringReader stringReader = new StringReader(inputSvg)) {
TranscoderInput transcoderInput = new TranscoderInput(stringReader);
imageTranscoder.transcode(transcoderInput, null);
return imageTranscoder.getImage();
}
}
}
BufferedImageTranscoder.java
file:
package com.example;
import java.awt.image.BufferedImage;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.ImageTranscoder;
public class BufferedImageTranscoder extends ImageTranscoder {
private BufferedImage image = null;
public BufferedImageTranscoder() {
// Nothing here ...
}
public BufferedImage getImage() {
return this.image;
}
@Override
public BufferedImage createImage(int width, int height) {
return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
@Override
public void writeImage(BufferedImage image, TranscoderOutput output) throws TranscoderException {
if (output != null) {
throw new TranscoderException("Buffered image transcoder does not support transcoder output.");
}
this.image = image;
}
}
Used dependencies:
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-codec -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-codec</artifactId>
<version>1.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-transcoder -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.17</version>
</dependency>