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);
	}
}

来看看转换后的文件


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值