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 convertSvgFile2PdfFile svg, File pdf) throws IOException,TranscoderException 
	{
		InputStream in = new FileInputStreamsvg);
		OutputStream out = new FileOutputStreampdf);
		out = new BufferedOutputStreamout);
		convert2Pdfin, out);
	}
	public static void convert2PdfInputStream in, OutputStream out)throws IOException, TranscoderException
	{
		Transcoder transcoder = new PDFTranscoder);
		try {
			TranscoderInput input = new TranscoderInputin);
			try {
				TranscoderOutput output = new TranscoderOutputout);
				transcoder.transcodeinput, output);
			} finally {
				out.close);
			}
		} finally {
			in.close);
		}
	}
	//svg转为png
	public static void convertSvgFile2PngFile svg, File pdf) throws IOException,TranscoderException 
	{
		InputStream in = new FileInputStreamsvg);
		OutputStream out = new FileOutputStreampdf);
		out = new BufferedOutputStreamout);
		convert2PNGin, out);
	}
	public static void convert2PNGInputStream in, OutputStream out)throws IOException, TranscoderException
	{
		Transcoder transcoder = new PNGTranscoder);
		try {
			TranscoderInput input = new TranscoderInputin);
			try {
				TranscoderOutput output = new TranscoderOutputout);
				transcoder.transcodeinput, output);
			} finally {
				out.close);
			}
		} finally {
			in.close);
		}
	}
	//字符串转成pdf
	public static void convertStr2PdfString svg, File pdf) throws IOException,TranscoderException 
	{
		InputStream in = new ByteArrayInputStreamsvg.getBytes));
		OutputStream out = new FileOutputStreampdf);
		out = new BufferedOutputStreamout);
		convert2Pdfin, out);
	}
	
}

2.下面准备svg文件,这种文件可以到开发包的samples目录下面找,使用方法很简单如下所示

package org.lxh;

import java.io.File;
public class ConvertSvg {
	public static void mainString[] args) throws Exception {
		//注:使用的是svg字符串转pdf的情况可能会出现编码错误的异常,就把字符串里的UTF-8替换为GBK
        File f=new File"src/sun.svg");
        File destFile=new File"src/sun.pdf");
		SvgUtil.convertSvgFile2Pdff, destFile);
	}
}

来看看转换后的文件

Published by

风君子

独自遨游何稽首 揭天掀地慰生平