JAVA: SVG to JPG converter

mohammad picture mohammad · Mar 6, 2013 · Viewed 15.2k times · Source

is there any libraries to convert SVG to JPG ? else than BATIK ? i used BATIK but i found some problems like this exception :

java.lang.NullPointerException at org.apache.batik.css.engine.CSSEngine.getCascadedStyleMap(CSSEngine.java:684) at org.apache.batik.css.engine.CSSEngine.getComputedStyle(CSSEngine.java:755) at org.apache.batik.bridge.CSSUtilities.getComputedStyle(CSSUtilities.java:96) at org.apache.batik.bridge.CSSUtilities.convertDisplay(CSSUtilities.java:509) at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:176) at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:148) at org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:76) at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:165) at org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscoder.java:86) at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:132)

this is my code :

          JPEGTranscoder transcoder = new JPEGTranscoder();

      transcoder.addTranscodingHint(JPEGTranscoder.KEY_XML_PARSER_CLASSNAME,
          "org.apache.crimson.parser.XMLReaderImpl");
      transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
          new Float(1.0));

      TranscoderInput input = new TranscoderInput(new FileInputStream("C:/Taha/SmartPlannerNew/rectangles.svg"));
      OutputStream ostream = new FileOutputStream("C:/Taha/SmartPlannerNew/out.jpg");
      TranscoderOutput output = new TranscoderOutput(ostream);

      try {
        transcoder.transcode(input, output);
    } catch (TranscoderException e) { 
        System.out.println("error***************************************************************************************************************************");
        e.printStackTrace();
    }
      System.out.println("done.");
      ostream.close();
      System.exit(0);

can any one help me please ?

Answer

trognanders picture trognanders · Mar 6, 2013

Batik is exactly what you are looking for, is totally stable, and there really is not much else.

You can read the handy official tutorial on using the transcoder portion of batik: http://xmlgraphics.apache.org/batik/using/transcoder.html

If you are still having trouble, try posting the code that you are using that causes the error, and a complete stack trace. Somebody here will be able to help you.