whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/util/FaceIdentifyUtil.java
@@ -19,38 +19,71 @@
public class FaceIdentifyUtil {
    //外网激活
    public static boolean activeOnline(){
    public static boolean active(int activeType){
        boolean res = false;
        String appId="";
        String sdkKey="";
        String activeKey = YamlProperties.activeKey;
        Integer faceType = YamlProperties.faceType;
        FaceEngine faceEngine =null;
        //从官网我的应用中获取
        /*//从官网我的应用中获取
        String appId4Win = "AafPX5afzycEnTqH3v79gY3VozNVTSNqT8kzSmP7b48q";
        String sdkKey4Win = "GsYwWhxzuQEE1WE34J3Fo8d2QppjkqYGojyc4625JhKb";
        //Linux系统appId和sdkKey及文件加载
        String appId4Linux ="AafPX5afzycEnTqH3v79gY3VozNVTSNqT8kzSmP7b48q";
        String sdkKey4Linux ="GsYwWhxzuQEE1WE34J3Fo8d2GiFdddqH81ghetQQTGmv";
        String sdkKey4Linux ="GsYwWhxzuQEE1WE34J3Fo8d2GiFdddqH81ghetQQTGmv";*/
        //从官网我的应用中获取
        String appId4Win = YamlProperties.appId4Win;
        String sdkKey4Win = YamlProperties.sdkKey4Win;
        String realPath = ActionUtil.getApplication().getRealPath("/");
        //Linux系统appId和sdkKey及文件加载
        String appId4Linux =YamlProperties.appId4Linux;
        String sdkKey4Linux =YamlProperties.sdkKey4Linux;
        //String realPath = ActionUtil.getApplication().getRealPath("/");
        String realPath = CommonUtil.classesPath();
        System.out.println("========realPath=========:"+realPath);
        System.out.println("========faceEngine dll 路径=========:"+realPath+ File.separator+"WEB-INF"+File.separator+"lib"+File.separator+"WIN64");
        //判断是什么系统
        System.out.println("========faceEngine dll 路径=========:"+realPath+ File.separator+"lib"+File.separator+"WIN64");
        //FaceEngine获取,判断是什么系统和什么版本
        if(System.getProperty("os.name").toLowerCase().contains("window")){
            appId=appId4Win;
            sdkKey=sdkKey4Win;
            faceEngine = new FaceEngine(realPath+ File.separator+"WEB-INF"+File.separator+"lib"+File.separator+"WIN64");
            appId = appId4Win;
            sdkKey = sdkKey4Win;
            if(faceType == 1 //免费版
            ) {
                faceEngine = new FaceEngine(realPath + File.separator + "lib" + File.separator + "WIN64");
            }else{ //付费版永久
                faceEngine = new FaceEngine(realPath + File.separator + "lib" + File.separator + "WIN64PRO");
            }
        }else{
            appId=appId4Linux;
            sdkKey=sdkKey4Linux;
            faceEngine = new FaceEngine(realPath+ File.separator+"WEB-INF"+File.separator+"lib"+File.separator+"LINUX64");
            if(faceType == 1 //免费版
            ) {
                faceEngine = new FaceEngine(realPath + File.separator + "lib" + File.separator + "LINUX64");
            }else{ //付费版永久
                faceEngine = new FaceEngine(realPath + File.separator + "lib" + File.separator + "LINUX64PRO");
            }
        }
        //激活引擎,只需一次就行,后续无需再激活
        int errorCode = faceEngine.activeOnline(appId, sdkKey);
        int errorCode = 666;
        /**========需要免费版的依赖包才能调用 Start========*/
        errorCode = faceEngine.activeOnline(appId, sdkKey);
        /**========需要免费版的依赖包才能调用 End========*/
        /**========需要增值版的依赖包才能调用 Start========*/
        /*if(activeType == 1){ //在线激活
            errorCode = faceEngine.activeOnline(appId,sdkKey,activeKey);
        }else if(activeType == 2){ //离线激活
            //先服务器生成离线授权文件,离线.dat文件放在 lib下即可
            errorCode = faceEngine.activeOffline(realPath + File.separator + "lib" + File.separator + activeKey+".dat");
        }*/
        /**========需要增值版的依赖包才能调用 End========*/
        if (errorCode != ErrorInfo.MOK.getValue() && errorCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
            //激活失败
        }else{
@@ -63,11 +96,21 @@
    /**初始化*/
    public static FaceEngine init(){
        FaceEngine faceEngine=null;
        String realPath = ActionUtil.getApplication().getRealPath("/");
        String realPath = CommonUtil.classesPath();
        if(System.getProperty("os.name").toLowerCase().contains("window")) {
            faceEngine = new FaceEngine(realPath + File.separator + "WEB-INF" + File.separator + "lib" + File.separator + "WIN64");
            if(YamlProperties.faceType == 1 //免费版
            ) {
                faceEngine = new FaceEngine(realPath + File.separator + "lib" + File.separator + "WIN64");
            }else{ //付费版永久
                faceEngine = new FaceEngine(realPath + File.separator + "lib" + File.separator + "WIN64PRO");
            }
        }else{
            faceEngine = new FaceEngine(realPath + File.separator + "WEB-INF" + File.separator + "lib" + File.separator + "LINUX64");
            if(YamlProperties.faceType == 1 //免费版
            ) {
                faceEngine = new FaceEngine(realPath + File.separator + "lib" + File.separator + "LINUX64");
            }else{ //付费版永久
                faceEngine = new FaceEngine(realPath + File.separator + "lib" + File.separator + "LINUX64PRO");
            }
        }
        //引擎配置
        EngineConfiguration engineConfiguration = new EngineConfiguration();
@@ -94,7 +137,8 @@
        int errorCode = faceEngine.init(engineConfiguration);
        if (errorCode != ErrorInfo.MOK.getValue()) {
            System.out.println("初始化引擎失败");
            String errorCodeStr = ErrorInfo.getValidEnum(errorCode).name();
            System.out.println("初始化引擎失败,错误码:"+errorCode+",错误码解释:"+errorCodeStr);
            return null;
        }
        System.out.println("初始化faceEngine引擎成功");
@@ -103,26 +147,21 @@
    }
    public static Response orientAndPxDetect(FaceEngine faceEngine, ImageInfo imageInfo) {
        Response response = new Response();
        response.setCode(1);
        //检查角度是否端正,检查人脸像素大小是否>50×50
        List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
        faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getImageFormat(), faceInfoList);
        if (faceInfoList.size()<1){
            response.setCode(0);
            response.setMsg("无效的人脸信息");
            return new Response().set(1,false,"无效的人脸信息");
        }else {
            FaceInfo faceInfo = faceInfoList.get(0);
            Rect rect = faceInfo.getRect();
            if (faceInfo.getOrient() != 1) {
                response.setCode(0);
                response.setMsg("人像姿势不端正");
                return new Response().set(1,false,"人像姿势不端正");
            } else if ((rect.bottom - rect.top) < 50 || (rect.right - rect.left) < 50) {
                response.setCode(0);
                response.setMsg("人像面部像素太低");
                return new Response().set(1,false,"人像面部像素太低");
            }
        }
        return response;
        return new Response().set(1,true,"校验通过");
    }
    public static Response liveDetect(FaceEngine faceEngine, ImageInfo imageInfo) {
@@ -191,10 +230,12 @@
        // 用于证件照或生活照与证件照之间的特征比对,推荐阈值0.82
        float faceThreshold = YamlProperties.faceThreshold;
        if(faceSimilar.getScore()<faceThreshold){
            res.setCode(0);
            res.setCode(1);
            res.setData(false);
            res.setMsg("人脸不匹配");
        }else{
            res.setCode(1);
            res.setData(true);
            res.setMsg("匹配成功");
        }
        return res;