在后端将svg格式转为png格式

导入poi


		
		
			batik
			batik-svggen
			1.6
		
		
			batik
			batik-awt-util
			1.6
		
		
			batik
			batik-bridge
			1.6
		
		
			batik
			batik-css
			1.6
		
		
			batik
			batik-dom
			1.6
		
		
			batik
			batik-gvt
			1.6
		
		
			batik
			batik-parser
			1.6
		
		
			batik
			batik-script
			1.6
		
		
			batik
			batik-svg-dom
			1.6
		
		
			batik
			batik-transcoder
			1.6
		
		
			batik
			batik-util
			1.6
		
		
			batikbatik-xml
			1.6
		
		
			xerces
			xercesImpl
			2.5.0
		
		

controller

 	@RequestMapping("/svg2png")
    public static Map convertSvg2Png(String svgCode) throws IOException, TranscoderException {
        //封装返回结果
        Map map = new HashMap();
        //返回类型为base64
        Base64.Encoder encoder = Base64.getEncoder();
        //使用输出字节缓冲流接收转换后的数据
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        //转换前端的svg字符串
        byte[] bytes = svgCode.getBytes("utf-8");
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);

        Transcoder transcoder = new PNGTranscoder();
        try {
            TranscoderInput input = new TranscoderInput(in);
            try {
                TranscoderOutput output = new TranscoderOutput(out);
                transcoder.transcode(input, output);
                String base64 = encoder.encodeToString(out.toByteArray());
                map.put("base64", base64);
                return map;
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }

你可能感兴趣的:(在后端将svg格式转为png格式)