zoukankan      html  css  js  c++  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);
    	}
    }
    


    来看看转换后的文件


  • 相关阅读:
    SQL 中单引号 和一些特殊字符的处理
    jquery 删除table行,该如何解决
    jQuery获取Select选中的Text和Value,根据Value值动态添加属性等
    C#中DataTable
    jquery操作select(取值,设置选中)
    JS刷新页面总和!多种JS刷新页面代码!
    VS中代码对齐等快捷键
    SQL递归查询(with cte as)
    SQL Server 公用表表达式(CTE)实现递归的方法
    linux ls和 ll 命令
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3359847.html
Copyright © 2011-2022 走看看