use of icu.easyj.sdk.ocr.CardSide 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;
}
use of icu.easyj.sdk.ocr.CardSide in project easyj by easyj-projects.
the class IIdCardOcrTemplate method idCardOcr.
// endregion
// region 正反两面一起识别,拿到一个完整的响应信息
/**
* 身份证识别(两面一起发送识别)
*
* @param image1 身份证正面或反面图片的Base64串或URL地址,两张图片入参顺序不做要求,但必须是不同的两面图片
* @param image2 身份证正面或反面图片的Base64串或URL地址,两张图片入参顺序不做要求,但必须是不同的两面图片
* @param returnIfHasWarn 如果第一张图片存在告警信息,则不再继续识别第二张图片了
* @param simpleRequest 少量的身份证识别请求信息
* @return response 响应
* @throws IdCardOcrSdkException 身份证识别异常
*/
@NonNull
default IdCardOcrResponse idCardOcr(@NonNull String image1, @NonNull String image2, boolean returnIfHasWarn, SimpleIdCardOcrRequest simpleRequest) throws IdCardOcrSdkException {
IdCardOcrRequest request = new IdCardOcrRequest(simpleRequest);
// region 请求识别第一张图片,获取response1
request.setImage(image1);
IdCardOcrResponse response1 = this.idCardOcr(request);
// 如果`returnIfHasWarn=true`,且存在告警信息,则直接返回
if (returnIfHasWarn && !response1.getWarns().isEmpty()) {
return response1;
}
// endregion 请求第一张 end
// region 请求识别第二张图片,获取response2
// 根据第一张图片的解析结果,设置第二张图片的卡片正反面参数
CardSide cardSide2 = CardSide.FRONT;
if (CardSide.FRONT == response1.getCardSide()) {
cardSide2 = CardSide.BACK;
}
// 请求识别第二张图片
// 覆盖图片参数
request.setImage(image2);
// 设置正反面参数
request.setCardSide(cardSide2);
IdCardOcrResponse response2 = this.idCardOcr(request);
// 如果两张图片属于同一面,则抛出异常
if (response2.getCardSide() == response1.getCardSide()) {
throw new IdCardOcrSdkException("两张图片的正反面属性相同", "SAME_CARD_SIDE");
}
// endregion 请求第二张 end
// region 合并两张图片的响应内容,使响应信息包含身份证正反两面的信息
// 先确定正反面响应
// 双面响应,先将正面赋值给它
IdCardOcrResponse doubleResponse;
// 反面响应
IdCardOcrResponse backResponse;
if (CardSide.FRONT == response1.getCardSide()) {
doubleResponse = response1;
backResponse = response2;
} else {
doubleResponse = response2;
backResponse = response1;
}
// 设置为正反面都有
doubleResponse.setCardSide(CardSide.BOTH);
// //region 将反面信息合并到双面响应中
doubleResponse.setAuthority(backResponse.getAuthority());
doubleResponse.setValidDateStart(backResponse.getValidDateStart());
doubleResponse.setValidDateEnd(backResponse.getValidDateEnd());
// 将反面信息的`idCardBase64`属性,设置到`backIdCardBase64`属性中
doubleResponse.setBackIdCardBase64(backResponse.getIdCardBase64());
// 告警信息
doubleResponse.getWarns().addAll(backResponse.getWarns());
return doubleResponse;
}
Aggregations