commit
8348b79ba4
@ -0,0 +1,155 @@
|
||||
package org.jeecg.modules.ocr.init;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.jeecg.common.constant.OcrConstant;
|
||||
import org.jeecg.common.system.vo.DictModel;
|
||||
import org.jeecg.common.util.AssertUtils;
|
||||
import org.jeecg.common.util.RestUtil;
|
||||
import org.jeecg.modules.ocr.dto.OcrIdentifyDTO;
|
||||
import org.jeecg.modules.ocr.entity.OcrIdentify;
|
||||
import org.jeecg.modules.ocr.service.IOcrIdentifyDetailService;
|
||||
import org.jeecg.modules.ocr.service.IOcrIdentifyService;
|
||||
import org.jeecg.modules.ocr.service.impl.TaskService;
|
||||
import org.jeecg.modules.ocr.utils.FileOUtils;
|
||||
import org.jeecg.modules.system.entity.SysLog;
|
||||
import org.jeecg.modules.system.service.ISysDictService;
|
||||
import org.jeecg.modules.system.service.ISysLogService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Description
|
||||
* @Author ZhouWenTao
|
||||
* @Date 2023/9/20 17:53
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class HandleTaskInit implements ApplicationRunner {
|
||||
@Resource
|
||||
IOcrIdentifyService ocrIdentifyService;
|
||||
@Resource
|
||||
IOcrIdentifyDetailService ocrIdentifyDetailService;
|
||||
@Resource
|
||||
TaskService taskService;
|
||||
@Resource
|
||||
private ISysDictService sysDictService;
|
||||
@Resource
|
||||
private ISysLogService sysLogService;
|
||||
@Value("${system.project.enableHandleTask}")
|
||||
private boolean enableHandleTask;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
if(enableHandleTask){
|
||||
try {
|
||||
Thread.sleep(5000L);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(10000L);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.info("---------检测是否有任务...");
|
||||
//获取任务
|
||||
List<OcrIdentify> list= ocrIdentifyService.getSemanticTaskList();
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//获取相对路径
|
||||
List<DictModel> ocr_relative_path = sysDictService.queryDictItemsByCode("ocr_relative_path");
|
||||
String relativePath = null;
|
||||
if (ocr_relative_path != null && ocr_relative_path.size() > 0) {
|
||||
for (DictModel dictModel : ocr_relative_path) {
|
||||
if (dictModel.getValue().equals("0")) {
|
||||
relativePath = dictModel.getText();
|
||||
}
|
||||
}
|
||||
}
|
||||
//获取识别的图片
|
||||
List<String> fileList=null;
|
||||
|
||||
//临时变量
|
||||
List<String> fileUrlList;
|
||||
String image;//图片
|
||||
//执行获取到的任务
|
||||
identifyFor: for (OcrIdentify ocrIdentify : list) {
|
||||
image=null;
|
||||
fileList=new ArrayList<>();
|
||||
//识别的图片路径
|
||||
String imageUrl = ocrIdentify.getIdentifyUrl();
|
||||
//判断是不是网络图片
|
||||
Boolean onlineFile = FileOUtils.isOnlineFile(ocrIdentify.getIdentifyUrl());
|
||||
|
||||
//把过去执行过的明细给删掉
|
||||
ocrIdentifyDetailService.deleteByOcrIdentifyId(ocrIdentify.getId());
|
||||
|
||||
//更新开始时间
|
||||
ocrIdentifyService.updateMasterTaskStartTime(ocrIdentify.getId());
|
||||
|
||||
//最终要请求ocr识别的图片对象
|
||||
JSONObject requestBody = new JSONObject();
|
||||
if (onlineFile) {
|
||||
//如果是网络图片,则将图片下载
|
||||
image = FileOUtils.downLoadFromUrl(imageUrl, FileOUtils.getFileName(imageUrl), OcrConstant.FILE_DOWNLOAD_URL_PREFIX);
|
||||
fileList.add(image);
|
||||
}else{
|
||||
//路径下识别到的图片集合
|
||||
fileUrlList = FileOUtils.fileLists(relativePath, ocrIdentify.getIdentifyUrl());
|
||||
AssertUtils.notNull(fileUrlList, "图片地址不存在");
|
||||
for (String fileUrl : fileUrlList) {
|
||||
//判断附件是否是 图片格式
|
||||
if (fileUrl.lastIndexOf(".png") != -1 || fileUrl.lastIndexOf(".jpg") != -1 || fileUrl.lastIndexOf(".jpeg") != -1) {
|
||||
fileList.add(fileUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//最终要识别哪些图片
|
||||
int i=0;
|
||||
for (String img : fileList) {
|
||||
i++;
|
||||
requestBody.put("task_id", ocrIdentify.getId()+"_"+i);
|
||||
requestBody.put("img_path", img);
|
||||
log.info("----------------------请求参数");
|
||||
log.info(requestBody.toJSONString());
|
||||
try {
|
||||
log.info("----------------------请求参数");
|
||||
log.info(requestBody.toJSONString());
|
||||
JSONObject semanticResponseJson = RestUtil.post(OcrConstant.api_test2_identify_url, requestBody);
|
||||
semanticResponseJson.put("identifyId", ocrIdentify.getId());
|
||||
log.info("ocr识别返回数据:");
|
||||
log.info(semanticResponseJson.toJSONString());
|
||||
ocrIdentifyService.getSemanticInfo(semanticResponseJson);
|
||||
}catch (Exception e){
|
||||
log.error("识别图片失败:");
|
||||
SysLog sysLog=new SysLog();
|
||||
sysLog.setLogType(2);
|
||||
sysLog.setLogContent(img+"_识别图片失败:"+e.getMessage());
|
||||
sysLog.setOperateType(2);
|
||||
sysLogService.save(sysLog);
|
||||
log.error(e.getMessage());
|
||||
log.error("正在重试...");
|
||||
continue identifyFor;
|
||||
}
|
||||
|
||||
}
|
||||
//更改任务状态
|
||||
ocrIdentifyService.updateOcrIdentifyStatus(ocrIdentify.getId(), "1");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue