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

前端 未结 5 2179
我寻月下人不归
我寻月下人不归 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条回答
  •  隐瞒了意图╮
    2021-01-31 20:06

    It's really quite easy, just not very intuitive.

    You need to extend ImageTranscoder. In the createImage method you allocate a BufferedImage, cache it as a member variable, and return it. The writeImage method is empty. And you'll need to add a getter to retrieve the BufferedImage.

    It will look something like this:

        class MyTranscoder extends ImageTranscoder {
            private BufferedImage image = null;
            public BufferedImage createImage(int w, int h) {
                image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
                return image;
            }
            public void writeImage(BufferedImage img, TranscoderOutput out) {
            }
            public BufferedImage getImage() {
                return image;
            }
        }
    

    Now, to create an image you create an instance of your transcoder and pass it the desired width and height by setting TranscodingHints. Finally you transcode from a TranscoderInput to a null target. Then call the getter on your transcoder to obtain the image.

    The call looks something like this:

        MyTranscoder transcoder = new MyTranscoder();
        TranscodingHints hints = new TranscodingHints();
        hints.put(ImageTranscoder.KEY_WIDTH, width);
        hints.put(ImageTranscoder.KEY_HEIGHT, height);
        transcoder.setTranscodingHints(hints);
        transcoder.transcode(new TranscoderInput(url), null);
        BufferedImage image = transcoder.getImage();
    

    Simple, right? (Yeah, right. Only took me 2 weeks to figure that out. Sigh.)

提交回复
热议问题