Languages
[Edit]
EN

Java - convert SVG file to PNG file

7 points
Created by:
Broncono
466

In this article, we would like to show how to convert SVG file to PNG file using Java.

By default Java ImageIO doesn't support SVG files. It is necessary to use some external implementation that is Apache™ Batik SVG Toolkit.

The article contains example project that let's to convert SVG files to PNG files.

 

Example project

The project was created as Mavan project and run using VS Code.

Project structure

Java project structure that contains logic to convert SVG files to PNG files.
Java project structure that contains logic to convert SVG files to PNG files.

 

Project files

Used SVG file is available here and should be saved under e.g. /path/to/input.svg (change it in Program.java file also).

 

src/main/java/com/example/Program.java file:

package com.example;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;

public class Program {

    public static void main(String[] args) throws IOException, TranscoderException {

        String inputPath = "/path/to/input.svg";
        String outputPath = "/path/to/output.png";

        float outputWidth = 600.0f;
        float outputHeight = 600.0f;

        PNGTranscoder transcoder = new PNGTranscoder();
        transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, outputWidth);
        transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, outputHeight);
        try (FileInputStream inputStream = new FileInputStream(inputPath)) {
            TranscoderInput input = new TranscoderInput(inputStream);
            try (OutputStream outputStream = new FileOutputStream(outputPath)) {
                TranscoderOutput output = new TranscoderOutput(outputStream);
                transcoder.transcode(input, output);
                outputStream.flush();
            }
        }
    }
}

 

pom.xml file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>svg-to-png</artifactId>

    <name>Example SVG to PNG converter</name>
    <description>Example SVG to PNG converter using Apache Batik Transcoder.</description>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>17</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <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>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

 

Project running (using command line)

Build project using:

mvn clean compile

Run project using:

mvn exec:java -Dexec.mainClass=com.example.Program

 

See also

  1. Java - convert SVG file to JPG file

  2. Java - convert SVG string to BufferedImage

References

  1. Apache™ Batik SVG Toolkit
  2. Transcoder API - Apache Docs
  3. Batik Codec - Maven Repository
  4. Batik Transcoder - Maven Repository
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join