http://www.muxuanli.com:8888/lmx/blog/10
1. Get svgCode
var chart = $("#container").highcharts(); var svgCode = chart.getSVG();
2. Generate pictures according to svgCode
String svgImgPath = uploadFilePathService.getUploadFilePath()+"svg/"+salivaBox.getBarCode()+"_"+m+".png"; String svgCode = svgCodeList.get(m).replace("Highcharts.com.cn",""); SvgPngConverter.convertToPng(svgCode, svgImgPath); params.put("image"+(m+1), SvgPngConverter.getImageBASE64Encoder(svgImgPath));
3. SvgPngConverter
import java.io. *; 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; import sun.misc.BASE64Encoder; /** * Created by lijie on 17/6/5. */ public class SvgPngConverter { /** *@Description: Convert svg string to png *@Author: *@param svgCode svg code *@param pngFilePath save path *@throws IOException io exception *@throws TranscoderException svg code exception */ public static void convertToPng(String svgCode,String pngFilePath) throws IOException,TranscoderException{ File file = new File (pngFilePath); FileOutputStream outputStream = null; try { file.createNewFile (); outputStream = new FileOutputStream (file); convertToPng (svgCode, outputStream); } finally { if (outputStream != null) { try { outputStream.close (); } catch (IOException e) { e.printStackTrace (); } } } } /** *@Description: Convert svgCode to png file and output it directly to the stream *@param svgCode svg code *@param outputStream output stream *@throws TranscoderException 异常 *@throws IOException io exception */ public static void convertToPng(String svgCode,OutputStream outputStream) throws TranscoderException,IOException{ try { byte[] bytes = svgCode.getBytes ("UTF-8"); PNGTranscoder t = new PNGTranscoder (); TranscoderInput input = new TranscoderInput (new ByteArrayInputStream (bytes)); 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 String getImageBASE64Encoder(String imagePath) { InputStream in = null; byte[] data = null; try { in = new FileInputStream(imagePath); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace (); } BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data); } }