[英]SVG to PNG - Batik
When converting from SVG to PNG with Apache Batik I sometimes get strange errors. 使用Apache Batik从SVG转换为PNG时,有时会出现奇怪的错误。 For example for this SVG https://www.macstories.net/app/themes/macstories4/images/logo-shape-bw.svg it throws an exception, but not for this https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg
例如,对于此SVG https://www.macstories.net/app/themes/macstories4/images/logo-shape-bw.svg,它会引发异常,但对于此https://upload.wikimedia.org/wikipedia不会/commons/0/02/SVG_logo.svg
Here is my code: 这是我的代码:
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;
}
}
And this is the exception 这是例外
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)
Am I doing something wrong? 难道我做错了什么?
SVG has no description tag. SVG没有描述标签。 I assume you meant to write
<desc>
which does exist . 我假设您要编写确实存在的
<desc>
。
Chrome and other browsers ignore unknown tags. Chrome和其他浏览器会忽略未知标签。 Batik should too, but does not.
蜡染也应该,但不是。 You could always replace the description tag by a desc tag using an XSLT transform (or some other mechanism) prior to giving it to Batik.
在将其提供给Batik之前,您始终可以使用XSLT转换(或其他某种机制)将desc标签替换为desc标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.