javaapachesvgbatikimage-transcoding

Generate JPG from SVG using Apache Batik


I'm trying to turn a .svg file into a .jpg or .png file. Right now I'm trying to use Apache Batik Transcoder (Link).

This is my code right now: (official code from here)

// Create a JPEG transcoder
JPEGTranscoder t = new JPEGTranscoder();

// Set the transcoding hints.
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
    new Float(.8));

// Create the transcoder input.
String svgURI = new File("C:/test.svg").toURL().toString();
TranscoderInput input = new TranscoderInput(svgURI);

// Create the transcoder output.
OutputStream ostream = new FileOutputStream("C:/out.jpg");
TranscoderOutput output = new TranscoderOutput(ostream);

// Save the image.
t.transcode(input, output);

// Flush and close the stream.
ostream.flush();
ostream.close();
System.exit(0);

But it gives my this error:

Exception in thread "main" org.apache.batik.transcoder.TranscoderException: null

Enclosed Exception:

null

at org.apache.batik.transcoder.image.ImageTranscoder.transcode(Unknown Source)

at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)

at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)

at SaveAsJPEG.main(SaveAsJPEG.java:27)

I don't know what to do as this is the code from their website (!). So if you know how to solve it or have another idea how I can turn my .svg pictures into .jpg and .png's let me know.


Solution

  • Well, I have now found the solution to my problem. It seems like Batik 1.8 (which I have used) does not work properly (or at least does not work in the way they say it on their API page.

    I now used Batik 1.7 wich works just fine. So now everything is fine and to everyone who wants to use Batik: I suggest you to use 1.7 because the documentation is not about 1.8.