新增了身份证识别,去除了反面

This commit is contained in:
wucongxing8150 2025-06-30 16:52:01 +08:00
parent 6cc5dc58de
commit 6119e760d3
2 changed files with 5 additions and 49 deletions

View File

@ -24,23 +24,17 @@ public class ImageController {
@ApiOperation(value = "身份证识别-ocr") @ApiOperation(value = "身份证识别-ocr")
@PostMapping("/ocr/idCard") @PostMapping("/ocr/idCard")
public ResponseDTO<GetIdCardOcrVo> getIdCardOcr( public ResponseDTO<GetIdCardOcrVo> getIdCardOcr(
@RequestParam("frontFile") MultipartFile frontFile, @RequestParam("frontFile") MultipartFile frontFile
@RequestParam("backFile") MultipartFile backFile
){ ){
if (frontFile == null || frontFile.isEmpty()) { if (frontFile == null || frontFile.isEmpty()) {
return ResponseDTO.userErrorParam("参数错误"); return ResponseDTO.userErrorParam("参数错误");
} }
if (backFile == null || backFile.isEmpty()) {
return ResponseDTO.userErrorParam("参数错误");
}
try { try {
byte[] frontBytes = frontFile.getBytes(); byte[] frontBytes = frontFile.getBytes();
byte[] backBytes = backFile.getBytes();
// 调用 Service // 调用 Service
GetIdCardOcrVo result = ocrService.getIdCardOcr(frontBytes, backBytes); GetIdCardOcrVo result = ocrService.getIdCardOcr(frontBytes);
return ResponseDTO.app_ok(result); return ResponseDTO.app_ok(result);
} catch (Exception e) { } catch (Exception e) {

View File

@ -22,10 +22,10 @@ public class OcrService {
/** /**
* 身份证识别 * 身份证识别
*/ */
public GetIdCardOcrVo getIdCardOcr(byte[] frontBytes,byte[] backBytes) { public GetIdCardOcrVo getIdCardOcr(byte[] frontBytes) {
try { try {
// //
JSONObject result = ocr.idCard(backBytes,"back"); JSONObject result = ocr.idCard(frontBytes,"front");
String imageStatus = result.getString("image_status"); String imageStatus = result.getString("image_status");
switch (imageStatus) { switch (imageStatus) {
@ -62,44 +62,6 @@ public class OcrService {
} }
} }
// 正面
result = ocr.idCard(frontBytes,"front");
imageStatus = result.getString("image_status");
switch (imageStatus) {
case "normal":
// 识别正常继续处理
break;
case "reversed_side":
throw new BusinessException("身份证未摆正,请重新上传");
case "non_idcard":
throw new BusinessException("上传的图片中不包含身份证,请上传正确图片");
case "blurred":
throw new BusinessException("身份证模糊,请重新上传清晰图片");
case "over_exposure":
throw new BusinessException("身份证关键字段反光或过曝,请重新拍摄");
case "unknown":
default:
throw new BusinessException("无法识别身份证,请重新上传");
}
if (result.has("risk_type")) {
String riskType = result.getString("risk_type");
if (!"normal".equals(riskType)) {
switch (riskType) {
case "copy":
throw new BusinessException("检测到复印件身份证,请上传原件");
case "temporary":
throw new BusinessException("检测到临时身份证,请上传正式身份证");
case "screen":
throw new BusinessException("检测到翻拍身份证,请上传原件照片");
case "unknow":
default:
throw new BusinessException("身份证真实性存疑,请重新上传");
}
}
}
// 解析识别结果 // 解析识别结果
String name = result.getJSONObject("words_result").getJSONObject("姓名").getString("words"); String name = result.getJSONObject("words_result").getJSONObject("姓名").getString("words");
String idNo = result.getJSONObject("words_result").getJSONObject("公民身份号码").getString("words"); String idNo = result.getJSONObject("words_result").getJSONObject("公民身份号码").getString("words");