How to get a BufferedImage from a SVG?

前端 未结 3 2042
梦毁少年i
梦毁少年i 2020-12-13 20:35

I am using Batik to handle SVG images. Is there any way to get a java.awt.image.BufferedImage from a SVG-file?

I know there are transcoders, with which I could trans

3条回答
  •  没有蜡笔的小新
    2020-12-13 21:03

    A very easy way is to use the TwelveMonkeys lib which adds additional image type support to java's ImageIO

    So for example you just add these to your maven (or copy the needed jars):

        
            com.twelvemonkeys.imageio
            imageio-batik 
            3.2.1
        
        
            batik
            batik-transcoder
            1.6-1
        
    

    And then you just read it with

    BufferedImage image = ImageIO.read(svg-file);
    

    To check if the svg reader is registered correctly you could print out the image readers:

    Iterator readers = ImageIO.getImageReadersByFormatName("SVG");
    while (readers.hasNext()) {
        System.out.println("reader: " + readers.next());
    }
    

    The lib also supports additional params, see readme on github.

提交回复
热议问题