Apache Batik Нет WriteAdapter доступен?
Я пишу код для преобразования SVG в PNG:
package com.example;
import java.io.*;
import java.nio.file.Paths;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.SVGAbstractTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
public class Main {
public static void main(String [] args) throws Exception {
// read the input SVG document into TranscoderInput
String svgURI = Paths.get(args[0]).toUri().toURL().toString();
TranscoderInput input = new TranscoderInput(svgURI);
// define OutputStream to PNG Image and attach to TranscoderOutput
OutputStream ostream = new FileOutputStream("out.png");
TranscoderOutput output = new TranscoderOutput(ostream);
// create a JPEG transcoder
PNGTranscoder t = new PNGTranscoder();
// set the transcoding hints
t.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, new Float(600));
t.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, new Float(600));
// convert and write output
t.transcode(input, output);
// flush and close the stream then exit
ostream.flush();
ostream.close();
}
}
Я получаю следующие исключения, выполняя его с различными SVG:
Exception in thread "main" org.apache.batik.transcoder.TranscoderException: null
Enclosed Exception:
Could not write PNG file because no WriteAdapter is availble
at org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscoder.java:132)
at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
at com.example.Main.main(Main.java:26)
Батик версия (сообщает Maven):
version=1.9
groupId=org.apache.xmlgraphics
artifactId=batik-transcoder
Я получаю ту же ошибку с батиком 1.7.
Предложения?
2 ответа
Эта проблема была решена Питером Коппенсом из списка рассылки xmlgraphics-batik-users. Проблема в том, что в репозитории Maven для Batik 1.9 отсутствует зависимость, которую можно устранить, добавив в pom.xml:
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-codec</artifactId>
<version>1.9</version>
</dependency>
Загадочное исключение исчезает, и код работает, как ожидается, с этим дополнением. Это было сообщено как ошибка для Batk 1.7 ( https://bz.apache.org/bugzilla/show_bug.cgi?id=44682).
В версии 1.17 все то же самое, решение все еще работает (implementation 'org.apache.xmlgraphics:batik-codec:1.17'
в градиенте)