将SVGGraphics2D转码为PNG时出错

时间:2014-04-14 13:50:02

标签: java svg batik

我试图旋转SVGGraphics2D,然后将其导出为PNG格式,我遇到了问题。你能救我吗?

    Exception in thread "main" org.apache.batik.transcoder.TranscoderException: null
    Enclosed Exception:
   The "xmlns" attribute was already specified for element "svg".
        at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:136)
        at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)
        at inclan.icon.svg.rasterizer.SVGRasterizer.transcodeToPNG(SVGRasterizer.java:382)
        at inclan.icon.svg.rasterizer.SVGRasterizer.main(SVGRasterizer.java:370)

我的代码:

protected TranscoderInput input;
protected TranscoderOutput output;

 public static void main(String [] args) throws Exception {

        SVGGraphics2D gra = getSVGGraphics2D(new ByteArrayInputStream(getXML().getBytes()));
        tilt(gra);

        FileOutputStream ops = new FileOutputStream(new File("C://Users//pablosouza//Desktop//svg//asf-logo-png.png"));

        SVGRasterizer r = new SVGRasterizer(getInputStream(gra), ops);

        r.transcodeToPNG();
    }



public static SVGGraphics2D getSVGGraphics2D(InputStream iStream){

    SVGGraphics2D svg = null;

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document parse = builder.parse(iStream);

        SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(parse);
        svg = new SVGGraphics2D(ctx,false);

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return svg;
}



 public SVGRasterizer(InputStream inputStream, FileOutputStream ops) {
    this.input = new TranscoderInput(inputStream);
    this.output = new TranscoderOutput(ops);
}

 private void transcodeToPNG() throws TranscoderException {
    PNGTranscoder png = new PNGTranscoder();
    png.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, 100f);
    png.addTranscodingHint(PNGTranscoder.KEY_WIDTH, 100f);
    png.transcode(input, output);

}



 private static String getXML() {
    StringBuilder sb = new StringBuilder();

    sb.append("<?xml version=\"1.0\"?>");
    sb.append("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" ");
    sb.append("\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> ");
    sb.append("<svg width=\"10cm\" height=\"8cm\" version=\"1.1\" ");
    sb.append("xmlns=\"http://www.w3.org/2000/svg\"> ");
    sb.append("<desc>Our first SVG document</desc> ");
    sb.append("<!-- This is a rectangle -->");
    sb.append("<rect x=\"1cm\" y=\"1cm\" width=\"4cm\" height=\"3cm\" ");
    sb.append("fill=\"moccasin\" stroke=\"midnightblue\" ");
    sb.append("stroke-width=\"0.2cm\"/> ");
    sb.append("<!-- This is a circle -->");
    sb.append("<circle cx=\"6cm\" cy=\"5cm\" r=\"2.5cm\" fill=\"lavender\" ");
    sb.append("stroke=\"firebrick\" stroke-width=\"0.15cm\" ");
    sb.append("opacity=\"0.7\"/> ");
    sb.append("</svg>");

    return sb.toString();
}

0 个答案:

没有答案