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:
xxxxxxxxxx
1
package com.example;
2
3
import java.awt.image.BufferedImage;
4
import java.io.IOException;
5
6
import org.apache.batik.transcoder.TranscoderException;
7
8
public class Program {
9
10
public static void main(String[] args) throws TranscoderException, IOException {
11
12
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>";
13
BufferedImage outputImage = SvgUtils.toImage(inputImage, 400, 400);
14
15
// ...
16
}
17
}
Hint: to save
BufferedImage
as PNG file read this article.
SvgUtils.java
file:
xxxxxxxxxx
1
package com.example;
2
3
import java.awt.image.BufferedImage;
4
import java.io.StringReader;
5
6
import org.apache.batik.transcoder.TranscoderException;
7
import org.apache.batik.transcoder.TranscoderInput;
8
import org.apache.batik.transcoder.image.ImageTranscoder;
9
10
public class SvgUtils {
11
12
private SvgUtils() {
13
// Nothing here ...
14
}
15
16
public static BufferedImage toImage(String inputSvg, int outputWidth, int outputHeight) throws TranscoderException {
17
BufferedImageTranscoder imageTranscoder = new BufferedImageTranscoder();
18
imageTranscoder.addTranscodingHint(ImageTranscoder.KEY_WIDTH, (float) outputWidth);
19
imageTranscoder.addTranscodingHint(ImageTranscoder.KEY_HEIGHT, (float) outputHeight);
20
try (StringReader stringReader = new StringReader(inputSvg)) {
21
TranscoderInput transcoderInput = new TranscoderInput(stringReader);
22
imageTranscoder.transcode(transcoderInput, null);
23
return imageTranscoder.getImage();
24
}
25
}
26
}
BufferedImageTranscoder.java
file:
xxxxxxxxxx
1
package com.example;
2
3
import java.awt.image.BufferedImage;
4
5
import org.apache.batik.transcoder.TranscoderException;
6
import org.apache.batik.transcoder.TranscoderOutput;
7
import org.apache.batik.transcoder.image.ImageTranscoder;
8
9
public class BufferedImageTranscoder extends ImageTranscoder {
10
11
private BufferedImage image = null;
12
13
public BufferedImageTranscoder() {
14
// Nothing here ...
15
}
16
17
public BufferedImage getImage() {
18
return this.image;
19
}
20
21
22
public BufferedImage createImage(int width, int height) {
23
return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
24
}
25
26
27
public void writeImage(BufferedImage image, TranscoderOutput output) throws TranscoderException {
28
if (output != null) {
29
throw new TranscoderException("Buffered image transcoder does not support transcoder output.");
30
}
31
this.image = image;
32
}
33
}
Used dependencies:
xxxxxxxxxx
1
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-codec -->
2
<dependency>
3
<groupId>org.apache.xmlgraphics</groupId>
4
<artifactId>batik-codec</artifactId>
5
<version>1.17</version>
6
</dependency>
7
8
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-transcoder -->
9
<dependency>
10
<groupId>org.apache.xmlgraphics</groupId>
11
<artifactId>batik-transcoder</artifactId>
12
<version>1.17</version>
13
</dependency>