將前端canvas圖片使用java生成

1.利用canva可以轉成base64碼

	var canvas = $chart.find("canvas")[0];
	var data = canvas.toDataURL('image/png');
	var b64 = data.split(",")[1];

2.將截取後的base64碼上傳到後端,生成圖片

//base64字符串轉化成圖片  
     public static Boolean GenerateImage(String imgStr,String desPath)  
     {   //對字節數組字符串進行Base64解碼並生成圖片  


         BASE64Decoder decoder = new BASE64Decoder();
          byte[] b;
        try {
            b = decoder.decodeBuffer(imgStr);
            
            ByteArrayInputStream bais = new ByteArrayInputStream(b);
            BufferedImage bi1 = ImageIO.read(bais);
            File w2 = new File(desPath);
            ImageIO.write(bi1, "png", w2);
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

          return false;
                  
     }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章