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

//控制层
@GetMapping("/getCertification")
    @ApiOperation(value = "ocr身份证识别接口  positiveImg:正面照片地址  backImg:反面照片地址 传一次调用一次接口", notes = "")
    public GlobalReponse<Map<String, Object>> 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<Map<String, Object>> getTencentAuthCertification(String positiveImg, String backImg);

//serviceImpl
@Override
    public GlobalReponse<Map<String, Object>> 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依赖
        <dependency>
			<groupId>com.tencentcloudapi</groupId>
			<artifactId>tencentcloud-sdk-java</artifactId>
			<version>3.1.94</version>
		</dependency>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值