识别接口修改

master
hansha 1 year ago
parent 0b348e38b0
commit 59205cf6d6

@ -15,7 +15,6 @@ import com.da.framework.config.ServerConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@ -29,6 +28,7 @@ import java.util.Date;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Controller
@ -57,10 +57,13 @@ public class DangAnCollectController extends BaseController {
private IDaYtzmService daYtzmService;
@Autowired
private IDaZfnyhkcgService daZfnyhkcgService;
@Autowired
private IDaTaskService daTaskService;
private static final String FILE_DELIMETER = ",";
@Value("${ocr.url}")
private String url;
@Value("${ocr.url1}")
private String url1;
@Value("${ocr.url4}")
private String url4;
/**
* /
@ -120,6 +123,117 @@ public class DangAnCollectController extends BaseController {
*/
@PostMapping("/recongnize")
public AjaxResult recognize(@RequestBody List<DaPicturesRecard> picRecards) {
String code;
String msg="";
try {
//取所有id的集合 stream()有顺序
if(picRecards!=null&&picRecards.size()>0){
List<Long> ids = picRecards.stream().map(DaPicturesRecard::getId).collect(Collectors.toList());
String ywType = picRecards.get(0).getYwType();
Long muId = picRecards.get(0).getMuId();
String muPath = picRecards.get(0).getMuPath();
//获取所有的文件
List<File> files = new ArrayList<>();
for (DaPicturesRecard picRecard : picRecards) {
String path = getPath(picRecard.getPicUrl());
File file = new File(path);
files.add(file);
}
//创建任务
DaTask daTask = new DaTask();
daTask.setCreateBy(getUsername());
daTask.setMuId(muId);
daTask.setMuPath(muPath);
daTask.setYwType(ywType);
daTask.setNum(picRecards.size());
daTask.setStatus("0");//默认失败 接收成功后修改为1
daTask.setPicIds(StringUtils.join(ids, ",") );//逗号分割);
daTaskService.insertDaTask(daTask);
//调用第三方接口
String result = CallThirdInterface.callThirdInterfaceUpload(url1, files, ywType, ids,daTask.getId());
//! 解析JSON字符串,获取键对应的值
JSONObject jsonobject = new JSONObject(result);
code = jsonobject.getStr("code");
msg = jsonobject.getStr("msg");
//如果code=0 任务接收失败code=1成功
if (code.equals("0")) {
return AjaxResult.error(msg).put("taskId",daTask.getId());
}else{
daTask.setStatus("1");
daTask.setUpdateTime(new Date());//同时设置updateTime 因定时任务判断任务超时使用updatetime
daTaskService.updateDaTask(daTask);
}
}else{
return AjaxResult.error("识别图片不能为0");
}
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.error(e.getMessage());
}
return AjaxResult.success("任务接收成功");
}
/**
*
*/
@PostMapping("/reRecongnize")
public AjaxResult reRecongnize(Long taskId) {
DaTask daTask = daTaskService.selectDaTaskById(taskId);
List<Long> ids;
if(daTask.getSuccessNum()!=null&&daTask.getSuccessNum()>0){
String failIds = daTask.getFailIds();
ids = Stream.of(failIds.split(","))
.map(Long::parseLong)
.collect(Collectors.toList());
}else {
String picIds = daTask.getPicIds();
ids = Stream.of(picIds.split(","))
.map(Long::parseLong)
.collect(Collectors.toList());
}
//获取所有需要重新识别上传的图片
List<DaPicturesRecard> picRecards = daPicturesRecardService.selectDaPicturesRecardByIds(ids.toArray(new Long[ids.size()]));
String code;
String msg;
try {
//获取所有的文件
List<File> files = new ArrayList<>();
for (DaPicturesRecard picRecard : picRecards) {
String path = getPath(picRecard.getPicUrl());
File file = new File(path);
files.add(file);
}
//调用第三方接口 先清数据
CallThirdInterface.callThirdInterfaceClearByTaskId(url4,taskId);
String result = CallThirdInterface.callThirdInterfaceUpload(url1, files, daTask.getYwType(), ids,taskId);
//! 解析JSON字符串,获取键对应的值
JSONObject jsonobject = new JSONObject(result);
code = jsonobject.getStr("code");
msg = jsonobject.getStr("msg");
//记录最后一次识别的操作时间、操作人
daTask.setUpdateTime(new Date());
daTask.setUpdateBy(getUsername());
if (code.equals("0")) { //code=0 任务接收失败code=1成功
daTask.setStatus("0");
daTaskService.updateDaTask(daTask);
return AjaxResult.error(msg);
}else{
daTask.setStatus("1");
daTask.setLastNum(picRecards.size());
daTaskService.updateDaTask(daTask);
}
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.error(e.getMessage());
}
return AjaxResult.success("任务接收成功");
}
/**
* ocr
*/
@PostMapping("/recongnize-old")
public AjaxResult recognizeOld(@RequestBody List<DaPicturesRecard> picRecards) {
String code;
String msg;
int success;
@ -157,7 +271,7 @@ public class DangAnCollectController extends BaseController {
logger.info("CompletableFuture耗时--——————————————————》"+ (end-start)+"ms");*/
//不使用循环调用接口 直接传递数组 在callThirdInterface方法内直接添加了username参数
String result = CallThirdInterface.callThirdInterface(url, files, ywType, ids);
String result = CallThirdInterface.callThirdInterfaceUpload(url1, files, ywType, ids,1l);
long end = System.currentTimeMillis();
logger.info("CompletableFuture耗时--——————————————————》" + (end - start) + "ms");
//! 解析JSON字符串,获取键对应的值
@ -201,11 +315,11 @@ public class DangAnCollectController extends BaseController {
List<CzrkDatas> czrkDatas = JSONArray.parseArray(datas, CzrkDatas.class);
for (CzrkDatas czrkData : czrkDatas) {
List<Long> picids = czrkData.getPicid();
String picIds = StringUtils.join(picids, ",");//号分割
String picIds = StringUtils.join(picids, ",");//号分割
List<String> picUrls = picRecards.stream()
.filter(recard -> picids.contains(recard.getId()))
.map(DaPicturesRecard::getPicUrl).collect(Collectors.toList());
String pictures = StringUtils.join(picUrls, ",");//号分割
String pictures = StringUtils.join(picUrls, ",");//号分割
List<DaCzrkdj> info = czrkData.getInfo();
if (info != null && info.size() > 0) {
@ -219,12 +333,12 @@ public class DangAnCollectController extends BaseController {
daCzrkdj.setPicIds(picIds);
daCzrkdj.setPictures(pictures);
//其他字段存储
daCzrkdj.setCreateTime(new Date());
daCzrkdj.setYwType(ywType);
daCzrkdj.setMuId(muId);
daCzrkdj.setMuPath(muPath);
daCzrkdj.setAuditStatus("0");
daCzrkdj.setErrorCorrect("0");
//daCzrkdj.setCreateBy();
daCzrkdjService.insertDaCzrkdj(daCzrkdj);
}
}
@ -236,11 +350,11 @@ public class DangAnCollectController extends BaseController {
List<CszmDatas> cszmDatas = JSONArray.parseArray(datas, CszmDatas.class);
for (CszmDatas cszmData : cszmDatas) {
List<Long> picids = cszmData.getPicid();
String picIds = StringUtils.join(picids, ",");//号分割
String picIds = StringUtils.join(picids, ",");//号分割
List<String> picUrls = picRecards.stream()
.filter(recard -> picids.contains(recard.getId()))
.map(DaPicturesRecard::getPicUrl).collect(Collectors.toList());
String pictures = StringUtils.join(picUrls, ",");//号分割
String pictures = StringUtils.join(picUrls, ",");//号分割
List<DaBirthDj> info = cszmData.getInfo();
if (info != null && info.size() > 0) {
@ -254,7 +368,6 @@ public class DangAnCollectController extends BaseController {
daBirthdj.setPicIds(picIds);
daBirthdj.setPictures(pictures);
//其他字段存储
daBirthdj.setCreateTime(new Date());
daBirthdj.setYwType(ywType);
daBirthdj.setMuId(muId);
daBirthdj.setMuPath(muPath);
@ -270,11 +383,11 @@ public class DangAnCollectController extends BaseController {
List<QyzDatas> qyzDatas = JSONArray.parseArray(datas, QyzDatas.class);
for (QyzDatas qyzData : qyzDatas) {
List<Long> picids = qyzData.getPicid();
String picIds = StringUtils.join(picids, ",");//号分割
String picIds = StringUtils.join(picids, ",");//号分割
List<String> picUrls = picRecards.stream()
.filter(recard -> picids.contains(recard.getId()))
.map(DaPicturesRecard::getPicUrl).collect(Collectors.toList());
String pictures = StringUtils.join(picUrls, ",");//号分割
String pictures = StringUtils.join(picUrls, ",");//号分割
List<DaQyz> info = qyzData.getInfo();
if (info != null && info.size() > 0) {
@ -288,7 +401,6 @@ public class DangAnCollectController extends BaseController {
qyz.setPicIds(picIds);
qyz.setPictures(pictures);
//其他字段存储
qyz.setCreateTime(new Date());
qyz.setYwType(ywType);
qyz.setMuId(muId);
qyz.setMuPath(muPath);
@ -304,11 +416,11 @@ public class DangAnCollectController extends BaseController {
List<YtzmDatas> ytzmDatas = JSONArray.parseArray(datas, YtzmDatas.class);
for (YtzmDatas ytzmData : ytzmDatas) {
List<Long> picids = ytzmData.getPicid();
String picIds = StringUtils.join(picids, ",");//号分割
String picIds = StringUtils.join(picids, ",");//号分割
List<String> picUrls = picRecards.stream()
.filter(recard -> picids.contains(recard.getId()))
.map(DaPicturesRecard::getPicUrl).collect(Collectors.toList());
String pictures = StringUtils.join(picUrls, ",");//号分割
String pictures = StringUtils.join(picUrls, ",");//号分割
List<DaYtzm> info = ytzmData.getInfo();
if (info != null && info.size() > 0) {
@ -322,7 +434,6 @@ public class DangAnCollectController extends BaseController {
ytzm.setPicIds(picIds);
ytzm.setPictures(pictures);
//其他字段存储
ytzm.setCreateTime(new Date());
ytzm.setYwType(ywType);
ytzm.setMuId(muId);
ytzm.setMuPath(muPath);
@ -338,11 +449,11 @@ public class DangAnCollectController extends BaseController {
List<ZfnyDatas> zfnyDatas = JSONArray.parseArray(datas, ZfnyDatas.class);
for (ZfnyDatas zfnyData : zfnyDatas) {
List<Long> picids = zfnyData.getPicid();
String picIds = StringUtils.join(picids, ",");//号分割
String picIds = StringUtils.join(picids, ",");//号分割
List<String> picUrls = picRecards.stream()
.filter(recard -> picids.contains(recard.getId()))
.map(DaPicturesRecard::getPicUrl).collect(Collectors.toList());
String pictures = StringUtils.join(picUrls, ",");//号分割
String pictures = StringUtils.join(picUrls, ",");//号分割
List<DaZfnyhkcg> info = zfnyData.getInfo();
if (info != null && info.size() > 0) {
@ -356,7 +467,6 @@ public class DangAnCollectController extends BaseController {
zfnyhkcg.setPicIds(picIds);
zfnyhkcg.setPictures(pictures);
//其他字段存储
zfnyhkcg.setCreateTime(new Date());
zfnyhkcg.setYwType(ywType);
zfnyhkcg.setMuId(muId);
zfnyhkcg.setMuPath(muPath);
@ -372,11 +482,11 @@ public class DangAnCollectController extends BaseController {
List<ZqzDatas> zqzDatas = JSONArray.parseArray(datas, ZqzDatas.class);
for (ZqzDatas zqzData : zqzDatas) {
List<Long> picids = zqzData.getPicid();
String picIds = StringUtils.join(picids, ",");//号分割
String picIds = StringUtils.join(picids, ",");//号分割
List<String> picUrls = picRecards.stream()
.filter(recard -> picids.contains(recard.getId()))
.map(DaPicturesRecard::getPicUrl).collect(Collectors.toList());
String pictures = StringUtils.join(picUrls, ",");//号分割
String pictures = StringUtils.join(picUrls, ",");//号分割
List<DaZqz> info = zqzData.getInfo();
if (info != null && info.size() > 0) {
@ -390,7 +500,6 @@ public class DangAnCollectController extends BaseController {
zqz.setPicIds(picIds);
zqz.setPictures(pictures);
//其他字段存储
zqz.setCreateTime(new Date());
zqz.setYwType(ywType);
zqz.setMuId(muId);
zqz.setMuPath(muPath);
@ -403,14 +512,14 @@ public class DangAnCollectController extends BaseController {
break;
}
//修改图片信息记录表为已识别 .如果有失败数据,剔除失败数据
/*//修改图片信息记录表为已识别 .如果有失败数据,剔除失败数据---现在新增合集信息处做图片已识别修改
if (failList.size() > 0) {
picRecards.removeAll(failList);
}
for (DaPicturesRecard picRecard : picRecards) {
picRecard.setRecognize("1");
daPicturesRecardService.updateDaPicturesRecard(picRecard);
}
}*/
}
} catch (Exception e) {
e.printStackTrace();
@ -421,13 +530,13 @@ public class DangAnCollectController extends BaseController {
}
/**
* ocr N
* ocr N-- 使
*/
@PostMapping("/recongnizeZu")
public AjaxResult recongnizeZu(@RequestBody PicParams params) {
List<DaPicturesRecard> checked = params.getChecked();
List<DaPicturesRecard> uncheked = params.getUnchecked();
String singleOrDouble = params.getSingeOrDouble();
//String singleOrDouble = params.getSingeOrDouble();
//拼接所有的图片
List<DaPicturesRecard> listAll = checked;
if (uncheked != null && uncheked.size() > 0) {
@ -456,7 +565,7 @@ public class DangAnCollectController extends BaseController {
long start = System.currentTimeMillis();
//参数:地址、文件、业务类型、单双面: 1单面 2双面
//String result = CallThirdInterface.callThirdInterface(url, files, ywType,singleOrDouble,ids);
String result = CallThirdInterface.callThirdInterface(url, files, ywType, ids);
String result = CallThirdInterface.callThirdInterfaceUpload(url1, files, ywType, ids,1l);
long end = System.currentTimeMillis();
logger.info("CompletableFuture耗时--——————————————————》" + (end - start) + "ms");
@ -535,30 +644,7 @@ public class DangAnCollectController extends BaseController {
}
/**
* test
*/
@PostMapping("/test3")
public String test() {
String result = "{\"code\": 0, \"datas\": [{\"Picid\": [\"21\"], \"info\": [{\"name\":\"333\"},{\"name\":\"111\"}]}, {\"Picid\": [\"22\"], \"info\":[]]}]}";
JSONObject jsonobject = new JSONObject(result);
String code = jsonobject.getStr("code");
String msg = jsonobject.getStr("msg");
Integer success = jsonobject.getInt("success");
Integer total = jsonobject.getInt("total");
//识别接口方说:多张识别,要么全识别成功,要么一张也识别不成功 ,所以---------failPicids暂时不用 失败则全失败
// String failPicid=jsonobject.getStr("msg");
String datas = jsonobject.getStr("datas");
//获取常住人口信息集合
List<CzrkDatas> czrkDatas = JSONArray.parseArray(datas, CzrkDatas.class);
System.out.println(czrkDatas.get(1).getInfo() == null);
System.out.println(czrkDatas.get(1).getInfo().size());
System.out.println(czrkDatas.get(0).getInfo().get(0).getName());
return "datas.toString()";
}
/**
*
* --使
*/
/*@PostMapping("/sdrecongnize")
public AjaxResult sdrecognize(@RequestBody DaPicturesRecard picRecard) {
@ -607,6 +693,7 @@ public class DangAnCollectController extends BaseController {
}
return AjaxResult.success();
}*/
/**
* 线
*/
@ -631,21 +718,21 @@ public class DangAnCollectController extends BaseController {
/**
* ____
* ____
*/
@GetMapping("/progress")
/*@GetMapping("/progress")
public ResponseEntity<Double> getProgress() {
//调用第三方进度条接口
// 返回进度值例如0.5代表50%
return ResponseEntity.ok(0.5);
}
}*/
/**
*
*/
@PostMapping("/test")
/*@PostMapping("/test")
public String test(@RequestParam("files[]") MultipartFile[] files, @RequestParam("fileType") String fileType) throws Exception {
List<File> fileList = new ArrayList<>();
for (MultipartFile multipartFile : files) {
@ -654,12 +741,12 @@ public class DangAnCollectController extends BaseController {
multipartFile.transferTo(file);
fileList.add(file);
}
String result = CallThirdInterface.callThirdInterface(url, fileList, fileType, new ArrayList<>());
String result = CallThirdInterface.callThirdInterfaceUpload(url1, fileList, fileType, new ArrayList<>(),1l);
return result;
}
}*/
/**
*
* --使
*/
@PostMapping("/test_water")
public AjaxResult tests(MultipartFile[] files) throws Exception {

@ -6,48 +6,86 @@ import cn.hutool.http.*;
import java.io.File;
import java.lang.reflect.Field;
import java.net.HttpCookie;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.da.common.utils.SecurityUtils.getLoginUser;
/**
*
*/
public class CallThirdInterface {
public static String callThirdInterface1(String url){
public static String callThirdInterfaceClearByTaskId(String url3,Long taskId) throws Exception {
// 定义上传的URL
//String url = "http://yourserver.com:8080/upload";
//添加json
/*JSONObject json = new JSONObject();
json.put("username", "1332788xxxxxx");
json.put("password", "123456.");*/
//JSONObject json = new JSONObject();
//json.put("username", "1332788xxxxxx");
//json.put("password", "123456.");
//添加请求头,可多个
HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
headers.put("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjA2NjhmYTM5LTE5OWEtNDAyYy1iYjJlLTk1MjI0OWExMTlhOSJ9.S5-6MC6xKHx1bGjuE0g_QR0vGTsAG1E6rmQCqm2GSMIgNNZkYCnZckegYI8UCCxuGkTV_GOFwmcGagG0IccNEw");
//HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
//headers.put("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjA2NjhmYTM5LTE5OWEtNDAyYy1iYjJlLTk1MjI0OWExMTlhOSJ9.S5-6MC6xKHx1bGjuE0g_QR0vGTsAG1E6rmQCqm2GSMIgNNZkYCnZckegYI8UCCxuGkTV_GOFwmcGagG0IccNEw");
// 定义要上传的文件
// File file = new File("/path/to/your/file.txt");
// 定义其他要上传的表单参数
String result = null;
try {
Map<String, Object> map = new HashMap<>();//存放参数
map.put("keyword", "王");
map.put("ywTypes", 0);
// map.put("taskId", taskId);
// 使用Hutool的HttpUtil上传文件
result = HttpUtil.createPost(url3)
// .addHeaders(headers)
.charset(CharsetUtil.CHARSET_UTF_8)
//.form("file", file) // 文件名称为"file"
//.form(map)// 添加其他表单参数
.form("taskId", taskId)
.execute().body();
} catch (HttpException e) {
e.printStackTrace();
throw new Exception("查询结果接口连接失败!!!");
}
return result;
}
public static String callThirdInterfaceGetByTaskId(String url,Long taskId) throws Exception {
// 定义上传的URL
//String url = "http://yourserver.com:8080/upload";
//添加json
//JSONObject json = new JSONObject();
//json.put("username", "1332788xxxxxx");
//json.put("password", "123456.");
//添加请求头,可多个
//HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
//headers.put("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjA2NjhmYTM5LTE5OWEtNDAyYy1iYjJlLTk1MjI0OWExMTlhOSJ9.S5-6MC6xKHx1bGjuE0g_QR0vGTsAG1E6rmQCqm2GSMIgNNZkYCnZckegYI8UCCxuGkTV_GOFwmcGagG0IccNEw");
// 定义要上传的文件
// File file = new File("/path/to/your/file.txt");
String result = HttpUtil.createGet(url)
.addHeaders(headers)
// 定义其他要上传的表单参数
String result = null;
try {
Map<String, Object> map = new HashMap<>();//存放参数
// map.put("taskId", taskId);
// 使用Hutool的HttpUtil上传文件
result = HttpUtil.createPost(url)
// .addHeaders(headers)
.charset(CharsetUtil.CHARSET_UTF_8)
//.form("file", file) // 文件名称为"file"
.form(map)// 添加其他表单参数
//.form(map)// 添加其他表单参数
.form("taskId", taskId)
.execute().body();
// 输出结果
System.out.println(result);
SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(simpleDateFormat.format(new Date())+""+result);
} catch (HttpException e) {
e.printStackTrace();
throw new Exception("查询结果接口连接失败!!!");
}
return result;
}
public static String callThirdInterface(String url,List<File> files,String fileType,List<Long> picIds) throws Exception {
public static String callThirdInterfaceUpload(String url,List<File> files,String fileType,List<Long> picIds,Long taskId) throws Exception {
String body = null; // 获取响应主体
try {
@ -61,10 +99,11 @@ public class CallThirdInterface {
// File file = new File("C:\\Users\\hssym\\Downloads\\UBQ.png");
httpRequest.charset(CharsetUtil.CHARSET_UTF_8);
httpRequest.form("files[]",files.toArray(new File[files.size()]));
httpRequest.form("file",files.toArray(new File[files.size()]));
httpRequest.form("picIds",picIds);
httpRequest.form("fileType",fileType);
httpRequest.form("username", getLoginUser().getUsername()); //添加一个参数做标识 username
// httpRequest.form("username", getLoginUser().getUsername()); //添加一个参数做标识 username
httpRequest.form("taskId", taskId); //添加一个参数做标识 taskId
//httpRequest.form("singeOrDouble",);
//httpRequest.form("picIds",ids.toArray(new Long[ids.size()]));
@ -89,7 +128,7 @@ public class CallThirdInterface {
System.out.println(body);
} catch (HttpException e) {
e.printStackTrace();
throw new Exception("识别接口连接失败!!!");
throw new Exception("识别上传接口连接失败!!!");
}
return body;
}

Loading…
Cancel
Save