首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SVG到PNG -巴蒂克

SVG到PNG -巴蒂克
EN

Stack Overflow用户
提问于 2015-09-22 15:56:34
回答 1查看 6.5K关注 0票数 1

在使用Apache从SVG转换为PNG时,有时会出现奇怪的错误。例如,对于这个SVG https://www.macstories.net/app/themes/macstories4/images/logo-shape-bw.svg,它会抛出一个异常,但这个logo.svg不会抛出异常。

这是我的代码:

代码语言:javascript
运行
复制
package com.stackoverflow.batik;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.batik.transcoder.SVGAbstractTranscoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;

public class Converter {

    public static BufferedImage convertSVGToPNG(String url) throws TranscoderException, IOException {
        ByteArrayOutputStream resultByteStream = new ByteArrayOutputStream();

        TranscoderInput transcoderInput = new TranscoderInput(url);
        TranscoderOutput transcoderOutput = new TranscoderOutput(resultByteStream);

        PNGTranscoder pngTranscoder = new PNGTranscoder();
        pngTranscoder.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, 256f);
        pngTranscoder.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, 256f);
        pngTranscoder.transcode(transcoderInput, transcoderOutput);

        resultByteStream.flush();

        return ImageIO.read(new ByteArrayInputStream(resultByteStream.toByteArray()));
    }

    public static void main(String args[]) throws TranscoderException, IOException {
        BufferedImage image = convertSVGToPNG("https://www.macstories.net/app/themes/macstories4/images/logo-shape-bw.svg");
        assert image.getWidth() == 256;
        assert image.getHeight() == 256;
    }
}

这是个例外

代码语言:javascript
运行
复制
Exception in thread "main" org.apache.batik.transcoder.TranscoderException: null
Enclosed Exception:
The current document is unable to create an element of the requested type (namespace: http://www.w3.org/2000/svg, name: description).
        at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)
        at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)
        at com.stackoverflow.batik.Converter.convertSVGToPNG(Converter.java:25)
        at com.stackoverflow.batik.Converter.main(Converter.java:33)

我做错了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-22 16:02:42

SVG没有描述标签。我想你是想写<desc> 它确实存在

Chrome和其他浏览器忽略未知的标签。蜡染也应该,但不是。在将描述标记交给Batik之前,可以使用XSLT转换(或其他机制)将description标记替换为desc标记。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32721467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档