Browse Source

/*红外算法入口改成规范统一的picAnalyse*/

master
htjcAdmin 8 months ago
parent
commit
89fd947f31
4 changed files with 354 additions and 55 deletions
  1. +4
    -4
      src/main/java/com/inspect/simulator/controller/InfraredController.java
  2. +2
    -1
      src/main/java/com/inspect/simulator/service/HikVisionService.java
  3. +112
    -50
      src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java
  4. +236
    -0
      src/main/java/com/inspect/simulator/utils/HttpClientUtils.java

+ 4
- 4
src/main/java/com/inspect/simulator/controller/InfraredController.java View File

@ -4,6 +4,7 @@ import com.inspect.simulator.domain.Infrared.*;
import com.inspect.simulator.service.HikVisionService;
import com.inspect.simulator.hikVision.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@ -51,10 +52,9 @@ public class InfraredController {
}
//图谱解析
@PostMapping("/ir/calculatePicture")
@PostMapping("/ir/picAnalyse")
@ResponseBody
public AjaxResult calculatePicture(@RequestBody InfraPictureInfo infraPictureInfo) {
return hikVisionService.calculatePicture(infraPictureInfo);
public ResponseEntity<String> irPicAnalyse(@RequestBody final String analyseRequest) {
return hikVisionService.irPicAnalyse(analyseRequest);
}
}

+ 2
- 1
src/main/java/com/inspect/simulator/service/HikVisionService.java View File

@ -2,6 +2,7 @@ package com.inspect.simulator.service;
import com.inspect.simulator.domain.Infrared.*;
import com.inspect.simulator.hikVision.utils.AjaxResult;
import org.springframework.http.ResponseEntity;
public interface HikVisionService {
@ -18,5 +19,5 @@ public interface HikVisionService {
public float[][] UploadFtpImage( String url, String type, String image);
//图谱解析
AjaxResult calculatePicture(InfraPictureInfo infraPictureInfo);
ResponseEntity<String> irPicAnalyse(String analyseRequest);
}

+ 112
- 50
src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java View File

@ -9,7 +9,13 @@ import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.inspect.simulator.domain.Infrared.*;
import com.inspect.simulator.domain.algorithm.in.AnalyseReqItem;
import com.inspect.simulator.domain.algorithm.in.AnalyseRequest;
import com.inspect.simulator.domain.algorithm.out.AnalyseResItem;
import com.inspect.simulator.domain.algorithm.out.AnalyseResPoint;
import com.inspect.simulator.domain.algorithm.out.AnalyseResult;
import com.inspect.simulator.domain.basedata.BasedataEqpBookChannel;
import com.inspect.simulator.hikVision.utils.AjaxResult;
import com.inspect.simulator.hikVision.utils.StringUtils;
@ -17,6 +23,7 @@ import com.inspect.simulator.hikVision.utils.jna.HikVisionUtils;
import com.inspect.simulator.hikVision.utils.jna.HCNetSDK;
import com.inspect.simulator.mapper.BasedataEqpBookChannelMapper;
import com.inspect.simulator.service.HikVisionService;
import com.inspect.simulator.utils.HttpClientUtils;
import com.inspect.simulator.utils.sftp.SftpClient;
import com.sun.jna.ptr.IntByReference;
@ -31,6 +38,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -313,13 +321,13 @@ public class HikVisionServiceImpl implements HikVisionService {
int secondY = 640;
int imgWidth = 640;
int imgHeight = 512;
if(infraPictureInfo.getCoordinates() != null && !infraPictureInfo.getCoordinates().isEmpty()) {
if (infraPictureInfo.getCoordinates() != null && !infraPictureInfo.getCoordinates().isEmpty()) {
firstX = infraPictureInfo.getCoordinates().get(0).getFirstX();
firstY = infraPictureInfo.getCoordinates().get(0).getFirstY();
secondX = infraPictureInfo.getCoordinates().get(0).getSecondX();
secondY = infraPictureInfo.getCoordinates().get(0).getSecondY();
imgWidth = infraPictureInfo.getImgHeight();
imgHeight = infraPictureInfo.getImgWidth();
imgHeight = infraPictureInfo.getImgWidth();
}
String channelContent = firstX + "," + firstY + "," + secondX + "," + secondY + "," + imgWidth + "," + imgHeight;
@ -1174,58 +1182,112 @@ public class HikVisionServiceImpl implements HikVisionService {
@Override
public AjaxResult calculatePicture(InfraPictureInfo infraPictureInfo) {
public ResponseEntity<String> irPicAnalyse(final String analyseRequestJson) {
log.info("[INFRARED] irPicAnalyse: analyseRequestJson={}", analyseRequestJson);
AnalyseRequest analyseRequest;
try {
analyseRequest = new Gson().fromJson(analyseRequestJson, AnalyseRequest.class);
} catch (Exception e) {
log.error("[INFRARED] irPicAnalyse: analyseRequestJson parse exception: {}", e.getMessage());
return ResponseEntity.ok().body("{\"code\":\"201\"}");
}
log.info("[INFRARED] irPicAnalyse: analyseRequest={}", new Gson().toJson(analyseRequest));
List<AnalyseReqItem> analyseReqItemList = analyseRequest.getObjectList();
if(analyseReqItemList == null || analyseReqItemList.isEmpty()) {
log.error("[INFRARED] irPicAnalyse: analyseReqItemList empty!");
return ResponseEntity.ok().body("{\"code\":\"202\"}");
}
final AnalyseReqItem analyseReqItem = analyseReqItemList.get(0);
final String[] typeList = analyseReqItem.getTypeList();
String algType = "unKnownType";
if(typeList != null && typeList.length != 0) {
algType = typeList[0];
}
final String feedBackHostIp = analyseRequest.getRequestHostIp();
final String feedBackPort = analyseRequest.getRequestHostPort();
final String feedbackUrl = feedBackHostIp + feedBackPort + "/picAnalyseRetNotify";
log.info("[INFRARED] irPicAnalyse: feedbackUrl={}", feedbackUrl);
AnalyseResult analyseResult = new AnalyseResult();
analyseResult.setRequestId(analyseRequest.getRequestId());
AnalyseResPoint analyseResPoint = new AnalyseResPoint();
analyseResPoint.setValue("0");
analyseResPoint.setConf("0.85");
analyseResPoint.setCode("2000");
analyseResPoint.setResImageUrl(analyseRequest.getObjectList().get(0).getImageUrlList()[0]);
List<AnalyseResPoint> analyseResPoints = new ArrayList<>();
analyseResPoints.add(analyseResPoint);
AnalyseResItem analyseResItem = new AnalyseResItem();
analyseResItem.setObjectId(analyseRequest.getObjectList().get(0).getObjectId());
analyseResItem.setAlgFactory(algType);
analyseResItem.setResults(analyseResPoints);
List<AnalyseResItem> analyseResItems = new ArrayList<>();
analyseResItems.add(analyseResItem);
analyseResult.setResultList(analyseResItems);
InfraredInfo infraredInfo = new InfraredInfo();
try {
String imagePath = infraPictureInfo.getFilePath();
// String imagePath = "/images/ir_test.jpg";
InputStream inputStream = downloadFtp(imagePath);
String protocol = hrFtpUrl.substring(0, 6); // "ftp://"
String withoutProtocol = hrFtpUrl.substring(6); // "zthr02:zthr02@123.184.14.138:50021/"
String[] userAndHost = withoutProtocol.split("@");
if (userAndHost.length != 2) {
throw new IllegalArgumentException("URL 格式错误,缺少 @ 分隔符");
}
String[] userPass = userAndHost[0].split(":");
String username = userPass[0];
String password = userPass[1];
String hostPort = userAndHost[1].replace("/", ""); // 移除末尾的 "/"
String[] hostAndPort = hostPort.split(":");
String host = hostAndPort[0];
int port = Integer.parseInt(hostAndPort[1]);
picFtp("/", inputStream, host, port, username, password);
String imageName = Paths.get(imagePath).getFileName().toString();
String ftpUrlName = "[\"" + hrFtpUrl + imageName + "\"]";
if (infraPictureInfo.getImgType() == 1) {
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
infraredInfo = readDataHw(imageBytes, infraPictureInfo.getCoordinates().get(0));
} else if (infraPictureInfo.getImgType() == 2) {
// String s = ImageOverlaysIvs(infraPictureInfo.getCoordinates().get(0), infraredInfo);
// infraredInfo.setOutPath(s);
} else if (infraPictureInfo.getImgType() == 3) {
//sdk版本调用x86_32
//float[][] imageTem = djService.getImageTem(coordinate.getFilePath());
//调用华软接口
float[][] imageTem = UploadFtpImage(hrUavUrl, null, ftpUrlName);
if (imageTem != null && imageTem.length > 0 && imageTem[0].length > 0) {
infraredInfo.setTemperatureMatrix(imageTem);
String s = ImageOverlaysUav(infraPictureInfo, infraredInfo);
infraredInfo.setOutPath(s);
} else {
return AjaxResult.error("温度解析错误!");
}
}
String analyseResultOutJson = JSONObject.toJSONString(analyseResult);
log.info("[INFRARED] irPicAnalyse: feedbackUrl={}, analyseResultOutJson={}", feedbackUrl, analyseResultOutJson);
String result = HttpClientUtils.sendPostAgain(feedbackUrl, analyseResultOutJson);
log.info("[INFRARED] irPicAnalyse: feedbackUrl={}, result: {}", feedbackUrl, result);
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.error();
log.info("[INFRARED] irPicAnalyse: feedbackUrl={}, EXCEPTION: {}", feedbackUrl, e.getMessage());
}
return AjaxResult.success(infraredInfo);
return ResponseEntity.ok().body("{\"code\":\"200\"}");
// InfraredInfo infraredInfo = new InfraredInfo();
// try {
// String imagePath = infraPictureInfo.getFilePath();
//// String imagePath = "/images/ir_test.jpg";
// InputStream inputStream = downloadFtp(imagePath);
// String protocol = hrFtpUrl.substring(0, 6); // "ftp://"
// String withoutProtocol = hrFtpUrl.substring(6); // "zthr02:zthr02@123.184.14.138:50021/"
// String[] userAndHost = withoutProtocol.split("@");
// if (userAndHost.length != 2) {
// throw new IllegalArgumentException("URL 格式错误,缺少 @ 分隔符");
// }
// String[] userPass = userAndHost[0].split(":");
// String username = userPass[0];
// String password = userPass[1];
// String hostPort = userAndHost[1].replace("/", ""); // 移除末尾的 "/"
// String[] hostAndPort = hostPort.split(":");
// String host = hostAndPort[0];
// int port = Integer.parseInt(hostAndPort[1]);
// picFtp("/", inputStream, host, port, username, password);
//
// String imageName = Paths.get(imagePath).getFileName().toString();
// String ftpUrlName = "[\"" + hrFtpUrl + imageName + "\"]";
//
//
// if (infraPictureInfo.getImgType() == 1) {
// byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
// infraredInfo = readDataHw(imageBytes, infraPictureInfo.getCoordinates().get(0));
// } else if (infraPictureInfo.getImgType() == 2) {
//
//// String s = ImageOverlaysIvs(infraPictureInfo.getCoordinates().get(0), infraredInfo);
//// infraredInfo.setOutPath(s);
// } else if (infraPictureInfo.getImgType() == 3) {
// //sdk版本调用x86_32
// //float[][] imageTem = djService.getImageTem(coordinate.getFilePath());
//
// //调用华软接口
// float[][] imageTem = UploadFtpImage(hrUavUrl, null, ftpUrlName);
// if (imageTem != null && imageTem.length > 0 && imageTem[0].length > 0) {
// infraredInfo.setTemperatureMatrix(imageTem);
// String s = ImageOverlaysUav(infraPictureInfo, infraredInfo);
// infraredInfo.setOutPath(s);
// } else {
// return AjaxResult.error("温度解析错误!");
// }
// }
// } catch (Exception e) {
// e.printStackTrace();
// return AjaxResult.error();
// }
// return AjaxResult.success(infraredInfo);
}


+ 236
- 0
src/main/java/com/inspect/simulator/utils/HttpClientUtils.java View File

@ -0,0 +1,236 @@
package com.inspect.simulator.utils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class HttpClientUtils {
public static String post(String requestUrl) {
return post(requestUrl, "", "");
}
public static String post(String requestUrl, String requestPram) {
return post(requestUrl, requestPram, "");
}
public static String post(String requestUrl, String requestPram, String accessToken) {
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
StringBuffer responseResult = new StringBuffer();
HttpURLConnection httpURLConnection = null;
try {
URL realUrl = new URL(requestUrl);
httpURLConnection = (HttpURLConnection) realUrl.openConnection();
httpURLConnection.setRequestProperty("accept", "*/*");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("connection", "Keep-Alive");
if (!accessToken.equals("") || accessToken != null) {
httpURLConnection.setRequestProperty("access-token", accessToken);
}
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(requestPram.length()));
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8");
outputStreamWriter.write(requestPram);
outputStreamWriter.flush();
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
responseResult.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpURLConnection.disconnect();
try {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseResult.toString();
}
public static InputStream getStream(String url, String param) throws Exception {
param = param.replace("#", "%23");
URLConnection connection = getConnection(url, param);
return connection.getInputStream();
}
public static URLConnection getConnection(String url, String param) throws Exception {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
return connection;
}
public static HttpURLConnection getConnection2(String url, String param) throws Exception {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
return connection;
}
public static String get(String url, String param) throws Exception {
StringBuilder result = new StringBuilder();
BufferedReader in = null;
try {
param = param.replace(" ", "%20");
param = param.replace("#", "%23");
URL realUrl = new URL(url + "?" + param);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
throw e;
} finally {
if (null != in) {
in.close();
}
}
return result.toString();
}
public static String postJson(String requestUrl, String requestPram, String tokenKey, String accessToken) {
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
StringBuffer responseResult = new StringBuffer();
HttpURLConnection httpURLConnection = null;
try {
URL realUrl = new URL(requestUrl);
httpURLConnection = (HttpURLConnection) realUrl.openConnection();
httpURLConnection.setRequestProperty("accept", "*/*");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("connection", "Keep-Alive");
if (StringUtils.isNotBlank(accessToken)) {
if (StringUtils.isNotBlank(tokenKey)) {
httpURLConnection.setRequestProperty(tokenKey, accessToken);
} else {
httpURLConnection.setRequestProperty("access-token", accessToken);
}
}
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(requestPram.length()));
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8");
outputStreamWriter.write(requestPram);
outputStreamWriter.flush();
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
String line;
while ((line = bufferedReader.readLine()) != null) {
responseResult.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpURLConnection.disconnect();
try {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseResult.toString();
}
public static String postJson(String requestUrl, String requestPram, String accessToken) {
return postJson(requestUrl, requestPram, "", accessToken);
}
public static String sendPostAgain(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
URLConnection conn = null;
try {
URL realUrl = new URL(url);
conn = realUrl.openConnection();
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setConnectTimeout(30000);
conn.setReadTimeout(120000);
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
out.print(param);
out.flush();
InputStream stream = conn.getInputStream();
in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
try {
in = new BufferedReader(new InputStreamReader(((HttpURLConnection) conn).getErrorStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (IOException e2) {
e2.printStackTrace();
}
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result.toString();
}
}

Loading…
Cancel
Save