// converter.svg2PDF(svgCode, “D:/” + name + “_SVG代码转PDF.pdf”);
// converter.svg2PNG(svgCode, “D:/” + name + “_SVG代码转PNG.png”);
// converter.svg2JPEG(svgCode, “D:/” + name + “_SVG代码转JPG.jpg”);
// converter.svg2PDF(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.pdf”)));
// converter.svg2PNG(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.png”)));
// converter.svg2JPEG(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.jpg”)));
// converter.svg2EPS(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.eps”)));
// converter.svg2PS(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.ps”)));
svgtoeps(svgCode, new FileOutputStream(new File(“E:/svgfile/” + name + “_SVG代码转输出流.eps”)));
}
public static File svgFileChangeJpg(File svgFile, String jpgPath){
log.info(“========svgFileChangeJpg.jpgPath:” + jpgPath);
try {
File file = new File(jpgPath);
if (file.exists()) {
file.delete();
}else {
file.getParentFile().mkdir();
file.createNewFile();
}
log.info(“========svgFileChangeJpg.jpgFile:” + file);
log.info(“========svgFileChangeJpg.svgFile:” + svgFile);
svg2JPEG(svgFile, file);
return file;
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
public static void svgtoeps(String svgCode, OutputStream os){
EPSTranscoder epsTranscoder = new EPSTranscoder();
try {
svgCode = svgCode.replaceAll(“:rect”, “rect”);
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgCode.getBytes()));
TranscoderOutput output = new TranscoderOutput(os);
epsTranscoder.transcode(input, output);
os.flush();
os.close();
} catch (Exception e) {
}
}
/**
-
SVG转PNG
-
@param svgCode SVG代码
-
@param outpath 输出路径
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2PNG(String svgCode, String outpath) throws TranscoderException, IOException {
Transcoder transcoder = new PNGTranscoder();
svgConverte(svgCode, outpath, transcoder);
}
/**
-
SVG转PNG
-
@param svgCode SVG代码
-
@param out 输出流
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2PNG(String svgCode, OutputStream out) throws TranscoderException, IOException {
Transcoder transcoder = new PNGTranscoder();
svgConverte(svgCode, out, transcoder);
}
/**
-
SVG转PNG
-
@param svgFile SVG文件
-
@param outFile 输出文件
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2PNG(File svgFile, File outFile) throws TranscoderException, IOException {
Transcoder transcoder = new PNGTranscoder();
svgConverte(svgFile, outFile, transcoder);
}
/**
-
SVG转JPG
-
@param svgCode SVG代码
-
@param outpath 输出路径
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2JPEG(String svgCode, String outpath) throws TranscoderException, IOException {
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);
svgConverte(svgCode, outpath, transcoder);
}
/**
-
SVG转JPG
-
@param svgCode SVG代码
-
@param out 输出流
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2JPEG(String svgCode, OutputStream out) throws TranscoderException, IOException {
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);
svgConverte(svgCode, out, transcoder);
}
/**
-
SVG转JPG
-
@param svgFile SVG文件
-
@param outFile 输出文件
-
@throws TranscoderException
-
@throws IOException
*/
public static void svg2JPEG(File svgFile, File outFile) throws TranscoderException, IOException {
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);
svgConverte(svgFile, outFile, transcoder);
}
/**
-
SVG转PDF
-
@param svgCode SVG代码
-
@param outpath 输出路径
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2PDF(String svgCode, String outpath) throws TranscoderException, IOException {
Transcoder transcoder = new PDFTranscoder();
svgConverte(svgCode, outpath, transcoder);
}
/**
-
SVG转PS
-
@param svgCode
-
@param outpath
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2PS(String svgCode, String outpath) throws TranscoderException, IOException {
Transcoder transcoder = new PSTranscoder();
svgConverte(svgCode, outpath, transcoder);
}
/**
-
SVG转PS
-
@param svgCode SVG代码
-
@param out 输出流
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2PS(String svgCode, OutputStream out) throws TranscoderException, IOException {
Transcoder transcoder = new PSTranscoder();
svgConverte(svgCode, out, transcoder);
}
/**
-
SVG转EPS
-
@param svgCode SVG代码
-
@param out 输出流
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2EPS(String svgCode, OutputStream out) throws TranscoderException, IOException {
Transcoder transcoder = new EPSTranscoder();
svgConverte(svgCode, out, transcoder);
}
/**
-
SVG转EPS
-
@param svgCode
-
@param outpath
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2EPS(String svgCode, String outpath) throws TranscoderException, IOException {
Transcoder transcoder = new EPSTranscoder();
svgConverte(svgCode, outpath, transcoder);
}
/**
-
SVG转PDF
-
@param svgCode SVG代码
-
@param out 输出流
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2PDF(String svgCode, OutputStream out) throws TranscoderException, IOException {
Transcoder transcoder = new PDFTranscoder();
svgConverte(svgCode, out, transcoder);
}
/**
-
SVG转PDF
-
@param svgFile SVG文件
-
@param outFile 输出文件
-
@throws TranscoderException
-
@throws IOException
*/
public void svg2PDF(File svgFile, File outFile) throws TranscoderException, IOException {
Transcoder transcoder = new PDFTranscoder();
svgConverte(svgFile, outFile, transcoder);
}
private void svgConverte(String svgCode, String outpath, Transcoder transcoder) throws IOException, TranscoderException {
svgConverte(svgCode, getOutputStream(outpath), transcoder);
}
private static void svgConverte(File svg, File outFile, Transcoder transcoder) throws IOException, TranscoderException {
svgConverte(svg2String(getInputStream(svg)), getOutputStream(outFile), transcoder);
}
private static void svgConverte(String svgCode, OutputStream out, Transcoder transcoder) throws IOException, TranscoderException {
svgCode = svgCode.replaceAll(“:rect”, “rect”);
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgCode.getBytes()));
TranscoderOutput output = new TranscoderOutput(out);
svgConverte(input, output, transcoder);
}
private static void svgConverte(TranscoderInput input, TranscoderOutput output, Transcoder transcoder) throws IOException, TranscoderException {
transcoder.transcode(input, output);
}
public static InputStream getInputStream(File file) throws IOException {
return new FileInputStream(file);
}
public InputStream getInputStream(String filepath) throws IOException {
File file = new File(filepath);
if (file.exists())
return getInputStream(file);
else
return null;
}
public static OutputStream getOutputStream(File outFile) throws IOException {
return new FileOutputStream(outFile);
}
public OutputStream getOutputStream(String outpath) throws IOException {
File file = new File(outpath);
if (!file.exists())
file.createNewFile();
return getOutputStream(file);
}