Convert SVG to JPG Image - Java Example Program

SVG Image to JPEG Conversion using Java Program:

In this tutorial, we will provide a Java program that will accept a SVG (Scalable Vector Graphics) input image file, and convert it into JPEG format, and create the JPEG file in the output. We will be using Apache Batik library to transcode SVG to JPG type. Download a copy of the library so that you can work out the example.

Contents:
SVG Image to JPEG Conversion in Java
Input SVG Image
Transcode SVG to JPG – JAR files
Step by Step Guide
   1.Read SVG Image in TranscoderInput
   2.Define JPG Output Format – TranscoderOutput
   3.Define Output Image Properties
   4.Convert SVG to JPEG
   5.Flush OutputStream After Conversion
SVG to JPEG Transcoding – Output Example

Input SVG Image:

A screen dump of the input SVG image that needs to be transcoded, is provided below.The tutorial assumes the user has the input image in a file format. We will cover other formats after this article.

SVG to JPG Conversion - Input Image Example
SVG to JPG Conversion - Input Image Example

Transcode SVG to JPG – JAR files

For us to transcode SVG to raster image format such as JPG, we would need the following JAR files to be available in the classpath.
  • Complete batik library
We can now move on to the step by step guide to a Java program that does the transcoding of the requirement in hand.

1.Read SVG Image in TranscoderInput

The first step to the conversion, is to read the SVG image into org.apache.batik.transcoder.TranscoderInput class. The input file needs to be converted to a URI and passed to this class. Following code snippet reads SVG Image in Java.
        //Step -1: We read the input SVG document into Transcoder Input
        String svg_URI_input = new File("chessboard.svg").toURL().toString();
        TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);

2. Define JPG Output Format – TranscoderOutput

The next step is to define the output file location. We want to change the SVG document to JPG, so we define a JPG file as OutputStream object, and attach it to org.apache.batik.transcoder.TranscoderOutput. Java code below:
        //Step-2: Define OutputStream to JPG file and attach to TranscoderOutput
        OutputStream jpg_ostream = new FileOutputStream("chessboard.jpg");
        TranscoderOutput output_jpg_image = new TranscoderOutput(jpg_ostream);

3. Define Output Image Properties

Here, we want the output to be JPEG format. So, we have to create a JPEGTranscoder and specify hints on the output format like quality. The code snippet is provided below:
        // Step-3: Create JPEGTranscoder and define hints
        JPEGTranscoder my_converter = new JPEGTranscoder();
        my_converter.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,new Float(.9));

4.Convert SVG to JPEG

We are done with the preparation steps. We can now invoke the “transcode” method and pass the input defined in step 1 and output defined in step 2. This code will write the JPEG image to the output location
        // Step-4: Write output
        my_converter.transcode(input_svg_image, output_jpg_image);

5. Flush OutputStream After Conversion

We are done. As a last step, flush the OutputStream, by following an example below
        // Step 5- close / flush Output Stream
        jpg_ostream.flush();
        jpg_ostream.close();        

Convert SVG to JPG in Java – Full Program

The complete Java program that explains how to convert SVG image to JPEG format by using Apache Batik library is provided below:
import java.io.*;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
public class svg2jpg {
    public static void main(String[] args) throws Exception {
        //Step -1: We read the input SVG document into Transcoder Input
        String svg_URI_input = new File("chessboard.svg").toURL().toString();
        TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);        
        //Step-2: Define OutputStream to JPG file and attach to TranscoderOutput
        OutputStream jpg_ostream = new FileOutputStream("chessboard.jpg");
        TranscoderOutput output_jpg_image = new TranscoderOutput(jpg_ostream);              
        // Step-3: Create JPEGTranscoder and define hints
        JPEGTranscoder my_converter = new JPEGTranscoder();
        my_converter.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,new Float(.9));
        // Step-4: Write output
        my_converter.transcode(input_svg_image, output_jpg_image);
        // Step 5- close / flush Output Stream
        jpg_ostream.flush();
        jpg_ostream.close();        
    }
}

SVG to JPEG Transcoding – Output Example

The program above created a JPEG image of 36.5 kb in the output from the SVG input. You need all the JAR files provided by Batik library to run this example. If you are stuck with an exception at runtime, you can refer to the exception list tutorial for a quick reference. To compile the code, you would require only batik-transcoder.jar file in your classpath.

That completes a superfast getting started example to convert SVG to JPG using Batik / Java. If you have any questions, you can post it in the comments section of this blog post.

2 comments:

  1. I would use this code in a separate and I would like appler in hand but I do not know how to help your class please

    ReplyDelete
  2. Wixh batik version did you use?

    Greets Dave

    ReplyDelete