java将svg图片转换为png格式图片

有时候在客户端使用svg画图,而在服务器端需要同样的图片,在服务器端重新画一遍是非常费事的。这时候我们就可以利用已有的svg直接通过下面的类转换成png格式。

使用这个方法需要引用batic相关的包,maven pom文件如下:

<!-- svg 生成png格式图片  -->
<dependency><groupId>batik</groupId><artifactId>batik-svggen</artifactId><version>1.6</version></dependency>        
<dependency><groupId>batik</groupId><artifactId>batik-awt-util</artifactId><version>1.6</version></dependency>
<dependency><groupId>batik</groupId><artifactId>batik-bridge</artifactId><version>1.6</version></dependency>
<dependency><groupId>batik</groupId><artifactId>batik-css</artifactId><version>1.6</version></dependency>
<dependency><groupId>batik</groupId><artifactId>batik-dom</artifactId><version>1.6</version></dependency>
<dependency><groupId>batik</groupId><artifactId>batik-gvt</artifactId><version>1.6</version></dependency>
<dependency><groupId>batik</groupId><artifactId>batik-parser</artifactId><version>1.6</version></dependency>
<dependency><groupId>batik</groupId><artifactId>batik-script</artifactId><version>1.6</version></dependency>
<dependency><groupId>batik</groupId><artifactId>batik-svg-dom</artifactId><version>1.6</version></dependency>
<dependency><groupId>batik</groupId><artifactId>batik-transcoder</artifactId><version>1.6</version></dependency>
<dependency><groupId>batik</groupId><artifactId>batik-util</artifactId><version>1.6</version></dependency>
<dependency><groupId>batik</groupId><artifactId>batik-xml</artifactId><version>1.6</version></dependency>
<!-- 此处不能使用2.9.1版本,使用2.9.1生成png会失败 -->
<dependency><groupId>xerces</groupId><artifactId>xercesImpl</artifactId><version>2.5.0</version></dependency>
<dependency><groupId>xml-apis</groupId><artifactId>xmlParserAPIs</artifactId><version>2.0.2</version></dependency>
<dependency><groupId>org.axsl.org.w3c.dom.svg</groupId><artifactId>svg-dom-java</artifactId><version>1.1</version></dependency>
<dependency><groupId>xml-apis</groupId>    <artifactId>xml-apis</artifactId><version>2.0.0</version></dependency>
<dependency><groupId>org.w3c.css</groupId> <artifactId>sac</artifactId>    <version>1.3</version></dependency>
<!-- svg 生成png格式图片结束  -->
package com.yhb.web.util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
public final class FileUtil {
/** 
     *@Description: 将svg字符串转换为png 
     *@Author: 
     *@param svgCode svg代码 
     *@param pngFilePath  保存的路径 
     *@throws IOException io异常 
     *@throws TranscoderException svg代码异常 
    */  
public static void convertToPng(String svgCode,String pngFilePath) throws IOException,TranscoderException{  
 
        File file = new File (pngFilePath);  
  
        FileOutputStream outputStream = null;  
        try {  
            file.createNewFile ();  
            outputStream = new FileOutputStream (file);  
            convertToPng (svgCode, outputStream);  
        } finally {  
            if (outputStream != null) {  
                try {  
                    outputStream.close ();  
                } catch (IOException e) {  
                    e.printStackTrace ();  
                }  
            }  
        }  
    }  
/** 
     *@Description: 将svgCode转换成png文件,直接输出到流中 
     *@param svgCode svg代码 
     *@param outputStream 输出流 
     *@throws TranscoderException 异常 
     *@throws IOException io异常 
     */  
    public static void convertToPng(String svgCode,OutputStream outputStream) throws TranscoderException,IOException{  
        try {  
        byte[] bytes = svgCode.getBytes ("UTF-8");  
            PNGTranscoder t = new PNGTranscoder ();  
            TranscoderInput input = new TranscoderInput (new ByteArrayInputStream (bytes));  
            TranscoderOutput output = new TranscoderOutput (outputStream);  
            t.transcode (input, output);  
            outputStream.flush ();  
        } finally {  
            if (outputStream != null) {  
                try {  
                    outputStream.close ();  
                } catch (IOException e) {  
                    e.printStackTrace ();  
                }  
            }  
        }  
    }  
}

你可能感兴趣的:(java将svg图片转换为png格式图片)