Apache Batik 没有可用的 WriteAdapter?

问题描述 投票:0回答:2

我正在编写代码将 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 中遇到同样的错误。

建议?

svg batik
2个回答
30
投票

该问题已由 xmlgraphics-batik-users 邮件列表上的 Peter Coppens 解决。问题是 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)。


0
投票

在 1.17 中仍然相同,解决方案仍然有效(

implementation 'org.apache.xmlgraphics:batik-codec:1.17'
在 gradle 中)

© www.soinside.com 2019 - 2024. All rights reserved.