[关闭]
@HUST-SuWB 2016-02-24T07:05:24.000000Z 字数 2870 阅读 969

Java通过Batik操作SVG生成本地图片(半转载)

项目实战


需求很简单,我在做PDF导出的时候,需要再PDF文档中插入前端界面上的图片,一种典型的解决方案就是前端把绘图时使用的SVG代码传到后台,然后我再用这些SVG把图片在本地绘制出来插到PDF里去。网上的参考代码基本上都一样,我贴一下

  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.OutputStream;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import org.apache.batik.transcoder.TranscoderException;
  12. import org.apache.batik.transcoder.TranscoderInput;
  13. import org.apache.batik.transcoder.TranscoderOutput;
  14. import org.apache.batik.transcoder.image.PNGTranscoder;
  15. /**
  16. * 测试Batik操作SVG转PNG图片
  17. * @author suwb
  18. *
  19. */
  20. public class Batik {
  21. public static void main(String[] args) throws Exception {
  22. File file = new File("C:\\Users\\suwb\\Downloads");
  23. File[] files = file.listFiles();
  24. for (File f : files){
  25. String filename = f.getName();
  26. if (filename.endsWith("svg")){
  27. System.out.println(filename);
  28. String name = filename.substring(0,filename.length() - 4)+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".png";
  29. BufferedReader br = new BufferedReader(new FileReader(f));
  30. ArrayList<String> strs = new ArrayList<String>();
  31. String s = null;
  32. while ((s = br.readLine()) != null)
  33. {
  34. strs.add(s);
  35. }
  36. StringBuilder sb = new StringBuilder();
  37. for (int i = 0 ;i < strs.size(); i++){
  38. sb.append(strs.get(i));
  39. if ( i != strs.size() -1){
  40. sb.append("\n");
  41. }
  42. }
  43. convertToPng(sb.toString(),"C:\\Users\\suwb\\Downloads\\"+name);
  44. }
  45. }
  46. }
  47. /**
  48. *@Description: 将svg字符串转换为png
  49. *@Author:
  50. *@param svgCode svg代码
  51. *@param pngFilePath 保存的路径
  52. *@throws IOException io异常
  53. *@throws TranscoderException svg代码异常
  54. */
  55. public static void convertToPng(String svgCode,String pngFilePath) throws IOException, TranscoderException{
  56. File file = new File (pngFilePath);
  57. FileOutputStream outputStream = null;
  58. try {
  59. file.createNewFile ();
  60. outputStream = new FileOutputStream (file);
  61. convertToPng (svgCode, outputStream);
  62. } finally {
  63. if (outputStream != null) {
  64. try {
  65. outputStream.close ();
  66. } catch (IOException e) {
  67. e.printStackTrace ();
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. *@Description: 将svgCode转换成png文件,直接输出到流中
  74. *@param svgCode svg代码
  75. *@param outputStream 输出流
  76. *@throws TranscoderException 异常
  77. *@throws IOException io异常
  78. */
  79. public static void convertToPng(String svgCode, OutputStream outputStream) throws TranscoderException,IOException{
  80. try {
  81. byte[] bytes = svgCode.getBytes ("UTF-8");
  82. PNGTranscoder t = new PNGTranscoder ();
  83. TranscoderInput input = new TranscoderInput (new ByteArrayInputStream (bytes));
  84. TranscoderOutput output = new TranscoderOutput (outputStream);
  85. t.transcode (input, output);
  86. outputStream.flush ();
  87. } finally {
  88. if (outputStream != null) {
  89. try {
  90. outputStream.close ();
  91. } catch (IOException e) {
  92. e.printStackTrace ();
  93. }
  94. }
  95. }
  96. }
  97. }

之所以我觉得需要重新贴一篇博客来说明这件事情,是因为如果你直接在网上去找资料把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

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注