springboot 集成 腾讯云ocr身份证识别

//控制层
@GetMapping("/getCertification")
    @ApiOperation(value = "ocr身份证识别接口  positiveImg:正面照片地址  backImg:反面照片地址 传一次调用一次接口", notes = "")
    public GlobalReponse> getCertification(String positiveImg, String backImg) {
        if (StringUtils.isRealBlank(positiveImg) || StringUtils.isRealBlank(backImg)) {
            GlobalReponse.fail("参数不能为空");
        }
        Long userId = JwtUtil.getCurrentJwtUser().getId();
        return iCertificationService.getTencentAuthCertification(positiveImg, backImg);
    }

//service
GlobalReponse> getTencentAuthCertification(String positiveImg, String backImg);

//serviceImpl
@Override
    public GlobalReponse> getTencentAuthCertification(String positiveImg, String backImg) {
        // positiveImg  (身份证正面网络地址)backImg   (身份证反面网络地址)
        //tsecretId和tsecretKey是你在腾讯云上的秘钥
            String tsecretId="你自己的tsecretId"//建议从配置文件读取
            String tsecretKey="你自己的tsecretKey"//建议从配置文件读取
        try {
            Credential cred = new Credential(tsecretId, tsecretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
            //正面
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ImageUrl", positiveImg);
            String params = jsonObject.toString();
            IDCardOCRRequest req = IDCardOCRRequest.fromJsonString(params, IDCardOCRRequest.class);
            IDCardOCRResponse resp = client.IDCardOCR(req);
            //反面
            JSONObject json = new JSONObject();
            json.put("ImageUrl", backImg);
            String param = json.toString();
            IDCardOCRRequest re = IDCardOCRRequest.fromJsonString(param, IDCardOCRRequest.class);
            IDCardOCRResponse res = client.IDCardOCR(re);

            Map map = new HashMap();
            map.put("front", IDCardOCRResponse.toJsonString(resp));
            map.put("back", IDCardOCRResponse.toJsonString(res));

            System.out.println(IDCardOCRResponse.toJsonString(resp));
            System.out.println(IDCardOCRResponse.toJsonString(res));
            return GlobalReponse.success(map);
        } catch (TencentCloudSDKException e) {
            log.error(e.toString());
            //System.out.println(e.toString());
            Map map = new HashMap();
            map.put("error", "实名认证失败");
            return GlobalReponse.fail(map);
        }

    }


        //maven依赖
        
			com.tencentcloudapi
			tencentcloud-sdk-java
			3.1.94
		

 

你可能感兴趣的:(springboot,java,spring,boot,后端)