Search in sources :

Example 1 with IdCardOcrSdkException

use of icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkException in project easyj by easyj-projects.

the class TencentEasyjIdCardOcrTemplateImpl method idCardOcr.

@NonNull
@Override
public IdCardOcrResponse idCardOcr(@NonNull IdCardOcrRequest request) throws IdCardOcrSdkException {
    Assert.notNull(request, "'request' must not be null");
    Assert.isTrue(StringUtils.isNotBlank(request.getImage()), "'image' must not be null");
    // 提取参数
    String image = request.getImage();
    CardSide cardSide = request.getCardSide();
    IdCardOcrAdvanced[] advancedArr = request.getAdvancedArr();
    Map<String, Object> configs = request.getConfigs();
    // 将入参配置与通用配置合并,生成当前请求所使用的配置
    TencentCloudIdCardOcrConfig config = ObjectUtils.mergeData(this.tencentCloudIdCardOcrService.getGlobalConfig(), configs);
    // 为两面时,重置为null
    if (CardSide.BOTH == cardSide) {
        cardSide = null;
    }
    // region 构建请求
    // 创建request builder
    IdCardOcrRequestBuilder builder = OcrRequestBuilder.idCardOcrRequestBuilder().image(// 图片
    image).cardSide(// 卡片正反面,可以为空
    cardSide);
    this.setAdvanced(builder, advancedArr);
    // 构建request
    IDCardOCRRequest req = builder.build();
    // endregion
    // region 发送请求,返回响应
    IDCardOCRResponse resp;
    try {
        resp = tencentCloudIdCardOcrService.doIdCardOcr(req, config);
    } catch (TencentCloudSDKException e) {
        String errorCode = e.getErrorCode();
        String errorMsg = "身份证识别失败" + (StringUtils.isNotEmpty(errorCode) ? ":" + errorCode : "");
        throw new IdCardOcrSdkServerException(errorMsg, errorCode, e);
    } catch (IdCardOcrSdkException e) {
        throw e;
    } catch (RuntimeException e) {
        throw new IdCardOcrSdkClientException("身份证识别出现未知异常", ErrorCodeConstants.UNKNOWN, e);
    }
    // endregion
    // region 读取响应信息
    // 转换为当前接口的响应类型
    IdCardOcrResponse response = new IdCardOcrResponse();
    // 设置正反面枚举
    if (StringUtils.isNotBlank(resp.getName())) {
        response.setCardSide(CardSide.FRONT);
    } else if (StringUtils.isNotBlank(resp.getAuthority())) {
        response.setCardSide(CardSide.BACK);
    } else {
        throw new IdCardOcrSdkServerException("未知的身份证正反面信息", "UNKNOWN_CARD_SIDE");
    }
    // 校验是否与入参一致
    if (cardSide != null && cardSide != response.getCardSide()) {
        throw new IdCardOcrSdkServerException("当前身份证图片不是" + cardSide.sideName() + "照", "WRONG_CARD_SIDE");
    }
    // 设置正面信息
    if (CardSide.FRONT == response.getCardSide()) {
        response.setName(resp.getName());
        response.setSex(resp.getSex());
        response.setNation(resp.getNation());
        this.setBirthday(response, resp.getBirth());
        response.setAddress(resp.getAddress());
        response.setIdNum(resp.getIdNum());
    }
    // 设置反面信息
    if (CardSide.BACK == response.getCardSide()) {
        response.setAuthority(resp.getAuthority());
        this.setValidDate(response, resp.getValidDate());
    }
    // 设置高级功能信息
    this.setResponseAdvancedInfo(response, resp.getAdvancedInfo(), config.getMinQuality());
    // 返回响应
    return response;
}
Also used : IdCardOcrSdkException(icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkException) IDCardOCRRequest(com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) IdCardOcrSdkClientException(icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkClientException) IdCardOcrAdvanced(icu.easyj.sdk.ocr.idcardocr.IdCardOcrAdvanced) IdCardOcrSdkServerException(icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkServerException) TencentCloudIdCardOcrConfig(icu.easyj.sdk.tencent.cloud.ocr.idcardocr.TencentCloudIdCardOcrConfig) IdCardOcrRequestBuilder(icu.easyj.sdk.tencent.cloud.ocr.idcardocr.IdCardOcrRequestBuilder) IdCardOcrResponse(icu.easyj.sdk.ocr.idcardocr.IdCardOcrResponse) CardSide(icu.easyj.sdk.ocr.CardSide) IDCardOCRResponse(com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse) NonNull(org.springframework.lang.NonNull)

Example 2 with IdCardOcrSdkException

use of icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkException in project easyj by easyj-projects.

the class TencentEasyjIdCardOcrTemplateImpl method setValidDate.

/**
 * 设置有效期限
 *
 * @param response  响应
 * @param validDate 有效期限字符串,格式有两种可能:`yyyy.mm.dd-yyyy.mm.dd` 或 `yyyy.mm.dd-长期`
 * @throws IdCardOcrSdkException 解析有效期限异常
 */
private void setValidDate(IdCardOcrResponse response, String validDate) throws IdCardOcrSdkException {
    String[] validDateArr = validDate.split(StrPool.DASHED);
    try {
        String validDateStartStr = validDateArr[0];
        String validDateEndStr = validDateArr[1];
        Date validDateStart = DateUtils.parseDate3(validDateStartStr);
        Date validDateEnd = "长期".equals(validDateEndStr) ? null : DateUtils.parseDate3(validDateEndStr);
        response.setValidDateStart(validDateStart);
        response.setValidDateEnd(validDateEnd);
    } catch (ParseException e) {
        throw new IdCardOcrSdkException("身份证有效期限解析失败:" + validDate, "PARSE_VALID_DATE_FAILED", e);
    }
}
Also used : IdCardOcrSdkException(icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkException) ParseException(java.text.ParseException) Date(java.util.Date)

Aggregations

IdCardOcrSdkException (icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkException)2 TencentCloudSDKException (com.tencentcloudapi.common.exception.TencentCloudSDKException)1 IDCardOCRRequest (com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest)1 IDCardOCRResponse (com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse)1 CardSide (icu.easyj.sdk.ocr.CardSide)1 IdCardOcrAdvanced (icu.easyj.sdk.ocr.idcardocr.IdCardOcrAdvanced)1 IdCardOcrResponse (icu.easyj.sdk.ocr.idcardocr.IdCardOcrResponse)1 IdCardOcrSdkClientException (icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkClientException)1 IdCardOcrSdkServerException (icu.easyj.sdk.ocr.idcardocr.IdCardOcrSdkServerException)1 IdCardOcrRequestBuilder (icu.easyj.sdk.tencent.cloud.ocr.idcardocr.IdCardOcrRequestBuilder)1 TencentCloudIdCardOcrConfig (icu.easyj.sdk.tencent.cloud.ocr.idcardocr.TencentCloudIdCardOcrConfig)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1 NonNull (org.springframework.lang.NonNull)1