如何将SVG转换为png或jpg

时间:2014-12-03 10:45:56

标签: java image-processing svg batik image-conversion

我尝试过使用蜡染。但是我得到了空的png文件。我还包括了所有必需的罐子。

  

我的代码是

import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;

public class TestSVG {
    String filePath="";
    TestSVG(String filePath) throws Exception {
        this.filePath=filePath;
                createImage();
    }


    public void createImage() throws Exception{
        String svg_URI_input = new File("test.svg").toURL().toString();
            TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);        
            //Step-2: Define OutputStream to PNG Image and attach to TranscoderOutput
            OutputStream png_ostream = new FileOutputStream("chessboard.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
            System.out.println("It will print");
            my_converter.transcode(input_svg_image, output_png_image);
            System.out.println("It will not print");
            png_ostream.flush();
            png_ostream.close();        
    }
}

请在代码中输入sysout。到第3步它工作正常。

2 个答案:

答案 0 :(得分:0)

您必须使用转码提示。 例如,对于PNG,您应该设置要使用的像素。

my_converter.addTranscodingHint(PNGTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER,0.084672F); my_converter.addTranscodingHint(PNGTranscoder.KEY_BACKGROUND_COLOR, Color.WHITE);

答案 1 :(得分:0)

使用您的代码,我可以生成PNG图片,

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;

public class TestSVG {
    String filePath="";
    TestSVG(String filePath) throws Exception {
       this.filePath=filePath;
            createImage();
    }


    public void createImage() throws Exception{
        String svg_URI_input = new File("asf-logo.svg").toURL().toString();
        TranscoderInput input_svg_image = new TranscoderInput(svg_URI_input);        
        //Step-2: Define OutputStream to PNG Image and attach to TranscoderOutput
        OutputStream png_ostream = new FileOutputStream(filePath);
        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
        System.out.println("It will print");
        my_converter.transcode(input_svg_image, output_png_image);
        System.out.println("It will not print");
        png_ostream.flush();
        png_ostream.close();        
}

public static void main(String[] args) throws Exception {
    TestSVG svg = new TestSVG("asf-logo.png");
}
}

附加enter image description here生成的png。