调用腾讯云OCR接口识别身份证和户口本

一、添加项目pom文件依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.68</version>
</dependency>
<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <version>3.1.49</version>
</dependency>

二、API调用的控制层代码编写


package com.wy.gcserver.ocr.controller;

import com.wy.gcserver.ocr.model.ImageScanParam;
import com.wy.gcserver.ocr.service.TencentApiOcrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/tencent/api/ocr")
public class TencentApiOcrController {

    @Autowired
    private TencentApiOcrService tencentApiOcrService;

    /**
     * 获取身份证OCR识别信息.
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/getIdcardOcr",method = RequestMethod.POST)
    public String getIdcardOcr(@RequestBody ImageScanParam param){
        String idCardSide = param.getJob();
        String imageStr = param.getInData().getImage();
        String result = tencentApiOcrService.getIdcardOcr(idCardSide, imageStr);
        return result;
    }

    /**
     * 获取户口本OCR识别信息.
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/getResidenceBookletOCR",method = RequestMethod.POST)
    public String getResidenceBookletOCR(@RequestBody ImageScanParam param){
        String imageStr = param.getInData().getImage();
        String result = tencentApiOcrService.getResidenceBookletOCR(imageStr);
        return result;
    }

}

三、腾讯云API接口调用Service层代码编写


package com.wy.gcserver.ocr.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
import com.tencentcloudapi.ocr.v20181119.models.ResidenceBookletOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.ResidenceBookletOCRResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * 腾讯云OCR文字识别接口调用.
 */
@Service
public class TencentApiOcrService{
    private static Logger logger = LoggerFactory.getLogger(TencentApiOcrService.class);

    private static final String FIELD_SECRET_ID = "";//填写secretId
    private static final String FIELD_SECRET_KEY = "";//填写secretKey
    private static final String FIELD_TENCENT_CLOUD_API = "ocr.tencentcloudapi.com";
    private static final String FIELD_REGION = "ap-guangzhou";

    /**
     * 获取OcrClient.
     *
     * @return
     */
    private OcrClient getOcrClient(){
        Credential cred = new Credential(FIELD_SECRET_ID, FIELD_SECRET_KEY);

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(FIELD_TENCENT_CLOUD_API);

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

        OcrClient ocrClient = new OcrClient(cred, FIELD_REGION, clientProfile);
        return ocrClient;
    }
    /**
     * 身份证识别.
     *
     * @param cardSide
     * @param imageStr
     * @return
     */
    public String getIdcardOcr(String cardSide, String imageStr) {
        OcrClient ocrClient = getOcrClient();
        Map<String, String> params = new HashMap<>();
        params.put("ImageBase64", imageStr);
        params.put("CardSide", cardSide);
        try {
            IDCardOCRRequest req = IDCardOCRRequest.fromJsonString(JSON.toJSONString(params), IDCardOCRRequest.class);
            IDCardOCRResponse resp = ocrClient.IDCardOCR(req);
            HashMap<String, String> dataMap = new HashMap<>();
            resp.toMap(dataMap, "");
            return  JSONObject.toJSONString(dataMap);
        } catch (Exception e) {
            logger.info(e.getMessage());
        }

        return null;
    }

    /**
     * 户口本识别.
     *
     * @param imageStr
     * @return
     */
    public String getResidenceBookletOCR(String imageStr){
        OcrClient ocrClient = getOcrClient();
        Map<String, String> params = new HashMap<>();
        params.put("ImageBase64", imageStr);
        try {
            ResidenceBookletOCRRequest req = ResidenceBookletOCRRequest.fromJsonString(JSON.toJSONString(params), ResidenceBookletOCRRequest.class);
            ResidenceBookletOCRResponse resp = ocrClient.ResidenceBookletOCR(req);
            HashMap<String, String> dataMap = new HashMap<>();
            resp.toMap(dataMap, "");
            return  JSONObject.toJSONString(dataMap);
        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        return null;
    }

}

参考文献:
腾讯云OCR文字识别API文档概览

https://cloud.tencent.com/document/sdk/Java

https://github.com/TencentCloud/tencentcloud-sdk-java

户口本首页、个人页面测试:


在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值