如何在Spring Rest webservice中将画布保存为图像,canvas包含基于用户输入的图形。我需要将图形自动保存为图像(我使用canvas.toDataUrl()获取图像数据)到服务器。服务器具有Spring REST Web服务。
提前致谢。
答案 0 :(得分:0)
试试这个〜
// jsp
function saveCanvasImage(){
var imageData = myCanvas.toDataUrl();
$.ajax({
url:'/controller/path/saveCanvasImage',
data:{imageBase64: imageData},
type: 'post',
dataType: 'json',
timeout: 10000,
async: false,
error: function(){
console.log("WOOPS");
},
success: function(res){
if(res.ret==0){
console.log("SUCCESS");
}else{
console.log("FAIL : " + res.msg);
}
}
});
}
// java(controller)
@RequestMapping(value = "/controller/path/saveCanvasImage", method = RequestMethod.POST)
@ResponseBody public Map<String,Object> saveCanvasImage(
@RequestParam(value="imageBase64", defaultValue="")String imageBase64) {
Map<String,Object> res = new HashMap<String, Object>();
File imageFile = new File("/home/data/canvasImage.png");
try{
byte[] decodedBytes = DatatypeConverter.parseBase64Binary(imageBase64.replaceAll("data:image/.+;base64,", ""));
BufferedImage bfi = ImageIO.read(new ByteArrayInputStream(decodedBytes));
ImageIO.write(bfi , "png", imageFile);
bfi.flush();
res.put("ret", 0);
}catch(Exception e){
res.put("ret", -1);
res.put("msg", "Cannot process due to the image processing error.");
return res;
}
return res;
}
祝你好运