I'm trying to convert a SVG image into JPEG as shown in https://xmlgraphics.apache.org/batik/using/transcoder.html#createImage example. Here is the code:
public void saveAsjpeg() throws Exception {
// 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(inputFilePath).toURL().toString();
TranscoderInput input = new TranscoderInput(svgURI);
// Create the transcoder output.
OutputStream ostream = new FileOutputStream(outputFilePath);
TranscoderOutput output = new TranscoderOutput(ostream);
// Save the image.
t.transcode(input, output);
// Flush and close the stream.
ostream.flush();
ostream.close();
System.exit(0);
}
Below is my pom.xml. I'm trying in spring boot project:
<?xml version="1.0" encoding="UTF-8"?>
<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>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-codec</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-svgpp</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-rasterizer</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-squiggle</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>xmlgraphics-commons</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-ttf2svg</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I'm getting following exception:
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 SaveToJPEG.saveAsjpeg(SaveToJPEG.java:31)
at SaveToJPEG.main(SaveToJPEG.java:42)
I've several issues here:
- Why the exception stacktrace says "Unknown Source" and exception is so uninformative? I googled on this and read that if the jars don't have sources attached to them, the exception can be uninformative. I've put the plugin code in the pom to add the source. But this is not working.
- What is wrong in the batik code for it for not converting the svg image to jpeg?
I don't know who published this artifact but it has solved this problem for me
These classes are included with this artifact
This is a bug inside Apache Batik 1.8, referenced in BATIK-1136.
The issue is the following: the
JPEGTranscoder
is using the Service Provider API to acquire an instance of anImageWriter
that handles the"image/jpeg"
format. However, the class configured in the artifactbatik-codec
insideMETA-INF/services
points a classorg.apache.batik.ext.awt.image.codec.imageio.ImageIOJPEGImageWriter
that apparently wasn't released in the final package (since it exists in the source code).As such, there are 2 solutions.
Downgrade
Downgrade to version 1.7 with:
The classes were correctly released in this version.
Copy necessary classes
Copy the necessary classes from Apache Batik into your own sources. Inside a package
org.apache.batik.ext.awt.image.codec.imageio
, create the two following classes.First,
ImageIOImageWriter
:and then
ImageIOJPEGImageWriter
:Keeping version 1.8 and adding above classes, the code will work as-is.