本教程是识别行驶证的正副页面,要识别不同的图片根据文档调用不同的对象
配套前端是笔者发布的上一篇文章:uniapp调用手机的拍照和上传功能
- 引入SDK
- OCR文字识别API
- 对接OCR车辆行驶证接口
- 解析返回数据进行数据封装
引入SDK
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-ocr</artifactId>
<version>3.1.1153</version>
</dependency>
OCR文字识别API
由于官方提供了SDK,所以我直接使用,也可以自己通过HTTP请求来进行文字识别
获取自己腾讯云密钥,建议使用子账号,赋予ocr权限
在自己的配置文件中进行配置
tencent:
sercretKey: xxx
sercretValue: xxx
属性绑定
@Component
@ConfigurationProperties(prefix = "tencent")
@Data
public class TencentOcr {
private String sercretKey;
private String sercretValue;
}
控制器编写
笔者需要index来判断正反面
@PassToken
@PostMapping("/identityDriverLicense")
@Operation(summary = "识别行驶证", description = "识别行驶证")
public Result<Void> identityDriverLicense(MultipartFile file,Integer index){
userService.identityDriverLicense(file,index);
return Result.success();
}
业务层编写
@Override
public void identityDriverLicense(MultipartFile file, Integer index) {
try {
byte[] bytes = file.getBytes();
String base64Img = Base64.getEncoder().encodeToString(bytes);
ocrClient.getVehicleLicenseInfo(base64Img,index);
} catch (IOException e) {
log.info(e.getMessage());
}
}
工具类编写
- JsonUtil:自定义JSON处理工具类
- LicenseUtil:自定义response处理工具类
- DriverLicenseFront:封装响应数据的对象
- DriverLicenseBack:封装响应数据的对象
- DriverLicense:自定义枚举,杜绝魔法数字
- getVehicleRegisterCert:获取车辆登记证书的接口
@Component
@Slf4j
public class OcrClient {
private final TencentOcr tencentOcr;
@Autowired
public OcrClient(TencentOcr tencentOcr) {
this.tencentOcr = tencentOcr;
}
public Object getVehicleLicenseInfo(String base64Img,Integer index){
try {
VehicleLicenseOCRResponse response = performOcrRequest(base64Img, index);
String json = JsonUtil.objectToJson(response);
if (index == DriverLicense.FRONT.type()){
DriverLicenseFront driverLicenseFront = LicenseUtil.getVehicleFront(json);
return driverLicenseFront;
} else if (index == DriverLicense.BACK.type()) {
DriverLicenseBack vehicleInfoBack = LicenseUtil.getVehicleInfoBack(json);
return vehicleInfoBack;
}
} catch (TencentCloudSDKException e) {
log.info(e.getMessage());
throw new GlobalException(504,"请选择正确的行驶证正副页");
}
return null;
}
public Object getVehicleRegisterCert(String base64Img){
try {
com.tencentcloudapi.ocr.v20181119.OcrClient client = createClient();
VehicleRegCertOCRRequest request = new VehicleRegCertOCRRequest();
request.setImageBase64(base64Img);
VehicleRegCertOCRResponse response = client.VehicleRegCertOCR(request);
String json = JsonUtil.objectToJson(response);
System.out.println(json);
} catch (TencentCloudSDKException e) {
throw new GlobalException(504,"请选择有效的车辆登记合格证书");
}
return null;
}
private VehicleLicenseOCRResponse performOcrRequest(String base64Img, Integer index) throws TencentCloudSDKException {
com.tencentcloudapi.ocr.v20181119.OcrClient client = createClient();
VehicleLicenseOCRRequest request = new VehicleLicenseOCRRequest();
request.setImageBase64(base64Img);
if (index == DriverLicense.FRONT.type()) {
request.setCardSide("FRONT");
} else if (index == DriverLicense.BACK.type()) {
request.setCardSide("BACK");
} else {
throw new GlobalException(504, "无效的行驶证面类型: " + index);
}
return client.VehicleLicenseOCR(request);
}
private com.tencentcloudapi.ocr.v20181119.OcrClient createClient() throws TencentCloudSDKException {
Credential cred = new Credential(tencentOcr.getId(), tencentOcr.getSecret());
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("ocr.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
com.tencentcloudapi.ocr.v20181119.OcrClient client = new com.tencentcloudapi.ocr.v20181119.OcrClient(cred, "ap-guangzhou", clientProfile);
return client;
}
}
如需笔者的工具类等,请私信笔者。