java将svg图片转换为png格式图片 博客分类: java
有时候在客户端使用svg画图,而在服务器端需要同样的图片,在服务器端重新画一遍是非常费事的。这时候我们就可以利用已有的svg直接通过下面的类转换成png格式。
使用这个方法需要引用batic相关的包,maven pom文件如下:
<!-- svg 生成png格式图片 --><dependency><groupId>batik</groupId><artifactId>batik-svggen</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-awt-util</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-bridge</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-css</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-dom</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-gvt</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-parser</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-script</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-svg-dom</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-transcoder</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-util</artifactId><version>1.6</version></dependency><dependency><groupId>batik</groupId><artifactId>batik-xml</artifactId><version>1.6</version></dependency><!-- 此处不能使用2.9.1版本,使用2.9.1生成png会失败 --><dependency><groupId>xerces</groupId><artifactId>xercesImpl</artifactId><version>2.5.0</version></dependency><dependency><groupId>xml-apis</groupId><artifactId>xmlParserAPIs</artifactId><version>2.0.2</version></dependency><dependency><groupId>org.axsl.org.w3c.dom.svg</groupId><artifactId>svg-dom-java</artifactId><version>1.1</version></dependency><dependency><groupId>xml-apis</groupId><artifactId>xml-apis</artifactId><version>2.0.0</version></dependency><dependency><groupId>org.w3c.css</groupId><artifactId>sac</artifactId><version>1.3</version></dependency><!-- svg 生成png格式图片结束 -->
package cn.outofmemory.util;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;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;/**
* 将svg转换为png格式的图片
*
*
*/publicabstractclassSvgPngConverter{/**
* 将svg字符串转换为png
*
* @param svgCode svg代码
* @param pngFilePath 保存的路径
* @throws TranscoderException svg代码异常
* @throws IOException io错误
*/publicstaticvoid convertToPng(String svgCode,String pngFilePath)throwsIOException,TranscoderException{File file =newFile(pngFilePath);FileOutputStream outputStream =null;try{
file.createNewFile();
outputStream =newFileOutputStream(file);
convertToPng(svgCode, outputStream);}finally{if(outputStream !=null){try{
outputStream.close();}catch(IOException e){
e.printStackTrace();}}}}/**
* 将svgCode转换成png文件,直接输出到流中
*
* @param svgCode svg代码
* @param outputStream 输出流
* @throws TranscoderException 异常
* @throws IOException io异常
*/publicstaticvoid convertToPng(String svgCode,OutputStream outputStream)throwsTranscoderException,IOException{try{byte[] bytes = svgCode.getBytes("utf-8");PNGTranscoder t =newPNGTranscoder();TranscoderInput input =newTranscoderInput(newByteArrayInputStream(bytes));TranscoderOutput output =newTranscoderOutput(outputStream);
t.transcode(input, output);
outputStream.flush();}finally{if(outputStream !=null){try{
outputStream.close();}catch(IOException e){
e.printStackTrace();}}}}} http://m.blog.csdn.net/blog/hefeng_aspnet_2010/10227331 http://www.open-open.com/lib/view/open1414817090294.html