ソースを参照

ias的ocr

master
yangpeilai 1ヶ月前
コミット
ceac3e35ea

+ 103
- 0
zhywpt-service-ias/src/main/java/cn/com/taiji/ias/manager/ocr/VehicleLicenseOcrManager.java ファイルの表示

@@ -0,0 +1,103 @@
package cn.com.taiji.ias.manager.ocr;

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.entity.log.IasInterfaceLog;
import cn.com.taiji.core.manager.comm.LogManager;
import cn.com.taiji.core.manager.tools.minio.MinioUtil;
import cn.com.taiji.core.model.comm.protocol.ats.ocr.VehicleLicenseOcrRequest;
import cn.com.taiji.core.model.comm.protocol.ats.ocr.VehicleLicenseOcrResponse;
import cn.com.taiji.core.model.comm.protocol.ias.ocr.IasVehicleLicenseOcrRequest;
import cn.com.taiji.core.model.comm.protocol.ias.ocr.IasVehicleLicenseOcrResponse;
import cn.com.taiji.core.repo.jpa.comm.OcrResultRepo;
import cn.com.taiji.core.repo.jpa.log.IasInterfaceLogRepo;
import cn.com.taiji.ias.manager.AbstractCommManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class VehicleLicenseOcrManager extends AbstractCommManager implements LogManager<IasVehicleLicenseOcrRequest, IasVehicleLicenseOcrResponse, IasInterfaceLog> {

@Autowired
private OcrResultRepo ocrResultRepo;

@Autowired
private IasInterfaceLogRepo logRepo;

/**
* type:1-身份证-人像面 2-身份证-国徽面 3-行驶证前页 4-行驶证背页 5-营业执照
* imagePath:图片地址
*/

public IasVehicleLicenseOcrResponse serviceHandle(IasVehicleLicenseOcrRequest request){
logger.info("开始处理身份证识别{}", request.toString());
long begin = System.currentTimeMillis();
IasVehicleLicenseOcrResponse response = new IasVehicleLicenseOcrResponse();

//判断图片类型
String imageType = request.getImageType();
OcrType ocrType = "1".equals(imageType) ? OcrType.vehicle_license_front : OcrType.vehicle_license_back;

//识别结果
OcrResult ocrResult = new OcrResult();
ocrResult.setCreateTime(LocalDateTime.now());
ocrResult.setUrl(request.getUrl());
ocrResult.setOcrType(ocrType);

try {
//查询图片是否存在已经识别
OcrResult existOcrResult = ocrResultRepo.findByUrlAndType(request.getUrl(), ocrType);
//存在则直接返回已识别的结果
if (existOcrResult != null) {
response = JsonTools.json2Object(existOcrResult.getResult(), IasVehicleLicenseOcrResponse.class);
return onSuccess(request, response, IasInterfaceLog.class, System.currentTimeMillis() - begin);
}
//不存在则调用业务处理
doBusiness(request, response, ocrResult, ocrType, begin);
ocrResultRepo.save(ocrResult);
return onSuccess(request, response, IasInterfaceLog.class, System.currentTimeMillis() - begin);
} catch (Exception e) {
ocrResult.setCode("9999");
ocrResult.setMessage(StringUtils.abbreviate(e.getMessage(), 500));
response.setOcrResultId(ocrResult.getId());
response.setRespCode(ocrResult.getCode());
response.setRespMessage(ocrResult.getMessage());
ocrResultRepo.save(ocrResult);
logger.error(e.getMessage(), e);
return onFailed(request, response, IasInterfaceLog.class, System.currentTimeMillis() - begin, e.getMessage());
}
}

@Transactional(rollbackFor = Exception.class)
private void doBusiness(IasVehicleLicenseOcrRequest request, IasVehicleLicenseOcrResponse response, OcrResult ocrResult,OcrType ocrType, long begin) throws ServiceHandleException, IOException {

//创建业务请求
VehicleLicenseOcrRequest vehicleLicenseOcrRequest = new VehicleLicenseOcrRequest();
BeanUtils.copyProperties(request, vehicleLicenseOcrRequest);
VehicleLicenseOcrResponse res = jsonPostRepeat(vehicleLicenseOcrRequest);

//保存结果
ocrResult.setResult(res.getBizContent());
ocrResult.setCode(res.getRespCode());
ocrResult.setMessage(res.getRespMessage());

//返回结果
BeanUtils.copyProperties(res, response);
response.setOcrResultId(ocrResult.getId());
response.setImageUrl(new MinioUtil().getOuterUrl(res.getImageUrl()));
}

@Override
public void doSaveLog(IasVehicleLicenseOcrRequest request, IasVehicleLicenseOcrResponse response, IasInterfaceLog log) {
logRepo.persist(log);
logger.info("处理身份证识别结束{}", response.toString());
}
}

読み込み中…
キャンセル
保存