腾讯云人脸识别接口demo

身份认证接口:姓名+身份证号+本人照片

    @ApiOperation(value = "自拍照+身份信息模式", notes = "自拍照+身份信息模式")
    @PostMapping(path = "/easyface")
    public @ResponseBody BaseResult easyface(@RequestBodyWithValid EasyfaceVo easyfaceVo) throws Exception {
        log.info("easyface 入参 {}", easyfaceVo.toString());
        String url = "https://ida.webank.com/api/paas/easyface";
        JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(easyfaceVo));
        String resp = HttpUtil.doPost(url, itemJSONObj);
        JSONObject jsonObject = JSON.parseObject(resp);

        return new  BaseResult(jsonObject);
    }

对外接口,获取签名及相关信息

    @ApiOperation(value = "获取人脸识别签名方法", notes = "获取人脸识别签名方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idCardNo", value = "身份证号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping(path = "/getFaceVerificationSign")
    public @ResponseBody BaseResult getFaceVerificationSign(@RequestParam("idCardNo")String idCardNo, @RequestParam("name")String name) {
        Map map = faceVerificationImpl.getFaceVerificationSignMap(idCardNo, name);
        return new  BaseResult(map);
    }

签名生成函数
这里的注意点是nonce 随机数32位随机串(字母+数字组成的随机数)在所有的地方都应该是一样的

    /*
    * 提供前端调用身份验证接口的参数
    * */
    public Map getFaceVerificationSignMap(String idCardNo, String name) {
        Map map = getFaceVerificationSign(idCardNo);
        Map returnMap = new HashMap();
        returnMap.putAll(map);
        returnMap.put("webankAppId", appId);
        //String nonce =getZhiDingRandomNumStr(32,"NONCE");//32位随机码
        //returnMap.put("nonce", nonce);
        returnMap.put("version", version);
        returnMap.put("orderNo", idCardNo);
        returnMap.put("name", name);
        returnMap.put("idNo", idCardNo);
        returnMap.put("userId", idCardNo);
        return returnMap;
    }
    /*
    * 生成签名  SIGN 类型
    *
    * */
    public  Map getFaceVerificationSign(String userId){
        String ticket =  getFaceSignTicket();
        log.info("ticket  {}", ticket);
        List stringList = new ArrayList<>();
        stringList.add(appId);
        stringList.add(userId);//orderNo订单号,本次人脸核身合作伙伴上送的订单号,唯一标识
        String nonceStr =getZhiDingRandomNumStr(32,"NONCE");//32位随机码
        stringList.add(nonceStr);//nonce	随机数32位随机串(字母+数字组成的随机数)
        stringList.add(version);//版本号version

        Map map = new HashMap();
        String sign = signAlgorithm(stringList,ticket);
        map.put("sign",sign);
        log.info("nonceStrLength={}",nonceStr.length());
        map.put("nonce",nonceStr);
        return map;
    }
        /**
     * 获取SIGN ticket
     * @return
     */
    public   String getFaceSignTicket(){
        String tickt = redisCache.getCache("face_sign_tickt", String.class);
        //String tickt = null;
        if(tickt != null && !"".equals(tickt)){
            return tickt;
        }

        String accessToken = getRequestFaceAccessToken();
        StringBuffer url = new StringBuffer("https://idasc.webank.com/api/oauth2/api_ticket");
        url.append("?app_id=").append(appId);
        url.append("&access_token=").append(accessToken);
        url.append("&type=").append("SIGN");
        url.append("&version=").append(version);
        String returnData = HttpUtil.get(url.toString());
        log.info("getFaceNonceTicket获取NonceTicket信息::{}",returnData);
        if(returnData != null && !"".equals(returnData)){
            JSONObject jsonObject = JSON.parseObject(returnData);
            Map map = JSONObject.toJavaObject(jsonObject, Map.class);
            log.info("getFaceSignTicket获取NonceTicket信息::{}",map);
            String tickets =  map.get("tickets") != null ? map.get("tickets").toString() : null;

            if(tickets != null){
                JSONArray jsonObjectT = JSON.parseArray(tickets);
                List> listw = new ArrayList>();
                for (Object object : jsonObjectT) {
                    Map  ret = (Map) object;//取出list里面的值转为map
                    listw.add(ret);
                }
                tickt =listw.get(0).get("value").toString();// map1.get("value") != null ? map1.get("value").toString() : null;
                redisCache.putCacheWithExpireTime("face_sign_tickt", tickt, 60*20);
                return tickt;
            }

        }
        return null;
    }

姓名+身份号+人脸照片验证
照片不需要前置的Base64等格式字样
腾讯云人脸识别接口demo_第1张图片

  • 调用第三方接口时的函数 StringEntity entity = new StringEntity(json.toString(),“utf-8”);要如此设置传值为utf-8, 否则传中文时会无法识别。
 public static String doPost(String url, JSONObject json){
        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        CloseableHttpResponse response = null;
        try {
            //StringEntity reqEntity = new StringEntity(params,"utf-8");
            StringEntity entity = new StringEntity(json.toString(),"utf-8");
            //entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json"); // 发送json数据需要设置contentType
            post.setEntity(entity);
            response = httpClient.execute(post);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                // 返回json格式
                return EntityUtils.toString(response.getEntity());
            }
        } catch (Exception e) {
            log.error("通讯异常,异常信息:[{}]", e);
        }
        finally {
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
 

你可能感兴趣的:(JAVA)