Search in sources :

Example 6 with IDCardOCRResponse

use of com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse in project easyj by easyj-projects.

the class DefaultTencentCloudIdCardOcrServiceImpl method doIdCardOcr.

// region Override start
// //region 身份证识别 start
/**
 * 身份证识别
 *
 * @param request 请求
 * @param config  当前请求的个性配置
 * @return response 响应
 * @see <a href="https://cloud.tencent.com/document/api/866/33524">API文档</a>
 * @see <a href="https://console.cloud.tencent.com/api/explorer?Product=ocr&Version=2018-11-19&Action=IDCardOCR">调试页面</a>
 */
@Override
public IDCardOCRResponse doIdCardOcr(IDCardOCRRequest request, @Nullable TencentCloudIdCardOcrConfig config) throws TencentCloudSDKException {
    if (config == null) {
        config = this.globalConfig;
    }
    Assert.notNull(config.getSecretId(), "'secretId' must not be null");
    Assert.notNull(config.getSecretKey(), "'secretKey' must not be null");
    Assert.notNull(config.getRegion(), "'region' must not be null");
    long startTime = System.nanoTime();
    try {
        // 实例化要请求产品的client对象,clientProfile是可选的
        OcrClient client;
        if (config == this.globalConfig) {
            client = this.globalClient;
        } else {
            client = this.newOcrClient(config);
        }
        // 发送请求,并返回一个IDCardOCRResponse的实例,与请求对象对应
        IDCardOCRResponse response = client.IDCardOCR(request);
        // 记录日志
        if (LOGGER.isInfoEnabled()) {
            // base64不打印在日志中
            String imageBase64Bak = request.getImageBase64();
            String advancedInfoBak = response.getAdvancedInfo();
            request.setImageBase64(null);
            response.setAdvancedInfo(null);
            try {
                LOGGER.info("IDCardOCR 请求成功!\r\n==>\r\n -  Request: {}\r\n - Response: {}\r\n -   Config: {}\r\n -     Cost: {} ms\r\n<==\r\n", icu.easyj.core.util.StringUtils.toString(request), icu.easyj.core.util.StringUtils.toString(response), icu.easyj.core.util.StringUtils.toString(config), (System.nanoTime() - startTime) / 1000000);
            } finally {
                request.setImageBase64(imageBase64Bak);
                response.setAdvancedInfo(advancedInfoBak);
            }
        }
        return response;
    } catch (TencentCloudSDKException | RuntimeException e) {
        String imageBase64Bak = request.getImageBase64();
        // base64不打印在日志中
        request.setImageBase64(null);
        try {
            LOGGER.error("身份证识别服务请求失败:{}\r\n==>\r\n - Request: {}\r\n -  Config: {}\r\n -    Cost: {} ms\r\n<==\r\n", e.getMessage(), icu.easyj.core.util.StringUtils.toString(request), icu.easyj.core.util.StringUtils.toString(config), (System.nanoTime() - startTime) / 1000000);
        } finally {
            request.setImageBase64(imageBase64Bak);
        }
        throw e;
    }
}
Also used : TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) OcrClient(com.tencentcloudapi.ocr.v20181119.OcrClient) IDCardOCRResponse(com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse)

Aggregations

IDCardOCRRequest (com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest)5 IDCardOCRResponse (com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse)4 TencentCloudSDKException (com.tencentcloudapi.common.exception.TencentCloudSDKException)3 IdCardOcrResponse (icu.easyj.sdk.ocr.idcardocr.IdCardOcrResponse)2 TencentCloudIdCardOcrConfig (icu.easyj.sdk.tencent.cloud.ocr.idcardocr.TencentCloudIdCardOcrConfig)2 OcrClient (com.tencentcloudapi.ocr.v20181119.OcrClient)1 CardSide (icu.easyj.sdk.ocr.CardSide)1 IOcrTemplate (icu.easyj.sdk.ocr.IOcrTemplate)1 WrapperOcrTemplate (icu.easyj.sdk.ocr.WrapperOcrTemplate)1 IdCardOcrAdvanced (icu.easyj.sdk.ocr.idcardocr.IdCardOcrAdvanced)1 IdCardOcrSdkClientException (icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkClientException)1 IdCardOcrSdkException (icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkException)1 IdCardOcrSdkServerException (icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkServerException)1 IdCardOcrAdvancedInfo (icu.easyj.sdk.tencent.cloud.ocr.idcardocr.IdCardOcrAdvancedInfo)1 IdCardOcrRequestBuilder (icu.easyj.sdk.tencent.cloud.ocr.idcardocr.IdCardOcrRequestBuilder)1 FileInputStream (java.io.FileInputStream)1 Disabled (org.junit.jupiter.api.Disabled)1 Test (org.junit.jupiter.api.Test)1 NonNull (org.springframework.lang.NonNull)1