feat: 身份证识别
This commit is contained in:
parent
b670ee61c1
commit
684d7aa7b2
|
@ -1,7 +1,6 @@
|
||||||
package com.atguigu.daijia.driver.client;
|
package com.atguigu.daijia.driver.client;
|
||||||
|
|
||||||
import com.atguigu.daijia.common.result.Result;
|
import com.atguigu.daijia.common.result.Result;
|
||||||
import com.atguigu.daijia.model.vo.driver.DriverLicenseOcrVo;
|
|
||||||
import com.atguigu.daijia.model.vo.driver.IdCardOcrVo;
|
import com.atguigu.daijia.model.vo.driver.IdCardOcrVo;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
@ -12,5 +11,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
@FeignClient(value = "service-driver")
|
@FeignClient(value = "service-driver")
|
||||||
public interface OcrFeignClient {
|
public interface OcrFeignClient {
|
||||||
|
|
||||||
|
@PostMapping(value = "/ocr/idCardOcr", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
|
Result<IdCardOcrVo> idCardOcr(@RequestPart("file") MultipartFile file);
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,9 +1,17 @@
|
||||||
package com.atguigu.daijia.driver.controller;
|
package com.atguigu.daijia.driver.controller;
|
||||||
|
|
||||||
|
import com.atguigu.daijia.common.result.Result;
|
||||||
|
import com.atguigu.daijia.driver.service.OcrService;
|
||||||
|
import com.atguigu.daijia.model.vo.driver.IdCardOcrVo;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestPart;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Tag(name = "腾讯云识别接口管理")
|
@Tag(name = "腾讯云识别接口管理")
|
||||||
|
@ -11,6 +19,14 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
@RequestMapping(value = "/ocr")
|
@RequestMapping(value = "/ocr")
|
||||||
public class OcrController {
|
public class OcrController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OcrService ocrService;
|
||||||
|
|
||||||
|
@Operation(summary = "身份证识别")
|
||||||
|
@PostMapping("/idCardOcr")
|
||||||
|
public Result<IdCardOcrVo> idCardOcr(@RequestPart("file") MultipartFile file) {
|
||||||
|
IdCardOcrVo idCardOcrVo = ocrService.idCardOcr(file);
|
||||||
|
return Result.ok(idCardOcrVo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,16 @@
|
||||||
package com.atguigu.daijia.driver.service;
|
package com.atguigu.daijia.driver.service;
|
||||||
|
|
||||||
|
import com.atguigu.daijia.model.vo.driver.IdCardOcrVo;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
public interface OcrService {
|
public interface OcrService {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 身份证识别
|
||||||
|
*
|
||||||
|
* @param file 身份认证识别
|
||||||
|
* @return 身份识别返回信息
|
||||||
|
*/
|
||||||
|
IdCardOcrVo idCardOcr(MultipartFile file);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,96 @@
|
||||||
package com.atguigu.daijia.driver.service.impl;
|
package com.atguigu.daijia.driver.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.common.codec.Base64;
|
||||||
|
import com.atguigu.daijia.common.execption.GuiguException;
|
||||||
|
import com.atguigu.daijia.common.result.ResultCodeEnum;
|
||||||
|
import com.atguigu.daijia.driver.config.TencentCloudProperties;
|
||||||
|
import com.atguigu.daijia.driver.service.CosService;
|
||||||
import com.atguigu.daijia.driver.service.OcrService;
|
import com.atguigu.daijia.driver.service.OcrService;
|
||||||
|
import com.atguigu.daijia.model.vo.driver.CosUploadVo;
|
||||||
|
import com.atguigu.daijia.model.vo.driver.IdCardOcrVo;
|
||||||
|
import com.tencentcloudapi.common.Credential;
|
||||||
|
import com.tencentcloudapi.common.profile.ClientProfile;
|
||||||
|
import com.tencentcloudapi.common.profile.HttpProfile;
|
||||||
|
import com.tencentcloudapi.ocr.v20181119.OcrClient;
|
||||||
|
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
|
||||||
|
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.joda.time.format.DateTimeFormat;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
||||||
public class OcrServiceImpl implements OcrService {
|
public class OcrServiceImpl implements OcrService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TencentCloudProperties tencentCloudProperties;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CosService cosService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 身份证识别
|
||||||
|
*
|
||||||
|
* @param file 身份认证识别
|
||||||
|
* @return 身份识别返回信息
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public IdCardOcrVo idCardOcr(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对象
|
||||||
|
IDCardOCRRequest req = new IDCardOCRRequest();
|
||||||
|
// 设置文件
|
||||||
|
req.setImageBase64(fileBase64);
|
||||||
|
|
||||||
|
// 返回的resp是一个IDCardOCRResponse的实例,与请求对象对应
|
||||||
|
IDCardOCRResponse resp = client.IDCardOCR(req);
|
||||||
|
|
||||||
|
// 转换为IdCardOcrVo对象
|
||||||
|
IdCardOcrVo idCardOcrVo = new IdCardOcrVo();
|
||||||
|
if (StringUtils.hasText(resp.getName())) {
|
||||||
|
// 身份证正面
|
||||||
|
idCardOcrVo.setName(resp.getName());
|
||||||
|
idCardOcrVo.setGender("男".equals(resp.getSex()) ? "1" : "2");
|
||||||
|
idCardOcrVo.setBirthday(DateTimeFormat.forPattern("yyyy/MM/dd").parseDateTime(resp.getBirth()).toDate());
|
||||||
|
idCardOcrVo.setIdcardNo(resp.getIdNum());
|
||||||
|
idCardOcrVo.setIdcardAddress(resp.getAddress());
|
||||||
|
|
||||||
|
// 上传身份证正面图片到腾讯云cos
|
||||||
|
CosUploadVo cosUploadVo = cosService.upload(file, "idCard");
|
||||||
|
idCardOcrVo.setIdcardFrontUrl(cosUploadVo.getUrl());
|
||||||
|
idCardOcrVo.setIdcardFrontShowUrl(cosUploadVo.getShowUrl());
|
||||||
|
} else {
|
||||||
|
// 身份证反面
|
||||||
|
// 证件有效期:"2010.07.21-2020.07.21"
|
||||||
|
String idcardExpireString = resp.getValidDate().split("-")[1];
|
||||||
|
idCardOcrVo.setIdcardExpire(DateTimeFormat.forPattern("yyyy.MM.dd").parseDateTime(idcardExpireString).toDate());
|
||||||
|
// 上传身份证反面图片到腾讯云cos
|
||||||
|
CosUploadVo cosUploadVo = cosService.upload(file, "idCard");
|
||||||
|
idCardOcrVo.setIdcardBackUrl(cosUploadVo.getUrl());
|
||||||
|
idCardOcrVo.setIdcardBackShowUrl(cosUploadVo.getShowUrl());
|
||||||
|
}
|
||||||
|
return idCardOcrVo;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new GuiguException(ResultCodeEnum.DATA_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,32 @@
|
||||||
package com.atguigu.daijia.driver.controller;
|
package com.atguigu.daijia.driver.controller;
|
||||||
|
|
||||||
|
import com.atguigu.daijia.common.result.Result;
|
||||||
import com.atguigu.daijia.driver.service.OcrService;
|
import com.atguigu.daijia.driver.service.OcrService;
|
||||||
|
import com.atguigu.daijia.model.vo.driver.IdCardOcrVo;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestPart;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Tag(name = "腾讯云识别接口管理")
|
@Tag(name = "腾讯云识别接口管理")
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(value="/ocr")
|
@RequestMapping(value = "/ocr")
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
||||||
public class OcrController {
|
public class OcrController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OcrService ocrService;
|
||||||
|
|
||||||
|
@Operation(summary = "身份证识别")
|
||||||
|
//@GuiguLogin
|
||||||
|
@PostMapping("/idCardOcr")
|
||||||
|
public Result<IdCardOcrVo> uploadDriverLicenseOcr(@RequestPart("file") MultipartFile file) {
|
||||||
|
return Result.ok(ocrService.idCardOcr(file));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,15 @@
|
||||||
package com.atguigu.daijia.driver.service;
|
package com.atguigu.daijia.driver.service;
|
||||||
|
|
||||||
|
import com.atguigu.daijia.model.vo.driver.IdCardOcrVo;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
public interface OcrService {
|
public interface OcrService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 身份证识别
|
||||||
|
*
|
||||||
|
* @param file 身份认证识别
|
||||||
|
* @return 身份识别返回信息
|
||||||
|
*/
|
||||||
|
IdCardOcrVo idCardOcr(MultipartFile file);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,30 @@
|
||||||
package com.atguigu.daijia.driver.service.impl;
|
package com.atguigu.daijia.driver.service.impl;
|
||||||
|
|
||||||
|
import com.atguigu.daijia.common.result.Result;
|
||||||
|
import com.atguigu.daijia.driver.client.OcrFeignClient;
|
||||||
import com.atguigu.daijia.driver.service.OcrService;
|
import com.atguigu.daijia.driver.service.OcrService;
|
||||||
|
import com.atguigu.daijia.model.vo.driver.IdCardOcrVo;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
||||||
public class OcrServiceImpl implements OcrService {
|
public class OcrServiceImpl implements OcrService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OcrFeignClient ocrFeignClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 身份证识别
|
||||||
|
*
|
||||||
|
* @param file 身份认证识别
|
||||||
|
* @return 身份识别返回信息
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public IdCardOcrVo idCardOcr(MultipartFile file) {
|
||||||
|
Result<IdCardOcrVo> ocrVoResult = ocrFeignClient.idCardOcr(file);
|
||||||
|
return ocrVoResult.getData();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue