Swing & Batik: Create an ImageIcon from an SVG file?

前端 未结 5 2190
我寻月下人不归
我寻月下人不归 2021-01-31 19:35

Simply put, I\'m looking for a way to make an ImageIcon from an SVG file using the batik library. I don\'t want to have to raster the SVG to disk first, I just want to be able

5条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 20:01

    I have just followed Devon's approach with Batik-1.7

    However, in order to make it work I had to make the following additions to the hints object:

    MyTranscoder transcoder =new MyTranscoder()
    
    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    TranscodingHints hints = new TranscodingHints();
    hints.put(ImageTranscoder.KEY_WIDTH, width); // e.g. width=new Float(300)
    hints.put(ImageTranscoder.KEY_HEIGHT,height);// e.g. height=new Float(75)
    hints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, impl.getDOMImplementation());
    hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,SVGConstants.SVG_NAMESPACE_URI);
    hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,SVGConstants.SVG_NAMESPACE_URI);
    hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, SVGConstants.SVG_SVG_TAG);
    hints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, false);
    
    transcoder.setTranscodingHints(hints);
    TranscoderInput ti=new TranscoderInput(uri)
    transcoder.transcode(ti, null);
    BufferedImage image = transcoder.getImage();
    

    Seems like something has been updated in batik's XMLAbstractTranscoder( http://svn.apache.org/repos/asf/xmlgraphics/batik/tags/batik-1_7/sources/org/apache/batik/transcoder/XMLAbstractTranscoder.java) with version 1.7.

提交回复
热议问题