官方地址:
API Explorer - 云 API - 控制臺https://console.cloud.tencent.com/api/explorer?Product=ocr&Version=2018-11-19&Action=DriverLicenseOCR前置操作與下面博客前置操作一致:實名認證 —— 騰訊云身份證認證接口-CSDN博客
首先編寫Controller:
@Operation(summary = "駕駛證識別")
@PostMapping("/driverLicenseOcr")
public Result<DriverLicenseOcrVo> driverLicenseOcr(@RequestPart("file") MultipartFile file) {return Result.ok(ocrService.driverLicenseOcr(file));
}
隨后編寫Service:
步驟解析:與實名認證 —— 騰訊云身份證認證接口-CSDN博客幾乎一致
////駕駛證識別@Overridepublic DriverLicenseOcrVo driverLicenseOcr(MultipartFile file) {try{//圖片轉換base64格式字符串byte[] base64 = Base64.encodeBase64(file.getBytes());String fileBase64 = new String(base64);// 實例化一個認證對象,入參需要傳入騰訊云賬戶 SecretId 和 SecretKey,此處還需注意密鑰對的保密Credential cred = new Credential(tencentCloudProperties.getSecretId(),tencentCloudProperties.getSecretKey());// 實例化一個http選項,可選的,沒有特殊需求可以跳過HttpProfile httpProfile = new HttpProfile();httpProfile.setEndpoint("ocr.tencentcloudapi.com");// 實例化一個client選項,可選的,沒有特殊需求可以跳過ClientProfile clientProfile = new ClientProfile();clientProfile.setHttpProfile(httpProfile);// 實例化要請求產品的client對象,clientProfile是可選的OcrClient client = new OcrClient(cred, tencentCloudProperties.getRegion(),clientProfile);// 實例化一個請求對象,每個接口都會對應一個request對象DriverLicenseOCRRequest req = new DriverLicenseOCRRequest();req.setImageBase64(fileBase64);// 返回的resp是一個DriverLicenseOCRResponse的實例,與請求對象對應DriverLicenseOCRResponse resp = client.DriverLicenseOCR(req);//封裝到vo對象里面DriverLicenseOcrVo driverLicenseOcrVo = new DriverLicenseOcrVo();if (StringUtils.hasText(resp.getName())) {//駕駛證正面//駕駛證名稱要與身份證名稱一致driverLicenseOcrVo.setName(resp.getName());driverLicenseOcrVo.setDriverLicenseClazz(resp.getClass_());driverLicenseOcrVo.setDriverLicenseNo(resp.getCardCode());driverLicenseOcrVo.setDriverLicenseIssueDate(DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(resp.getDateOfFirstIssue()).toDate());driverLicenseOcrVo.setDriverLicenseExpire(DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime(resp.getEndDate()).toDate());//上傳駕駛證反面圖片到騰訊云cosCosUploadVo cosUploadVo = cosService.upload(file, "driverLicense");driverLicenseOcrVo.setDriverLicenseFrontUrl(cosUploadVo.getUrl());driverLicenseOcrVo.setDriverLicenseFrontShowUrl(cosUploadVo.getShowUrl());} else {//駕駛證反面//上傳駕駛證反面圖片到騰訊云cosCosUploadVo cosUploadVo = cosService.upload(file, "driverLicense");driverLicenseOcrVo.setDriverLicenseBackUrl(cosUploadVo.getUrl());driverLicenseOcrVo.setDriverLicenseBackShowUrl(cosUploadVo.getShowUrl());}return driverLicenseOcrVo;} catch (Exception e) {e.printStackTrace();throw new GuiguException(ResultCodeEnum.DATA_ERROR);}}
隨后編寫feign:
/*** 駕駛證識別* @param file* @return*/
@PostMapping(value = "/ocr/driverLicenseOcr", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Result<DriverLicenseOcrVo> driverLicenseOcr(@RequestPart("file") MultipartFile file);