新增了图像识别
This commit is contained in:
parent
7356d6806e
commit
b2e9b7fff5
@ -30,4 +30,10 @@ public class Base {
|
||||
ocrClient.setConnectionTimeoutInMillis(2000);
|
||||
ocrClient.setSocketTimeoutInMillis(60000);
|
||||
}
|
||||
|
||||
// 注意:项目使用log4j2,百度AI SDK会自动检测log4j2配置文件
|
||||
// 如果确实需要手动指定log4j2配置文件路径,可以使用以下方式:
|
||||
// System.setProperty("log4j.configurationFile", "classpath:dev/log4j2.xml");
|
||||
// 或者完全移除这行配置,让log4j2自动检测
|
||||
// }
|
||||
}
|
||||
|
||||
@ -5,7 +5,9 @@ import io.swagger.annotations.ApiOperation;
|
||||
import net.lab1024.sa.admin.constant.AdminSwaggerTagConst;
|
||||
import net.lab1024.sa.admin.extend.baidubce.Ocr;
|
||||
import net.lab1024.sa.admin.module.app.file.domain.form.GetIdCardOcrForm;
|
||||
import net.lab1024.sa.admin.module.app.file.domain.form.GetImageOcrForm;
|
||||
import net.lab1024.sa.admin.module.app.file.domain.vo.GetIdCardOcrVo;
|
||||
import net.lab1024.sa.admin.module.app.file.domain.vo.GetImageOcrVo;
|
||||
import net.lab1024.sa.admin.module.app.file.service.OcrService;
|
||||
import net.lab1024.sa.admin.module.business.bankcard.domain.form.CaseplatformBankAddForm;
|
||||
import net.lab1024.sa.common.common.domain.ResponseDTO;
|
||||
@ -47,19 +49,19 @@ public class ImageController {
|
||||
|
||||
@ApiOperation(value = "通用文字识别-ocr")
|
||||
@PostMapping("/ocr/image")
|
||||
public ResponseDTO<GetIdCardOcrVo> getImageOcr(
|
||||
@RequestBody @Valid GetIdCardOcrForm form
|
||||
public ResponseDTO<GetImageOcrVo> getImageOcr(
|
||||
@RequestBody @Valid GetImageOcrForm form
|
||||
){
|
||||
if (form.getFrontBase64() == null || form.getFrontBase64().isEmpty()) {
|
||||
if (form.getImageBase64() == null || form.getImageBase64().isEmpty()) {
|
||||
return ResponseDTO.userErrorParam("参数错误");
|
||||
}
|
||||
|
||||
try {
|
||||
// 解码 Base64 字符串为字节数组
|
||||
byte[] frontBytes = Base64.getDecoder().decode(form.getFrontBase64());
|
||||
byte[] imageBytes = Base64.getDecoder().decode(form.getImageBase64());
|
||||
|
||||
// 调用 Service
|
||||
GetIdCardOcrVo result = ocrService.getIdCardOcr(frontBytes);
|
||||
GetImageOcrVo result = ocrService.getImageOcr(imageBytes);
|
||||
return ResponseDTO.app_ok(result);
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
@ -0,0 +1,13 @@
|
||||
package net.lab1024.sa.admin.module.app.file.domain.form;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Data
|
||||
public class GetImageOcrForm {
|
||||
@ApiModelProperty("图片")
|
||||
@NotBlank(message = "图片 不能为空")
|
||||
private String imageBase64;
|
||||
}
|
||||
@ -1,14 +1,24 @@
|
||||
package net.lab1024.sa.admin.module.app.file.domain.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.Size;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class GetImageOcrVo {
|
||||
@ApiModelProperty(value = "身份证姓名")
|
||||
private String idCardName;
|
||||
@ApiModelProperty(value = "识别结果数,表示words_result的元素个数")
|
||||
private String wordsResultNum;
|
||||
|
||||
@ApiModelProperty(value = "身份证号")
|
||||
private String idCardNo;
|
||||
@ApiModelProperty(value = "定位和识别结果数组")
|
||||
private List<wordsResultData> wordsResult;
|
||||
|
||||
@Data
|
||||
public static class wordsResultData {
|
||||
@JsonProperty("words")
|
||||
private String words; // 识别结果字符串
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -5,13 +5,17 @@ import net.lab1024.sa.admin.extend.fangxinqian.company.Company;
|
||||
import net.lab1024.sa.admin.module.app.expert.admin.ExpertSignVO;
|
||||
import net.lab1024.sa.admin.module.app.expert.dao.ExpertSignDao;
|
||||
import net.lab1024.sa.admin.module.app.file.domain.vo.GetIdCardOcrVo;
|
||||
import net.lab1024.sa.admin.module.app.file.domain.vo.GetImageOcrVo;
|
||||
import net.lab1024.sa.common.common.exception.BusinessException;
|
||||
import net.lab1024.sa.common.common.util.SmartRequestUtil;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class OcrService {
|
||||
@ -75,4 +79,44 @@ public class OcrService {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用文字识别
|
||||
*/
|
||||
public GetImageOcrVo getImageOcr(byte[] imageBytes) {
|
||||
try {
|
||||
JSONObject result = ocr.image(imageBytes);
|
||||
|
||||
// 校验返回是否包含识别内容
|
||||
int resultNum = result.optInt("words_result_num", 0);
|
||||
if (resultNum == 0 || !result.has("words_result")) {
|
||||
throw new BusinessException("识别失败:未检测到有效文字");
|
||||
}
|
||||
|
||||
JSONArray wordsArray = result.getJSONArray("words_result");
|
||||
List<GetImageOcrVo.wordsResultData> wordList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < wordsArray.length(); i++) {
|
||||
JSONObject wordObj = wordsArray.getJSONObject(i);
|
||||
String word = wordObj.optString("words", "");
|
||||
if (!word.isEmpty()) {
|
||||
GetImageOcrVo.wordsResultData data = new GetImageOcrVo.wordsResultData();
|
||||
data.setWords(word);
|
||||
wordList.add(data);
|
||||
}
|
||||
}
|
||||
|
||||
if (wordList.isEmpty()) {
|
||||
throw new BusinessException("识别失败:没有提取到有效文字");
|
||||
}
|
||||
|
||||
GetImageOcrVo vo = new GetImageOcrVo();
|
||||
vo.setWordsResultNum(String.valueOf(wordList.size()));
|
||||
vo.setWordsResult(wordList);
|
||||
|
||||
return vo;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user