public class FaceMatch {
public static final String accessToken = "需要自己在百度AI平台申请对应类型的";
public static final Double SCORE= 80.00;
public static String checkErrorMessage(String code) {
switch(code) {
case "0":
return "成功";
case "223113":
return "人脸被遮挡";
case "223114":
return "人脸模糊";
case "223115":
return "人脸光照不好";
case "223116":
return "人脸不完整";
case "223120":
return "活体检测未通过";
case "223121":
return "请勿遮挡左眼";
case "223122":
return "请勿遮挡右眼";
case "223123":
return "请勿遮挡左脸";
case "223124":
return "请勿遮挡右脸";
case "223125":
return "请勿遮挡下巴";
case "223126":
return "请勿遮挡鼻子";
case "223127":
return "请勿遮挡嘴巴";
default:
return "其他错误类型,请向管理员反馈";
}
}
public static String match(String image1,String image2) {
String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
try {
List<Map<String, Object>> images = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("image", image1);
map1.put("image_type", "BASE64");
map1.put("face_type", "LIVE");
map1.put("quality_control", "LOW");
map1.put("liveness_control", "NORMAL");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("image", image2);
map2.put("image_type", "BASE64");
map2.put("face_type", "LIVE");
map2.put("quality_control", "LOW");
map2.put("liveness_control", "NORMAL");
images.add(map1);
images.add(map2);
String param = GsonUtils.toJson(images);
String result = HttpUtil.post(url, accessToken, "application/json", param);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@RequestMapping(value = "/courseFacePunchOrLogin")
@ResponseBody
public Map<String, Object> facePunch(@RequestParam("account") String account,@RequestParam(value = "file", required = false) MultipartFile file
,@RequestParam("type") Integer type, HttpSession session,HttpServletRequest request,HttpServletResponse response) {
try {
if(file.isEmpty()) {
return setJson(false, "参数错误-照片。", null);
}
String img=Base64Util.encode(file.getBytes());
String myImg="http://xxxx";
String result=FaceMatch.match(img,
Base64Util.encode(FileUtil.readFileByBytes("D:/1.png")));
String ext = FileUploadUtils.getSuffix(file.getOriginalFilename());
String filePath="/自定义路径/"+ DateUtils.toString(new Date(), "yyyyMMdd")+"/"+System.currentTimeMillis()+".png";
File files = new File(rootpath+"/"+filePath);
if(!files.getParentFile().exists()){
files.getParentFile().mkdirs();
}
file.transferTo(files);
Gson gson = new Gson();
Map<String, Object> results = new HashMap<String, Object>();
results = gson.fromJson(result, results.getClass());
Map<String, Object> faceSignResult = new HashMap<String, Object>();
faceSignResult = gson.fromJson(results.get("result")+"", faceSignResult.getClass());
if(results.get("error_msg").equals("SUCCESS")) {
if(Double.parseDouble(faceSignResult.get("score")+"")>FaceMatch.SCORE) {
insertFaceSign.put("statusSgin", 1);
}else {
}
}else {
StringerrorInfo=FaceMatch.checkErrorMessage(results.get("error_code")+"".replaceAll(".0", ""));
}
switch(type) {
case 1:
break;
case 2:
break;
default:
return setJson(false, "类型错误。", null);
}
} catch (Exception e) {
logger.error("CourseController.courseFacePunch()---error", e);
this.setAjaxException(setJson(false, e+"", null));
}
return null;
}