我从 apache png 转码器得到一个 org.apache.batik.bridge.BridgeException
I get an org.apache.batik.bridge.BridgeException from apache png transcoder
我想使用以下代码从 svg 字符串生成 png:
try {
PNGTranscoder coder = new PNGTranscoder();
StringReader reader = new StringReader(getSVG());
TranscoderInput input = new TranscoderInput(reader);
FileOutputStream outputStream = new FileOutputStream("tmp/tmp.png");
TranscoderOutput transcoderOutput = new TranscoderOutput(outputStream);
try{
coder.transcode(input, transcoderOutput);
}finally{
reader.close();
outputStream.close();
}
} catch (Exception e) {
ErrorHandler.getInstance().handle(e);
}
但是如果我 运行 代码,我会得到一个异常,因为 uri 因损坏或不受支持而无法打开:
org.apache.batik.bridge.BridgeException: null:0
The URI "https://cdn.discordapp.com/avatars/xxx/xxx.png"
on element <image> can't be opened because:
JDK URL is corrupt or unsupported variant
at org.apache.batik.bridge.UserAgentAdapter.getBrokenLinkDocument(UserAgentAdapter.java:449)
at org.apache.batik.bridge.SVGImageElementBridge.createRasterImageNode(SVGImageElementBridge.java:604)
at org.apache.batik.bridge.SVGImageElementBridge.createImageGraphicsNode(SVGImageElementBridge.java:327)
at org.apache.batik.bridge.SVGImageElementBridge.buildImageGraphicsNode(SVGImageElementBridge.java:177)
at org.apache.batik.bridge.SVGImageElementBridge.createGraphicsNode(SVGImageElementBridge.java:119)
[...]
在转换此 svg 时:https://hastebin.com/mamudofira.xml
图像 url 已为保护隐私而被替换,但它有效!
我自己修复了这个问题,首先将图像下载为 base64 字符串,然后在 svg 中将其设置为 xlink:href
。
我想使用以下代码从 svg 字符串生成 png:
try {
PNGTranscoder coder = new PNGTranscoder();
StringReader reader = new StringReader(getSVG());
TranscoderInput input = new TranscoderInput(reader);
FileOutputStream outputStream = new FileOutputStream("tmp/tmp.png");
TranscoderOutput transcoderOutput = new TranscoderOutput(outputStream);
try{
coder.transcode(input, transcoderOutput);
}finally{
reader.close();
outputStream.close();
}
} catch (Exception e) {
ErrorHandler.getInstance().handle(e);
}
但是如果我 运行 代码,我会得到一个异常,因为 uri 因损坏或不受支持而无法打开:
org.apache.batik.bridge.BridgeException: null:0
The URI "https://cdn.discordapp.com/avatars/xxx/xxx.png"
on element <image> can't be opened because:
JDK URL is corrupt or unsupported variant
at org.apache.batik.bridge.UserAgentAdapter.getBrokenLinkDocument(UserAgentAdapter.java:449)
at org.apache.batik.bridge.SVGImageElementBridge.createRasterImageNode(SVGImageElementBridge.java:604)
at org.apache.batik.bridge.SVGImageElementBridge.createImageGraphicsNode(SVGImageElementBridge.java:327)
at org.apache.batik.bridge.SVGImageElementBridge.buildImageGraphicsNode(SVGImageElementBridge.java:177)
at org.apache.batik.bridge.SVGImageElementBridge.createGraphicsNode(SVGImageElementBridge.java:119)
[...]
在转换此 svg 时:https://hastebin.com/mamudofira.xml
图像 url 已为保护隐私而被替换,但它有效!
我自己修复了这个问题,首先将图像下载为 base64 字符串,然后在 svg 中将其设置为 xlink:href
。