From 459bee2d05b9237da2646aa05554637237185cac Mon Sep 17 00:00:00 2001 From: whycxzp <perryhsu@163.com> Date: 星期三, 27 十月 2021 13:46:59 +0800 Subject: [PATCH] 更新人脸识别模块功能:包括人脸库的管理,人脸识别引擎在线激活,人脸识别对比(人脸登录)及用户权限查询 --- src/main/java/com/whyc/controller/FaceController.java | 327 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 322 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/whyc/controller/FaceController.java b/src/main/java/com/whyc/controller/FaceController.java index 0bcec45..37788df 100644 --- a/src/main/java/com/whyc/controller/FaceController.java +++ b/src/main/java/com/whyc/controller/FaceController.java @@ -1,12 +1,36 @@ package com.whyc.controller; +import com.arcsoft.face.FaceEngine; +import com.arcsoft.face.toolkit.ImageFactory; +import com.arcsoft.face.toolkit.ImageInfo; +import com.google.gson.reflect.TypeToken; import com.whyc.constant.YamlProperties; import com.whyc.dto.Response; +import com.whyc.factory.FaceEngineFactory; +import com.whyc.pojo.PermitGroup; +import com.whyc.pojo.UserFace; +import com.whyc.pojo.UserInf; +import com.whyc.service.FaceService; +import com.whyc.service.PermitGroupUserService; +import com.whyc.service.UserService; +import com.whyc.util.ActionUtil; +import com.whyc.util.FaceIdentifyUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.assertj.core.util.Arrays; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; +import sun.misc.BASE64Decoder; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; + +import static com.whyc.util.ActionUtil.getApplication; /** * 浜鸿劯璇嗗埆 @@ -16,13 +40,306 @@ @Api(tags = "浜鸿劯璇嗗埆") public class FaceController { + @Resource + private FaceService service; + + @Resource + private PermitGroupUserService permitGroupUserService; + + @Resource + private UserService userService; + @GetMapping("activeOnline") @ApiOperation(value = "鍦ㄧ嚎婵�娲讳汉鑴歌瘑鍒紩鎿�",notes = "鍙渶瑕佸垰寮�濮嬫椂璋冪敤涓�娆�,浼氬鑷碼ppId涓庣‖浠惰澶囩粦瀹�,蹇呴』鏈夊閮ㄧ綉缁�,鍚﹀垯婵�娲讳細澶辫触!" + "鍚庣画浣跨敤鏃犻渶鍐嶈皟鐢ㄦ縺娲�,鍙互绂荤嚎浣跨敤") public Response activeOnline(){ - - return null; + Response model = new Response(); + boolean b = FaceIdentifyUtil.activeOnline(); + if(b){ + return new Response().set(1,true,"寮曟搸婵�娲绘垚鍔�"); + }else{ + return new Response().set(1,false,"寮曟搸婵�娲诲け璐�,璇锋鏌ョ綉缁滄槸鍚︿负澶栫綉骞剁‘璁ppId鍜宬ey鏄惁姝g‘!"); + } } + @PostMapping + @ApiOperation(value = "娣诲姞,鍔犲叆鍒颁汉鑴稿簱") + public Response add(@RequestParam String fileData,String uName,Integer uId ){ + //浼犲弬 uId,uName,fileData--鏂囦欢娴� + fileData = fileData.replaceAll(" ", "+"); + + //鐢变簬鍥剧墖瀛楃涓叉槸鐢眀ase64缂栫爜鐨�,瑙g爜鎴愬瓧鑺傛暟缁� + //data:image/jpeg;base64, + // fileData = fileData.replaceAll("data:image/jpeg;base64,",""); + fileData = fileData.split(",")[1]; + BASE64Decoder decoder = new BASE64Decoder(); + byte[] dataBytes = new byte[0]; + try { + dataBytes = decoder.decodeBuffer(fileData); + /*for (int i = 0; i < dataBytes.length; ++i) { + if (dataBytes[i] < 0) {// 璋冩暣寮傚父鏁版嵁 + dataBytes[i] += 256; + } + }*/ + } catch (IOException e) { + e.printStackTrace(); + } + + /*=========妫�鏌ュ浘鐗囨槸鍚︽湁鏁�=======*/ + //妫�楠屽浘鐗囨槸鍚︽湁鏁� + ImageInfo imageInfo = ImageFactory.getRGBData(dataBytes); + //鍒濆鍖栧紩鎿� +// FaceEngine faceEngine = FaceIdentifyUtil.init(); + FaceEngine faceEngine = FaceEngineFactory.getInstance(); + if (faceEngine==null){ + return new Response().set(1,false,"鍒濆鍖栧紩鎿庡け璐�"); + }else { + //妫�鏌ヨ搴︽槸鍚︾姝�,妫�鏌ヤ汉鑴稿儚绱犲ぇ灏忔槸鍚�>50脳50 + Response checkRes1 = FaceIdentifyUtil.orientAndPxDetect(faceEngine,imageInfo); + if((boolean)checkRes1.getData()){ + /*=========鍥剧墖淇濆瓨===========*/ + //鑾峰彇鍒皌omcat webapp缁濆璺緞 + String realPath = getApplication().getRealPath("/"); +// String[] split = realPath.split(File.separator); + String[] split = realPath.split("\\\\"); + String projectName = split[split.length - 1]; + + String webAppPath = realPath.substring(0, realPath.lastIndexOf(projectName)); + + //瀛樺偍浜鸿劯鍥剧墖鐨勬枃浠跺す + String fileDirName = webAppPath + projectName + "_photo"; + + File file = new File(fileDirName); + //涓嶅瓨鍦ㄥ垯鍒涘缓璇ユ枃浠跺す + if (!file.exists()) { + file.mkdirs(); + } + + //鏂囦欢鍏ㄨ矾寰� + String fileName = fileDirName + File.separator + uName + ".jpg"; + + try { + FileOutputStream fot = new FileOutputStream(fileName); + + fot.write(dataBytes); + fot.flush(); + fot.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + + /*=========鏁版嵁搴撹〃鏇存柊===========*/ + // model = service.add(uId, uName, projectName); + service.add(uId, uName, fileName); + + return new Response().set(1,true,"鏂板鎴愬姛"); + } + return checkRes1; + } + + } + + /** + * 浜鸿劯搴撶鐞�: 鏇存柊 + * @return + */ + @PutMapping + @ApiOperation(value = "鏇存柊") + public Response update(@RequestParam String fileData,String uName){ + //浼犲弬,uName,fileData--鏂囦欢娴� + + //鐢变簬鍥剧墖瀛楃涓叉槸鐢眀ase64缂栫爜鐨�,瑙g爜鎴愬瓧鑺傛暟缁� + fileData = fileData.replaceAll(" ", "+"); + fileData = fileData.split(",")[1]; + BASE64Decoder decoder = new BASE64Decoder(); + byte[] dataBytes = new byte[0]; + try { + dataBytes = decoder.decodeBuffer(fileData); + } catch (IOException e) { + e.printStackTrace(); + } + + /*=========妫�鏌ュ浘鐗囨槸鍚︽湁鏁�=======*/ + //妫�楠屽浘鐗囨槸鍚︽湁鏁� + //鑾峰彇鍥惧儚淇℃伅 + ImageInfo imageInfo = ImageFactory.getRGBData(dataBytes); + //鍒濆鍖栧紩鎿� +// FaceEngine faceEngine = FaceIdentifyUtil.init(); + FaceEngine faceEngine = FaceEngineFactory.getInstance(); + if (faceEngine==null){ + return new Response().set(1,false,"鍒濆鍖栧紩鎿庡け璐�"); + }else { + //妫�娴嬪浘鐗� + Response checkRes1 = FaceIdentifyUtil.orientAndPxDetect(faceEngine, imageInfo); + if ((boolean)checkRes1.getData()) { + /*=========鍥剧墖淇濆瓨===========*/ + //鑾峰彇鍒皌omcat webapp缁濆璺緞 + String realPath = getApplication().getRealPath("/"); + String[] split = realPath.split(File.separator); + String projectName = split[split.length - 1]; + + String webAppPath = realPath.substring(0, realPath.lastIndexOf(projectName)); + + //瀛樺偍浜鸿劯鍥剧墖鐨勬枃浠跺す + String fileDirName = webAppPath + projectName + "_photo"; + + File file = new File(fileDirName); + //涓嶅瓨鍦ㄥ垯鍒涘缓璇ユ枃浠跺す + if (!file.exists()) { + file.mkdirs(); + } + + //鏂囦欢鍏ㄨ矾寰� + String fileName = fileDirName + File.separator + uName + ".jpg"; + + try { + FileOutputStream fot = new FileOutputStream(fileName); + + fot.write(dataBytes); + fot.flush(); + fot.close(); + + } catch (IOException e) { + e.printStackTrace(); + } + return new Response().setII(1,"鏇存柊鎴愬姛"); + } + return checkRes1; + } + } + + /** + * 浜鸿劯搴撶鐞�: 鍒犻櫎 + * @return + */ + @DeleteMapping + @ApiOperation(value = "鍒犻櫎") + @Transactional + public Response delete(@RequestParam Integer uId,@RequestParam Integer faceId, + @RequestParam String uName){ + + //鏇存柊user_inf鍜宖ace琛� + service.update(uId,faceId); + + //鍒犻櫎鍥剧墖 + //鑾峰彇鍒板浘鐗囨墍鍦ㄦ枃浠跺す鐨勫叏璺緞 + String fileName = findFileDirPath(); + //鑾峰彇鍥剧墖鐨勫叏璺緞 + File file = new File(fileName+File.separator+uName+".jpg"); + if(file.exists()){ + file.delete(); + } + + return new Response().setII(1,"鍒犻櫎鎴愬姛"); + } + + /**鑾峰彇鍥剧墖鏂囦欢澶瑰叏璺緞*/ + private String findFileDirPath() { + String realPath = getApplication().getRealPath("/"); + String[] split = realPath.split("\\\\"); + String projectName = split[split.length - 1]; + + String webAppPath = realPath.substring(0, realPath.lastIndexOf(projectName)); + + //瀛樺偍浜鸿劯鍥剧墖鐨勬枃浠跺す + String fileDirName = webAppPath + "\\" + projectName + "_photo"; + + return fileDirName; + } + + /**鑾峰彇鍥剧墖鐨刪ttp鍔犺浇url*/ + private String findWebPhotoUrl(String uName){ + HttpServletRequest request = ActionUtil.getRequest(); + //鑾峰彇椤圭洰鍚嶇О + String realPath = ActionUtil.getApplication().getRealPath("/"); + String[] split = realPath.split("/"); + String projectName = split[split.length - 1]; + //鍥剧墖淇濆瓨璺緞,閲囧彇ip+port璁块棶褰㈠紡鑰岄潪纭洏褰㈠紡,鏂逛究鍥剧墖鍔犺浇 + String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/"+projectName+"_photo/"+uName+".jpg"; + return url; + } + + /** + * 浜鸿劯搴撶鐞�: 鏌ヨ鍒嗛〉鍒楄〃 + * 鐜板湪閫昏緫鏄綋鍓嶇敤鎴锋煡璇㈣嚜宸辩殑浜鸿劯淇℃伅 + * @return + */ + @GetMapping("info") + @ApiOperation(value = "鏌ヨ浜鸿劯淇℃伅") + public Response getInfo(){ + //浼犲叆pageNum,pageSize + UserInf userInf = (UserInf) ActionUtil.getSession().getAttribute("user"); + UserInf user = service.getInfo(userInf.getUId()); + + return new Response().set(1,user); + } + + /** + * 浜鸿劯璇嗗埆: 娲讳綋妫�楠�,鐩镐技搴﹀姣�,1姣擭 + */ + @PostMapping("faceCompare2N") + @ApiOperation(value = "浜鸿劯璇嗗埆瀵规瘮") + public Response faceCompare2N(@RequestParam String fileData){ + Response res = new Response(); + /*====鑾峰彇鍥剧墖骞舵牎楠屾椿浣�,瑙掑害,鍍忕礌澶у皬====*/ + //鐢变簬鍥剧墖瀛楃涓叉槸鐢眀ase64缂栫爜鐨�,瑙g爜鎴愬瓧鑺傛暟缁� + fileData = fileData.replaceAll(" ", "+"); + fileData = fileData.split(",")[1]; + BASE64Decoder decoder = new BASE64Decoder(); + byte[] dataBytes = new byte[0]; + try { + dataBytes = decoder.decodeBuffer(fileData); + } catch (IOException e) { + e.printStackTrace(); + } + ImageInfo imageInfo = ImageFactory.getRGBData(dataBytes); + FaceEngine faceEngine = FaceEngineFactory.getInstance(); + if(faceEngine!=null) { + res = FaceIdentifyUtil.liveDetect(faceEngine, imageInfo); + //娲讳綋鏍¢獙閫氳繃 + if (res.getCode() == 1) { + res = FaceIdentifyUtil.orientAndPxDetect(faceEngine, imageInfo); + //浜哄儚瑙掑害,澶у皬鏍¢獙閫氳繃 + if(res.getCode()==1) { + //鑾峰彇鏁版嵁搴撲腑鎵�鏈夌殑浜鸿劯鍥剧墖 + List<UserInf> userInfList = service.findAllFaceUrl(); + if(userInfList.size()==0){ + res.setCode(0); + res.setMsg("浜鸿劯搴撴殏鏃犱换浣曟暟鎹�"); + }else{ + for (UserInf temp:userInfList){ + String filePath = temp.getFace().getUrl(); + ImageInfo imageInfo2 = ImageFactory.getRGBData(new File(filePath)); + Response res3 = FaceIdentifyUtil.faceCompare(faceEngine, imageInfo, imageInfo2); + //瀵规瘮鎴愬姛,璁板綍鐢ㄦ埛鐧诲綍淇℃伅 + if(res3.getCode()==1){ + // 灏嗙櫥闄嗘垚鍔熺殑鐢ㄦ埛瀛樺叆session + ActionUtil.getSession().setAttribute("user", temp); + + //璁剧疆session涓嶆椿鍔ㄦ椂闂翠负30鍒� + ActionUtil.getSession().setMaxInactiveInterval(60 * 30); + ActionUtil.getSession().setAttribute("ip", ActionUtil.getRequest().getRemoteAddr()); + List<PermitGroup> permitList = permitGroupUserService.getPermitByUser(temp.getUId()); + if (permitList!=null && permitList.size()!=0) { + ActionUtil.getSession().setAttribute("permits", permitList); + } else { + res.setCode(0); + res.setMsg("杩樻湭鍒嗛厤鏉冮檺"); + } + //灏嗘柊鐧诲綍鐨勭敤鎴峰瓨鍏pplication + userService.setApplication(temp); + } + } + } + } + } + }else{ + res.setCode(0); + res.setMsg("鍒濆鍖栧紩鎿庡け璐�"); + } + + return res; + } } -- Gitblit v1.9.1