lxw
2023-05-25 f3c27fb78447449a950ba73c5e72ceda64ad8a12
src/main/java/com/whyc/controller/FaceController.java
@@ -3,12 +3,10 @@
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;
@@ -17,7 +15,7 @@
import com.whyc.util.FaceIdentifyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.assertj.core.util.Arrays;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;
@@ -27,8 +25,8 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.whyc.util.ActionUtil.getApplication;
@@ -37,7 +35,7 @@
 */
@RequestMapping("face")
@RestController
@Api(tags = "人脸识别")
@Api(tags = "用户管理-人脸识别")
public class FaceController {
    @Resource
@@ -53,8 +51,18 @@
    @ApiOperation(value = "在线激活人脸识别引擎",notes = "只需要刚开始时调用一次,会导致appId与硬件设备绑定,必须有外部网络,否则激活会失败!" +
            "后续使用无需再调用激活,可以离线使用")
    public Response activeOnline(){
        Response model = new Response();
        boolean b = FaceIdentifyUtil.activeOnline();
        boolean b = FaceIdentifyUtil.active(1);
        if(b){
            return new Response().set(1,true,"引擎激活成功");
        }else{
            return new Response().set(1,false,"引擎激活失败,请检查网络是否为外网并确认appId和key是否正确!");
        }
    }
    @GetMapping("activeOffline")
    @ApiOperation(value = "离线激活人脸识别引擎,增值版使用")
    public Response activeOffline(){
        boolean b = FaceIdentifyUtil.active(2);
        if(b){
            return new Response().set(1,true,"引擎激活成功");
        }else{
@@ -64,7 +72,10 @@
    @PostMapping
    @ApiOperation(value = "添加",notes = "添加到人脸库")
    public Response add(@RequestParam String fileData,String uName,Integer uId ){
    public Response add(@RequestBody Map<String,Object> paramMap ){
        String fileData = (String) paramMap.get("fileData");
        String uName  = (String) paramMap.get("uName");
        Integer uId  = Integer.parseInt((String)paramMap.get("uId"));
        //传参 uId,uName,fileData--文件流
        fileData = fileData.replaceAll(" ", "+");
@@ -98,16 +109,16 @@
            Response checkRes1 = FaceIdentifyUtil.orientAndPxDetect(faceEngine,imageInfo);
            if((boolean)checkRes1.getData()){
                /*=========图片保存===========*/
                //获取到tomcat 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));
                ApplicationHome applicationHome = new ApplicationHome(getClass());
                File jarFile = applicationHome.getDir();
                //存储人脸图片的文件夹
                String fileDirName = webAppPath + projectName + "_photo";
                String fileDirName;
                if(YamlProperties.runModel == 1) {
                    fileDirName = jarFile.getParentFile().toString() + File.separator + "fg_photo";
                }else{
                    fileDirName = jarFile.toString() + File.separator + "fg_photo";
                }
                File file = new File(fileDirName);
                //不存在则创建该文件夹
@@ -146,7 +157,7 @@
     */
    @PutMapping
    @ApiOperation(value = "更新")
    public Response update(@RequestParam String fileData,String uName){
    public Response update(@RequestBody String fileData,@RequestParam String uName){
        //传参,uName,fileData--文件流
        //由于图片字符串是由base64编码的,解码成字节数组
@@ -174,15 +185,16 @@
            Response checkRes1 = FaceIdentifyUtil.orientAndPxDetect(faceEngine, imageInfo);
            if ((boolean)checkRes1.getData()) {
                /*=========图片保存===========*/
                //获取到tomcat 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));
                ApplicationHome applicationHome = new ApplicationHome(getClass());
                File jarFile = applicationHome.getDir();
                //存储人脸图片的文件夹
                String fileDirName = webAppPath + projectName + "_photo";
                String fileDirName;
                if(YamlProperties.runModel == 1) {
                    fileDirName = jarFile.getParentFile().toString() + File.separator + "fg_photo";
                }else{
                    fileDirName = jarFile.toString() + File.separator + "fg_photo";
                }
                File file = new File(fileDirName);
                //不存在则创建该文件夹
@@ -217,14 +229,14 @@
    @ApiOperation(value = "删除")
    @Transactional
    public Response delete(@RequestParam Integer uId,@RequestParam Integer faceId,
                         @RequestParam String uName){
                         @RequestParam String uName,HttpServletRequest request){
        //更新user_inf和face表
        service.update(uId,faceId);
        //删除图片
        //获取到图片所在文件夹的全路径
        String fileName = findFileDirPath();
        String fileName = findFileDirPath(request);
        //获取图片的全路径
        File file = new File(fileName+File.separator+uName+".jpg");
        if(file.exists()){
@@ -234,18 +246,13 @@
        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;
    /**获取图片文件夹全路径
     * @param request*/
    private String findFileDirPath(HttpServletRequest request) {
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        File jarFile = applicationHome.getDir();
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        return jarFile.getParentFile().toString()+File.separator+"fg_photo";
    }
    /**获取图片的http加载url*/
@@ -253,7 +260,8 @@
        HttpServletRequest request = ActionUtil.getRequest();
        //获取项目名称
        String realPath = ActionUtil.getApplication().getRealPath("/");
        String[] split = realPath.split("/");
        //String[] split = realPath.split("/");
        String[] split = realPath.split(File.separator);
        String projectName = split[split.length - 1];
        //图片保存路径,采取ip+port访问形式而非硬盘形式,方便图片加载
        String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/"+projectName+"_photo/"+uName+".jpg";
@@ -267,9 +275,9 @@
     */
    @GetMapping("info")
    @ApiOperation(value = "查询人脸信息")
    public Response getInfo(){
    public Response getInfo(HttpServletRequest request){
        //传入pageNum,pageSize
        UserInf userInf = (UserInf) ActionUtil.getSession().getAttribute("user");
        UserInf userInf = (UserInf) request.getSession().getAttribute("user");
        UserInf user = service.getInfo(userInf.getUId());
        return new Response().set(1,user);
@@ -280,8 +288,14 @@
     */
    @PostMapping("faceCompare2N")
    @ApiOperation(value = "人脸识别对比")
    public Response faceCompare2N(@RequestParam String fileData){
    public Response faceCompare2N(@RequestBody String fileData,@RequestParam String uKeyId){
        Response res = new Response();
        //先验证uKeyId对应的用户
        UserInf userByUKeyId = userService.getUserByUKeyId(uKeyId);
        if (userByUKeyId == null){
            return res.set(1,false,"Ukey没有绑定用户");
        }
        /*====获取图片并校验活体,角度,像素大小====*/
        //由于图片字符串是由base64编码的,解码成字节数组
        fileData = fileData.replaceAll(" ", "+");
@@ -310,10 +324,21 @@
                    }else{
                        for (UserInf temp:userInfList){
                            String filePath = temp.getFace().getUrl();
                            ImageInfo imageInfo2 = ImageFactory.getRGBData(new File(filePath));
                            Response res3 = FaceIdentifyUtil.faceCompare(faceEngine, imageInfo, imageInfo2);
                            File file = new File(filePath);
                            //不存在则不继续执行
                            if(!file.exists()){
                                continue;
                            }
                            ImageInfo imageInfo2 = ImageFactory.getRGBData(file);
                            res = FaceIdentifyUtil.faceCompare(faceEngine, imageInfo, imageInfo2);
                            //对比成功,记录用户登录信息
                            if(res3.getCode()==1){
                            if(res.getCode()==1){
                                //Ukey对应用户和人脸对应用户验证
                                if(!temp.getUId().equals(userByUKeyId.getUId())){
                                    return res.set(1,false,"UKey对应用户和当前人脸对应用户不匹配");
                                }
                                res.setData2(temp);
                                // 将登陆成功的用户存入session
                                ActionUtil.getSession().setAttribute("user", temp);
@@ -324,7 +349,8 @@
                                if (permitList!=null && permitList.size()!=0) {
                                    ActionUtil.getSession().setAttribute("permits", permitList);
                                } else {
                                    res.setCode(0);
                                    res.setCode(1);
                                    res.setData(false);
                                    res.setMsg("还未分配权限");
                                }
                                //将新登录的用户存入application