Convert svg to png java

Asked

Viewed 502 times

5

I am trying to perform an SGV image conversion to PNG, but am having some problems.

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
  • 1

    I believe the problem is in the path of the image passed which may be with the wrong formatting or the path may be incomplete.

1 answer

2

As told by Dener, it seems your problem is way to the file.

I tested your program and it worked with full paths, but it didn’t work with the "/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 for:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.