当前位置:编程学习 > JAVA >>

java使用batik转换svg文件

svg是一种矢量图片格式,用来保存高保真的图片。我们可以用编辑器打开svg,我们可以看到svg文件其实就是一个xml文件,这种文件浏览器也可以识别。因此要查看svg用现成的浏览器就可以了。值得庆幸的是java也提供了开发包来转换svg文件,目前用的最多的是svg转pdf,jpg,png.这个开发工具包就是batik,需要的话可以直接到百度输入batik下载。
 
下面分享下我最近使用batik的心得
 
1.为了方便以后使用,我把转换的操作写成了一个工具类,如下所示
 
开发之前要导入batik的lib目录下面的所有jar包引入到项目中
 
 
package org.lxh;  
  
import java.io.BufferedOutputStream;  
import java.io.ByteArrayInputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import org.apache.batik.transcoder.Transcoder;  
import org.apache.batik.transcoder.TranscoderException;  
import org.apache.batik.transcoder.TranscoderInput;  
import org.apache.batik.transcoder.TranscoderOutput;  
import org.apache.fop.svg.PDFTranscoder;  
import org.apache.batik.transcoder.image.PNGTranscoder;  
  
/** 
 * svg转换工具类(以下方法开发足够用了) 
 * @param svg 
 * @param pdf 
 * @throws IOException 
 * @throws TranscoderException 
 */  
public class SvgUtil {  
    //svg文件转成  
    public static void convertSvgFile2Pdf(File svg, File pdf) throws IOException,TranscoderException   
    {  
        InputStream in = new FileInputStream(svg);  
        OutputStream out = new FileOutputStream(pdf);  
        out = new BufferedOutputStream(out);  
        convert2Pdf(in, out);  
    }  
    public static void convert2Pdf(InputStream in, OutputStream out)throws IOException, TranscoderException  
    {  
        Transcoder transcoder = new PDFTranscoder();  
        try {  
            TranscoderInput input = new TranscoderInput(in);  
            try {  
                TranscoderOutput output = new TranscoderOutput(out);  
                transcoder.transcode(input, output);  
            } finally {  
                out.close();  
            }  
        } finally {  
            in.close();  
        }  
    }  
    //svg转为png  
    public static void convertSvgFile2Png(File svg, File pdf) throws IOException,TranscoderException   
    {  
        InputStream in = new FileInputStream(svg);  
        OutputStream out = new FileOutputStream(pdf);  
        out = new BufferedOutputStream(out);  
        convert2PNG(in, out);  
    }  
    public static void convert2PNG(InputStream in, OutputStream out)throws IOException, TranscoderException  
    {  
        Transcoder transcoder = new PNGTranscoder();  
        try {  
            TranscoderInput input = new TranscoderInput(in);  
            try {  
                TranscoderOutput output = new TranscoderOutput(out);  
                transcoder.transcode(input, output);  
            } finally {  
                out.close();  
            }  
        } finally {  
            in.close();  
        }  
    }  
    //字符串转成pdf  
    public static void convertStr2Pdf(String svg, File pdf) throws IOException,TranscoderException   
    {  
        InputStream in = new ByteArrayInputStream(svg.getBytes());  
        OutputStream out = new FileOutputStream(pdf);  
        out = new BufferedOutputStream(out);  
        convert2Pdf(in, out);  
    }  
      
}  

 

 
2.下面准备svg文件,这种文件可以到开发包的samples目录下面找,使用方法很简单如下所示
 
package org.lxh;  
  
import java.io.File;  
public class ConvertSvg {  
    public static void main(String[] args) throws Exception {  
        //注:使用的是svg字符串转pdf的情况可能会出现编码错误的异常,就把字符串里的UTF-8替换为GBK  
        File f=new File("src/sun.svg");  
        File destFile=new File("src/sun.pdf");  
        SvgUtil.convertSvgFile2Pdf(f, destFile);  
    }  
}  

 

来看看转换后的文件
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,