赞
踩
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-svg-dom -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-svg-dom</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-codec</artifactId>
<version>1.9.1</version>
</dependency>
package cn.com.eduedu.exam.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
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;
public class SVGUtils {
public static void convertToPng(byte[] svgCode, OutputStream outputStream) throws TranscoderException, IOException {
try {
// byte[] bytes = svgCode.getBytes("utf-8");
PNGTranscoder t = new PNGTranscoder();
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgCode));
TranscoderOutput output = new TranscoderOutput(outputStream);
t.transcode(input, output);
// outputStream.flush();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static byte[] convertToPngByte(byte[] bytes) throws TranscoderException, IOException {
ByteArrayOutputStream byteoutput=new ByteArrayOutputStream();
try {
PNGTranscoder t = new PNGTranscoder();
TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
TranscoderOutput output = new TranscoderOutput(byteoutput);
t.transcode(input, output);
return byteoutput.toByteArray();
} finally {
if (byteoutput != null) {
try {
byteoutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void convertToPng(String svgCode, OutputStream outputStream) throws TranscoderException, IOException {
convertToPng(svgCode.getBytes("utf-8"), outputStream);
}
}
参考了一些资料,但是总是报错,这个亲测没有问题..
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。