@HUST-SuWB
2016-02-24T07:05:24.000000Z
字数 2870
阅读 1076
项目实战
需求很简单,我在做PDF导出的时候,需要再PDF文档中插入前端界面上的图片,一种典型的解决方案就是前端把绘图时使用的SVG代码传到后台,然后我再用这些SVG把图片在本地绘制出来插到PDF里去。网上的参考代码基本上都一样,我贴一下
import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;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;/*** 测试Batik操作SVG转PNG图片* @author suwb**/public class Batik {public static void main(String[] args) throws Exception {File file = new File("C:\\Users\\suwb\\Downloads");File[] files = file.listFiles();for (File f : files){String filename = f.getName();if (filename.endsWith("svg")){System.out.println(filename);String name = filename.substring(0,filename.length() - 4)+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".png";BufferedReader br = new BufferedReader(new FileReader(f));ArrayList<String> strs = new ArrayList<String>();String s = null;while ((s = br.readLine()) != null){strs.add(s);}StringBuilder sb = new StringBuilder();for (int i = 0 ;i < strs.size(); i++){sb.append(strs.get(i));if ( i != strs.size() -1){sb.append("\n");}}convertToPng(sb.toString(),"C:\\Users\\suwb\\Downloads\\"+name);}}}/***@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 ();}}}}}
之所以我觉得需要重新贴一篇博客来说明这件事情,是因为如果你直接在网上去找资料把Batik的jar包引进来,然后跑这段代码会报错,原因是缺少另外一个jar包,我查了好久,才在stackoverflow上看到说这是一个bug,官方写漏了jar包依赖。
所以,我在强调一下,svg转png(jpg)除了上面那段代码,还需要xml-apis-ext.jar和batik-all-1.7.jar连个jar包。
P.S. 测试用的svg代码自己去找哈,给几个参考资料,肯定有用:
http://blog.csdn.net/zwx19921215/article/details/34442593
http://www.cnblogs.com/wanggd/p/3529804.html
http://stackoverflow.com/questions/7891250/batik-not-in-classpath