Convert svg to java png

5

I'm trying to do an SGV image conversion to PNG, but I'm having some issues.

My code:

   String svg_URI_input = Paths.get("/home/ivan/Documentos/imagem/Svg2.svg").toUri().toURL().toString();
    TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);        



    OutputStream png_ostream = new FileOutputStream("/home/ivan/Documentos/imagem/tica.png");
    TranscoderOutput output_png_image = new TranscoderOutput(png_ostream);              


    // Step-3: Create PNGTranscoder and define hints if required
    PNGTranscoder my_converter = new PNGTranscoder();        


    // Step-4: Convert and Write output
    my_converter.transcode(input_svg_image, output_png_image);


    // Step 5- close / flush Output Stream
    png_ostream.flush();
    png_ostream.close();       

}

Exception:

on element <image> can't be opened because:
URL data in unsupported format or corrupt
    at org.apache.batik.bridge.UserAgentAdapter.getBrokenLinkDocument(Unknown Source)
    at org.apache.batik.bridge.SVGImageElementBridge.createRasterImageNode(Unknown Source)
    at org.apache.batik.bridge.SVGImageElementBridge.createImageGraphicsNode(Unknown Source)
    at org.apache.batik.bridge.SVGImageElementBridge.buildImageGraphicsNode(Unknown Source)
    at org.apache.batik.bridge.SVGImageElementBridge.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.build(Unknown Source)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.image.ImageTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)
    at convertsvgtopng.ConvertSvgToPng.main(ConvertSvgToPng.java:46)
Exception in thread "main" org.apache.batik.transcoder.TranscoderException: null
Enclosed Exception:
file:/home/ivan/Documentos/imagem/Svg2.svg:0

    on element <image> can't be opened because:
URL data in unsupported format or corrupt
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.image.ImageTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)
    at convertsvgtopng.ConvertSvgToPng.main(ConvertSvgToPng.java:46)
Java Result: 1
    
asked by anonymous 02.07.2015 / 14:29

1 answer

2

As said by Dener, it seems that your problem is path to the file.

I tested your program and it worked with full paths, but it did not work with "/home" .

You can try changing the input to:

String svg_URI_input = Paths.get(System.getProperty("user.home")+"/Documentos/imagem/Svg2.svg").toUri().toURL().toString();

And the output to:

OutputStream png_ostream = new FileOutputStream(System.getProperty("user.home")+"/Documentos/imagem/tica.png");

The sources for this answer have been found in related questions in stackoverflow in English - here and here .

    
25.07.2015 / 22:58