1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Apache batik 转换svg文件为jpeg/png/pdf

Apache batik 转换svg文件为jpeg/png/pdf

时间:2020-10-12 03:50:22

相关推荐

Apache batik 转换svg文件为jpeg/png/pdf

所需主要架包:

<!-- /artifact/org.apache.xmlgraphics/batik-transcoder --><dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>batik-transcoder</artifactId><version>1.7</version></dependency><!-- /artifact/org.apache.xmlgraphics/batik-codec 转PNG需要 --><dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>batik-codec</artifactId><version>1.7</version></dependency><!-- /artifact/org.apache.xmlgraphics/fop 转pdf需要,注意版本 --><dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>fop</artifactId><version>1.0</version></dependency>

此处用的batik 1.7版本,更高版本出现一些问题,坑了:

1、如所需某些类没有了,估计是高版本没有整合完;

2、某些高版本出现转换pdf时,出现死循环的情况,有点无语。

直接上完整代码:

import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;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.batik.transcoder.image.JPEGTranscoder;import org.apache.batik.transcoder.image.PNGTranscoder;import org.apache.fop.svg.PDFTranscoder;import java.io.*;public class SvgUtil {//svg文件转成PDFpublic static void convert2Pdf(File svg, File pdf) {OutputStream outs =null;InputStream ins = null;try {outs =new BufferedOutputStream(new FileOutputStream(pdf));ins= new FileInputStream(svg);Transcoder transcoder = new PDFTranscoder();TranscoderInput input = new TranscoderInput(ins);TranscoderOutput output = new TranscoderOutput(outs);transcoder.transcode(input, output);} catch (Exception e) {e.printStackTrace();}finally {try {ins.close();outs.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}//svg转为pngpublic static void convert2Jpeg(File svg, File jpeg){InputStream ins = null ;OutputStream outs = null;try {ins= new FileInputStream(svg);outs = new BufferedOutputStream(new FileOutputStream(jpeg));Transcoder transcoder = new JPEGTranscoder();//为防止ERROR: The JPEG quality has not been specified. Use the default one: no compression 错误,需如下配置transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, 0.99f);TranscoderInput input = new TranscoderInput(ins);TranscoderOutput output = new TranscoderOutput(outs);transcoder.transcode(input, output);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{try {ins.close();outs.close();} catch (Exception ee) {// TODO: handle exceptionee.printStackTrace();}}}//svg转为pngpublic static void convert2Png(File svg, File png){InputStream ins = null ;OutputStream outs = null;try {ins= new FileInputStream(svg);outs = new BufferedOutputStream(new FileOutputStream(png));Transcoder transcoder = new PNGTranscoder();//为防止图片转换时出现图片大小和SVG设置的大小不一致,需要在转换时设置图片大小 2450和150是SVG中width和height//transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(2450.0));//transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(150.0)); TranscoderInput input = new TranscoderInput(ins);TranscoderOutput output = new TranscoderOutput(outs);transcoder.transcode(input, output);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{try {ins.close();outs.close();} catch (Exception ee) {// TODO: handle exceptionee.printStackTrace();}}}//字符串转成pdfpublic static void convertStr2Pdf(String svg, File pdf){OutputStream outs =null;InputStream ins = null;try {outs =new BufferedOutputStream(new FileOutputStream(pdf));ins= new ByteArrayInputStream(svg.getBytes());Transcoder transcoder = new PDFTranscoder();TranscoderInput input = new TranscoderInput(ins);TranscoderOutput output = new TranscoderOutput(outs);transcoder.transcode(input, output);} catch (Exception e) {e.printStackTrace();}finally {try {ins.close();outs.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void main(String[] args) throws Exception {File file=new File("g://score_0.svg");File pdf=new File("g://score_0.pdf");File png =new File("g://score_0.png");File jpeg =new File("g://score.jpg");SvgUtil.convert2Pdf(file, pdf);// SvgUtil.convert2Png(file, png);// SvgUtil.convert2Jpeg(file,jpeg);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。