有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

转换SVG时Batik出现java错误:<use>元素的属性“xlink:href”是必需的

我有一个SVG文件,结构如下:

<svg xmlns="http://www.w3.org/2000/svg" width="506px" height="261px" viewBox="0 0 506 261" style="overflow: hidden; display: block; width: 506px; height: 261px;">
   <defs>
      <path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_0" />
      <path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_1" />
      <path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_2" />
      <path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_3" />
      <path fill="none" stroke="rgb(211,211,211)" d="M 5,-5 L 0,0 L 5,5" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" id="ygc10_4" />
   </defs>
   <g style="pointer-events:visiblePainted" transform="translate(-57.822952932363535 -41.05614567526554)" image-rendering="auto" shape-rendering="auto">
      <g>
         <path fill="none" stroke="rgb(211,211,211)" d="M 257.9119715596548,73.05614567526554 L 257.9119715596548,135.06828972638465" stroke-opacity="1" stroke-width="2" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" />
         <use href="#ygc10_0" transform="matrix(0 -2 2 0 257.912 135.068)" style="pointer-events: none;" />
      </g>
      <g>
         <path fill="none" stroke="rgb(211,211,211)" d="M 107.82295293236353,207.4377203121161 L 107.82295293236353,261.03014731718906" stroke-opacity="1" stroke-width="2" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="10" />
         <use href="#ygc10_1" transform="matrix(0 -2 2 0 107.823 261.03)" style="pointer-events: none;" />
      </g>
...

我试图从它的{{CD1}}中生成一个PNG,像这样(请不要考虑现在没有例外的处理):

static ByteArrayOutputStream svgToPng(ByteArrayInputStream streamBytes) throws TranscoderException, IOException {
    PNGTranscoder t = new PNGTranscoder();

    TranscoderInput input = new TranscoderInput(streamBytes);
    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
    TranscoderOutput output = new TranscoderOutput(ostream);

    t.transcode(input, output);

    ostream.flush();
    return ostream;
}

但是,我在transcode()的调用中遇到了一个例外:

org.apache.batik.bridge.BridgeException: null:-1
The attribute "xlink:href" of the element <use> is required
    at org.apache.batik.bridge.SVGUseElementBridge.buildCompositeGraphicsNode(Unknown Source)
    at org.apache.batik.bridge.SVGUseElementBridge.createGraphicsNode(Unknown Source)
    at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(Unknown Source)
    at org.apache.batik.bridge.GVTBuilder.buildComposite(Unknown Source)
    at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(Unknown Source)
    at org.apache.batik.bridge.GVTBuilder.buildComposite(Unknown Source)
...

你能帮帮我吗?在我的PNGTranscoder声明之后,我还尝试:

t.addTranscodingHint(SVGAbstractTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,
                SVGDOMImplementation.SVG_NAMESPACE_URI);

但没有效果。谢谢大家


共 (0) 个答案