实名认证 人脸登录

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


业务场景

程序人脸登录,上传身份证实名认证、实名认证活体检测

一、实名认证

实名认证方案 调用百度人脸识别的人脸实名认证接口 此接口需要身份证信息,采用OCR身份证识别,上传身份证 采集用户信息

    
        String result = null;
        try {
            String imgParam = URLEncoder.encode(form.getImage(), "UTF-8");
            String param = "id_card_side=" + "front" + "&image=" + imgParam;
            String accessToken = getAuth(config.getAPI_KEY(), config.getSECRET_KEY());


            result = HttpUtil.post(FaceConst.uploadIdCard, accessToken, param);
            //请求日志记录
            FaceRequestLog requestLog = new FaceRequestLog();
            requestLog.setCreatedTime(new Date());
            requestLog.setRequestData(param);
            requestLog.setTenantId(tenantId);
            requestLog.setRemark("上传身份证正面");
            requestLog.setResponseData(result);
            logService.save(requestLog);
        } catch (Exception e) {
            log.info("上传身份证正面请求异常:{}", e.getMessage());
            return false;
        }
        JSONObject front = JSONObject.parseObject(result);
        JSONObject wordsResult = front.getJSONObject("words_result");
        String idCard = wordsResult.getJSONObject("公民身份号码").get("words").toString();
        String name = wordsResult.getJSONObject("姓名").get("words").toString();
        String sex = wordsResult.getJSONObject("性别").get("words").toString();
        String nation = wordsResult.getJSONObject("民族").get("words").toString();
        log.info("身份证正面返回信息", result);

        String resultBack = null;
        try {
            String imgParam2 = URLEncoder.encode(form.getImage(), "UTF-8");
            String param2 = "id_card_side=" + "back" + "&image=" + imgParam2;
            resultBack = HttpUtil.post(FaceConst.uploadIdCard, accessToken, param2);
            FaceRequestLog requestLog = new FaceRequestLog();
            requestLog.setCreatedTime(new Date());
            requestLog.setRequestData(param2);
            requestLog.setTenantId(tenantId);
            requestLog.setRemark("上传身份证国徽面");
            requestLog.setResponseData(resultBack);
            logService.save(requestLog);
        } catch (Exception e) {
            log.info("上传身份证国徽面请求异常:{}", e.getMessage());
            return false;
        }
        log.info("身份证上传国徽面返回信息:{}", resultBack);
        JSONObject back = JSONObject.parseObject(resultBack);
        JSONObject wordsResult = back.getJSONObject("words_result");
        String expirationDate = wordsResult.getJSONObject("失效日期").get("words").toString();
        String company = wordsResult.getJSONObject("签发机关").get("words").toString();
        String dateOfIssue = wordsResult.getJSONObject("签发日期").get("words").toString();
        log.info("身份证国徽面返回信息", resultBack);

人脸登录 采用人脸识别的人脸对比

/**
     * 比对照片
     *
     * @param face1
     * @param face2
     * @return
     * @throws IOException
     */
    public Map<String, Object> checkPhotos(String face1, String face2,Long tenantId) throws Exception {

        JSONArray jsonArray = new JSONArray();

        JSONObject map1 = new JSONObject();
        map1.put("image", face1);
        map1.put("image_type", "URL");
        map1.put("face_type", "LIVE");
        map1.put("quality_control", "NONE");
        map1.put("liveness_control", "NONE");

        JSONObject map2 = new JSONObject();
        map2.put("image", face2);
        map2.put("image_type", "URL");
        map2.put("face_type", "LIVE");
        map2.put("quality_control", "NONE");
        map2.put("liveness_control", "NONE");

        jsonArray.add(map1);
        jsonArray.add(map2);


        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";

        String param = jsonArray.toString();
        FaceRequestLog requestLog = new FaceRequestLog();
        requestLog.setCreatedTime(new Date());
        requestLog.setRequestData(param);
        requestLog.setTenantId(tenantId);
        requestLog.setRemark("人脸比对");
        logService.save(requestLog);

        String accessToken = getAuth(config.getAPI_KEY(), config.getSECRET_KEY());
        // 请求模板识别
        JSONObject result = null;

        result = JSONObject.parseObject(HttpUtil.post(url, accessToken, "application/json", param));
        log.info("请求返回信息={}", result);
        if (Integer.valueOf(result.get("error_code").toString())!=0) {
            throw new BusinessException(Integer.valueOf(result.get("error_code").toString()), result.get("error_msg").toString());
        }

        Map<String, Object> map = new HashMap<>();

        map.put("req", param);

        map.put("res", result);
        float v = Float.parseFloat(result.getJSONObject("result").get("score").toString());
        map.put("similarity", v);
        map.put("logId", requestLog.getFaceRequestLogId());

        return map;

    }
	//比对人脸 计算分数
    public Boolean verifyFace(Long userId, Long tenantId, String face1, String face2) throws Exception {

        String loginImg = config.getFileHost() + face2;
 

        FaceComparisonLog comparisonLog = new FaceComparisonLog();
        comparisonLog.setUserId(userId);
        comparisonLog.setUserHeadPic(config.getFileHost() + face1);
        comparisonLog.setLastLoginImg(loginImg);
        comparisonLog.setCreatedTime(new Date());
        comparisonLog.setTenantId(tenantId);
        save(comparisonLog);

        Map<String, Object> map = checkPhotos(config.getFileHost() + face1, loginImg, tenantId);

        FaceRequestLog requestLog = new FaceRequestLog();

        requestLog.setResponseData(map.get("res").toString());
        Long logId = Long.valueOf(map.get("logId").toString());
        BigDecimal similarity = new BigDecimal(map.get("similarity").toString());
        requestLog.setSimilarity(similarity);
        requestLog.setTenantId(tenantId);
        requestLog.setFaceRequestLogId(logId);
        logService.updateById(requestLog);
        comparisonLog.setSimilarity(similarity);
        comparisonLog.setFaceComparisonLogId(comparisonLog.getFaceComparisonLogId());
        updateById(comparisonLog);
        BigDecimal decimal = new BigDecimal(config.getSimilarity());
        int compare = similarity.compareTo(decimal);
        log.info("平台比对返回分数={},系统设置分数={}", similarity, decimal);

        if (compare < 0) {
            //低于配置分数
            return false;
        } else {
            return true;
        }


    }

总结

实名认证很简单 此处只为记录

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值