java svg转png
svg
转成png``java
实现简单介绍
简介
将svg
转成png
使用的是Java
提供的开发工具包batik
。
Maven
下载地址:
<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
//svg转为png
public static void convertSvg2Png(File svg, File png) throws IOException,TranscoderException
{
InputStream in = new FileInputStream(svg);
OutputStream out = new FileOutputStream(png);
out = new BufferedOutputStream(out);
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();
}
}
public static void main(String args[]){
File f=new File("E:/Pinterest_pinterest30.svg");
File destFile=new File("E:/Pinterest_pinterest30.png");
try {
convertSvg2Png(f, destFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TranscoderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
原svg:
png:
Other
这里我们也可以通过svg字符串转成图片。
Pinterest_pinterest30.svg
:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 22 22" style="enable-background:new 0 0 22 22;" xml:space="preserve">
<path style="fill-rule:evenodd;clip-rule:evenodd;" d="M11,0C4.925,0,0,4.925,0,11s4.925,11,11,11c6.075,0,11-4.925,11-11
S17.075,0,11,0z M12.012,14.331c-0.813,0-1.578-0.449-1.841-0.959c0,0-0.437,1.772-0.53,2.114c-0.156,0.578-0.457,1.155-0.736,1.612
c-0.398,0.52-0.857,0.134-0.912-0.34c-0.018-0.528,0.001-1.153,0.132-1.722c0.145-0.624,0.969-4.191,0.969-4.191
s-0.241-0.491-0.241-1.217c0-1.14,0.647-1.991,1.453-1.991c0.685,0,1.016,0.526,1.016,1.155c0,0.703-0.439,1.755-0.665,2.729
c-0.189,0.816,0.401,1.481,1.189,1.481c1.427,0,2.389-1.872,2.389-4.089c0-1.686-1.112-2.947-3.135-2.947
c-2.285,0-3.709,1.74-3.709,3.683c0,0.67,0.193,1.143,0.497,1.508c0.139,0.168,0.159,0.236,0.108,0.429
c-0.036,0.142-0.119,0.482-0.153,0.617c-0.05,0.195-0.205,0.264-0.377,0.192c-1.053-0.439-1.543-1.616-1.543-2.938
c0-2.185,1.805-4.805,5.384-4.805c2.876,0,4.769,2.125,4.769,4.406C16.077,12.077,14.434,14.331,12.012,14.331z"/>
</svg>
String svgCode ="<svg><path d='M11,0C4.925,0,0,4.925,0,11s4.925,11,11,11c6.075,0,11-4.925,11-11"+
"S17.075,0,11,0z M12.012,14.331c-0.813,0-1.578-0.449-1.841-0.959c0,0-0.437,1.772-0.53,2.114c-0.156,0.578-0.457,1.155-0.736,1.612"+
"c-0.398,0.52-0.857,0.134-0.912-0.34c-0.018-0.528,0.001-1.153,0.132-1.722c0.145-0.624,0.969-4.191,0.969-4.191"+
"s-0.241-0.491-0.241-1.217c0-1.14,0.647-1.991,1.453-1.991c0.685,0,1.016,0.526,1.016,1.155c0,0.703-0.439,1.755-0.665,2.729"+
"c-0.189,0.816,0.401,1.481,1.189,1.481c1.427,0,2.389-1.872,2.389-4.089c0-1.686-1.112-2.947-3.135-2.947"+
"c-2.285,0-3.709,1.74-3.709,3.683c0,0.67,0.193,1.143,0.497,1.508c0.139,0.168,0.159,0.236,0.108,0.429"+
"c-0.036,0.142-0.119,0.482-0.153,0.617c-0.05,0.195-0.205,0.264-0.377,0.192c-1.053-0.439-1.543-1.616-1.543-2.938"+
"c0-2.185,1.805-4.805,5.384-4.805c2.876,0,4.769,2.125,4.769,4.406C16.077,12.077,14.434,14.331,12.012,14.331z'/></svg>";
byte[] bytes = svgCode.getBytes("utf-8");
......
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));