Apache Batik没有可用的WriteAdapter?

17
我正在编写代码将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)

Batik版本(由Maven报告):

version=1.9
groupId=org.apache.xmlgraphics
artifactId=batik-transcoder

我在使用Batik 1.7时遇到了相同的错误。
有什么建议吗?

参见:https://github.com/css4j/echosvg/issues/11 - Dave Jarvis
2个回答

30

Peter Coppens在xmlgraphics-batik-users邮件列表中解决了这个问题。问题是Batik 1.9的Maven库缺少一个依赖项,可以通过向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)。


在哪里找到pom.xml文件? - Lokesh Pandey
1
我的pom.xml文件的依赖节点包括: <dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>batik-transcoder</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>org.apache.xmlgraphics</groupId> <artifactId>batik-codec</artifactId> <version>1.9</version> </dependency>希望这有所帮助。 - Al Pacifico
1
我在1.10版本中也遇到了同样的问题。 - ankitkpd
1
同版本1.11相同 - rumman0786
2
仍然在1.13中保持不变。 - ZhekaKozlov
显示剩余2条评论

0
依然在1.17版本中保持不变,解决方案仍然有效(在gradle中使用implementation 'org.apache.xmlgraphics:batik-codec:1.17'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接