qiubh 2 місяці тому
джерело
коміт
65fc86d7be

+ 1
- 0
zhywpt-app-iaw/build.gradle Переглянути файл

@@ -10,6 +10,7 @@ dependencies {
implementation "${groupname}:comm-core:1.0.0-SNAPSHOT"
implementation "${groupname}:sample-protocol:1.0.0-SNAPSHOT"
implementation "${groupname}:ias-protocol:1.0.0-SNAPSHOT"
implementation "${groupname}:ats-protocol:1.0.0-SNAPSHOT"
implementation "${groupname}:smp-protocol:1.0.0-SNAPSHOT"
implementation('cn.com.taiji.common:sso-client:2.3.10.7')
implementation "org.springframework.boot:spring-boot-starter-security"

+ 34
- 0
zhywpt-app-iaw/src/main/java/cn/com/taiji/iaw/api/comm/OcrController.java Переглянути файл

@@ -0,0 +1,34 @@
package cn.com.taiji.iaw.api.comm;

import cn.com.taiji.common.manager.ManagerException;
import cn.com.taiji.common.web.ApiResponse;
import cn.com.taiji.core.model.comm.protocol.ats.AbstractAtsResponse;
import cn.com.taiji.iaw.api.MyValidController;
import cn.com.taiji.iaw.dto.comm.OcrRequestDTO;
import cn.com.taiji.iaw.manager.comm.OcrManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(tags = {"OCR服务"})
@RestController
@RequestMapping("/api/ocr")
public class OcrController extends MyValidController {

@Autowired
private OcrManager manager;

@ApiOperation(value = "CR识别接口")
@PostMapping(value = "/do")
public ApiResponse<AbstractAtsResponse> doOcr(@Valid @RequestBody OcrRequestDTO reqDto) throws ManagerException {
AbstractAtsResponse resDto = manager.ocr(reqDto);
return ApiResponse.of(resDto).setMessage("识别成功");
}

}

+ 33
- 0
zhywpt-app-iaw/src/main/java/cn/com/taiji/iaw/dto/comm/OcrRequestDTO.java Переглянути файл

@@ -0,0 +1,33 @@
package cn.com.taiji.iaw.dto.comm;

import cn.com.taiji.common.valid.BaseValidDTO;
import cn.com.taiji.common.valid.ViolationValidator;
import cn.com.taiji.core.model.comm.protocol.constraint.IntegerConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotNull;

@ApiModel(description = "文本管理查询请求")
@Getter
@Setter
@Accessors(chain = true)
public class OcrRequestDTO extends BaseValidDTO {


@NotNull
@IntegerConstant(values = "1,2,3,4,5")
@ApiModelProperty(value = "识别类型1-身份证-人像面 2-身份证-国徽面 3-行驶证前页 4-行驶证背页 5-营业执照")
private Integer type;
@NotNull
@ApiModelProperty(value = "图片地址")
private String imagePath;

@Override
protected void validate(ViolationValidator validator) {

}
}

+ 105
- 0
zhywpt-app-iaw/src/main/java/cn/com/taiji/iaw/manager/AbstractIawManager.java Переглянути файл

@@ -0,0 +1,105 @@
package cn.com.taiji.iaw.manager;

import cn.com.taiji.common.manager.ManagerException;
import cn.com.taiji.common.manager.net.http.ServiceHandleException;
import cn.com.taiji.common.pub.json.JsonTools;
import cn.com.taiji.core.entity.comm.OcrResult;
import cn.com.taiji.core.entity.dict.OcrType;
import cn.com.taiji.core.model.comm.protocol.ats.AbstractAtsResponse;
import cn.com.taiji.core.model.comm.protocol.ats.ocr.*;
import cn.com.taiji.core.repo.jpa.comm.OcrResultRepo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.time.LocalDateTime;

public abstract class AbstractIawManager extends AbstractCommManager {

@Autowired
private OcrResultRepo ocrResultRepo;

/**
* type:1-身份证-人像面 2-身份证-国徽面 3-行驶证前页 4-行驶证背页 5-营业执照
* imagePath:图片地址
*/
protected AbstractAtsResponse getOcrResult(Integer type, String imagePath) throws ManagerException, IOException {
OcrResult ocrResult = new OcrResult();
ocrResult.setCreateTime(LocalDateTime.now());
ocrResult.setUrl(imagePath);
ocrResult.setOcrType(OcrType.findByCode(type));
// 相同的图片,直接取已有的结果
OcrResult exist = ocrResultRepo.findByUrlAndType(ocrResult.getUrl(), ocrResult.getOcrType());
switch (ocrResult.getOcrType()) {
case id_front:
if (exist != null)
return JsonTools.json2Object(exist.getResult(), IdCardOcrResponse.class);
return doIdCardOcr(1, imagePath, ocrResult);
case id_back:
if (exist != null)
return JsonTools.json2Object(exist.getResult(), IdCardOcrResponse.class);
return doIdCardOcr(2, imagePath, ocrResult);
case vehicle_license_front:
if (exist != null)
return JsonTools.json2Object(exist.getResult(), VehicleLicenseOcrResponse.class);
return doVehicleLicenseOcr(1, imagePath, ocrResult);
case vehicle_license_back:
if (exist != null)
return JsonTools.json2Object(exist.getResult(), VehicleLicenseOcrResponse.class);
return doVehicleLicenseOcr(2, imagePath, ocrResult);
case business_license:
if (exist != null)
return JsonTools.json2Object(exist.getResult(), BusinessLicenseOcrResponse.class);
return doBusinessLicenseOcr(imagePath, ocrResult);
default:
throw new ManagerException("OCR类型异常,请手动输入");
}
}

private IdCardOcrResponse doIdCardOcr(int type, String imagePath, OcrResult ocrResult) throws ManagerException {
try {
IdCardOcrRequest req = new IdCardOcrRequest();
req.setImageType(type+"");
req.setUrl(imagePath);
IdCardOcrResponse res = jsonPostRepeat(req);
ocrResult.setResult(res.getBizContent());
ocrResult.setCode(res.getRespCode());
ocrResultRepo.save(ocrResult);
res.setOcrResultId(ocrResult.getId());
return res;
} catch (ServiceHandleException e) {
throw new ManagerException("身份证OCR识别异常,请手动输入信息");
}
}

private VehicleLicenseOcrResponse doVehicleLicenseOcr(int type, String imagePath, OcrResult ocrResult) throws ManagerException {
VehicleLicenseOcrRequest req = new VehicleLicenseOcrRequest();
req.setImageType(type+"");
req.setUrl(imagePath);
try {
VehicleLicenseOcrResponse res = jsonPostRepeat(req);
ocrResult.setResult(res.getBizContent());
ocrResult.setCode(res.getRespCode());
ocrResultRepo.save(ocrResult);
res.setOcrResultId(ocrResult.getId());
return res;
} catch (ServiceHandleException e) {
throw new ManagerException("行驶证OCR识别异常,请手动输入信息");
}
}

private BusinessLicenseOcrResponse doBusinessLicenseOcr(String imagePath, OcrResult ocrResult) throws ManagerException {
BusinessLicenseOcrRequest req = new BusinessLicenseOcrRequest();
req.setUrl(imagePath);
try {
BusinessLicenseOcrResponse res = jsonPostRepeat(req);
ocrResult.setResult(res.getBizContent());
ocrResult.setCode(res.getRespCode());
ocrResultRepo.save(ocrResult);
res.setOcrResultId(ocrResult.getId());
return res;
} catch (ServiceHandleException e) {
throw new ManagerException("营业执照OCR识别异常,请手动输入信息");
}
}

}

+ 11
- 0
zhywpt-app-iaw/src/main/java/cn/com/taiji/iaw/manager/comm/OcrManager.java Переглянути файл

@@ -0,0 +1,11 @@
package cn.com.taiji.iaw.manager.comm;

import cn.com.taiji.common.manager.ManagerException;
import cn.com.taiji.core.model.comm.protocol.ats.AbstractAtsResponse;
import cn.com.taiji.iaw.dto.comm.OcrRequestDTO;

public interface OcrManager {

public AbstractAtsResponse ocr(OcrRequestDTO req) throws ManagerException;

}

+ 23
- 0
zhywpt-app-iaw/src/main/java/cn/com/taiji/iaw/manager/comm/OcrManagerImpl.java Переглянути файл

@@ -0,0 +1,23 @@
package cn.com.taiji.iaw.manager.comm;

import cn.com.taiji.common.manager.ManagerException;
import cn.com.taiji.core.model.comm.protocol.ats.AbstractAtsResponse;
import cn.com.taiji.iaw.dto.comm.OcrRequestDTO;
import cn.com.taiji.iaw.manager.AbstractIawManager;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class OcrManagerImpl extends AbstractIawManager implements OcrManager {

@Override
public AbstractAtsResponse ocr(OcrRequestDTO req) throws ManagerException {
try {
return getOcrResult(req.getType(), req.getImagePath());
} catch (IOException e) {
throw new ManagerException(e.getMessage());
}
}

}

Завантаження…
Відмінити
Зберегти