From 966702c4e96edab43b1984a7d65d1ebcd19d87c7 Mon Sep 17 00:00:00 2001 From: wangxun Date: Mon, 14 Apr 2025 18:20:03 +0800 Subject: [PATCH] =?UTF-8?q?ivs1800=E7=BA=A2=E5=A4=96=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=A0=87=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simulator/domain/Infrared/Coordinate.java | 4 + .../domain/Infrared/InfraredInfo.java | 5 +- .../service/impl/HikVisionServiceImpl.java | 295 +++++++++++++++--- 3 files changed, 268 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/inspect/simulator/domain/Infrared/Coordinate.java b/src/main/java/com/inspect/simulator/domain/Infrared/Coordinate.java index 1f77136..4bb50f1 100644 --- a/src/main/java/com/inspect/simulator/domain/Infrared/Coordinate.java +++ b/src/main/java/com/inspect/simulator/domain/Infrared/Coordinate.java @@ -24,4 +24,8 @@ public class Coordinate { private Integer imgHeight; private Coordinate[] coordinates; + + //红外图片类型 1-NVR;2-IVS;3-无人机 + private Integer imgType; + } diff --git a/src/main/java/com/inspect/simulator/domain/Infrared/InfraredInfo.java b/src/main/java/com/inspect/simulator/domain/Infrared/InfraredInfo.java index eb14f8b..88cc901 100644 --- a/src/main/java/com/inspect/simulator/domain/Infrared/InfraredInfo.java +++ b/src/main/java/com/inspect/simulator/domain/Infrared/InfraredInfo.java @@ -48,7 +48,7 @@ public class InfraredInfo { @JsonIgnore //框选区域-平均值 private float frameAverage; - @JsonIgnore +// @JsonIgnore //框选区域-最大值 private float frameMax; @JsonIgnore @@ -58,5 +58,8 @@ public class InfraredInfo { // 指定点的温度 private float pointTemperature; + //标注后图片路径 + private String outPath; + } diff --git a/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java b/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java index beb027d..00bb9b9 100644 --- a/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java +++ b/src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java @@ -3,6 +3,7 @@ package com.inspect.simulator.service.impl; import cn.hutool.core.util.ObjectUtil; import cn.hutool.http.ContentType; +//import com.inspect.simulator.dj.service.DjService; import com.inspect.simulator.domain.Infrared.Camera; import com.inspect.simulator.domain.Infrared.Coordinate; import com.inspect.simulator.domain.Infrared.InfraredInfo; @@ -15,10 +16,9 @@ import com.inspect.simulator.service.HikVisionService; import com.sun.jna.ptr.IntByReference; +import org.aspectj.weaver.ast.Var; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.http.HttpEntity; import org.springframework.stereotype.Service; import javax.imageio.ImageIO; @@ -48,6 +48,8 @@ public class HikVisionServiceImpl implements HikVisionService { // @Autowired // private HCNetSDK hcNetSDK; +// @Autowired +// private DjService djService; @Value("${file.picPath:test}") private String picPath; @@ -262,12 +264,22 @@ public class HikVisionServiceImpl implements HikVisionService { @Override public AjaxResult analyzeInfrared(Coordinate coordinate) { - + InfraredInfo infraredInfo = new InfraredInfo(); try { String imagePath = coordinate.getFilePath(); byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath)); - - InfraredInfo infraredInfo = readDataHw(imageBytes, coordinate); + if (coordinate.getImgType() == 1) { + infraredInfo = readDataHw(imageBytes, coordinate); + } else if (coordinate.getImgType() == 2) { + + String s = ImageOverlaysIvs(coordinate,infraredInfo); + infraredInfo.setOutPath(s); + } else if (coordinate.getImgType() == 3) { +// float[][] imageTem = djService.getImageTem(coordinate.getFilePath()); +// infraredInfo.setTemperatureMatrix(imageTem); +// String s = ImageOverlaysUav(coordinate, infraredInfo); +// infraredInfo.setOutPath(s); + } return AjaxResult.success(infraredInfo); } catch (Exception e) { @@ -394,7 +406,8 @@ public class HikVisionServiceImpl implements HikVisionService { // } //绘制框选标识 if (coordinate != null) { - ImageOverlays(coordinate, infraredInfo); + String s = ImageOverlaysNvr(coordinate, infraredInfo); + infraredInfo.setOutPath(s); } } else { System.err.println("错误:剩余数据不足以解析环境参数"); @@ -410,15 +423,16 @@ public class HikVisionServiceImpl implements HikVisionService { InfraredInfo infraredInfo = new InfraredInfo(); // 获取指定坐标的温度 if (ObjectUtil.isNotEmpty(coordinate.getFirstX())) { - - //比例缩放像素 获取底图温度值 - double xMultiple = (coordinate.getImgWidth().doubleValue() / width) * 100 / 100.0; - double yMultiple = (coordinate.getImgHeight().doubleValue() / height) * 100 / 100.0; - - int x = (int) (coordinate.getFirstX() / xMultiple); - int y = (int) (coordinate.getFirstY() / yMultiple); - coordinate.setFirstX(x); - coordinate.setFirstY(y); + if (coordinate.getImgType()!=3) {//无人机不需要对比底图 + //比例缩放像素 获取底图温度值 + double xMultiple = (coordinate.getImgWidth().doubleValue() / width) * 100 / 100.0; + double yMultiple = (coordinate.getImgHeight().doubleValue() / height) * 100 / 100.0; + + int x = (int) (coordinate.getFirstX() / xMultiple); + int y = (int) (coordinate.getFirstY() / yMultiple); + coordinate.setFirstX(x); + coordinate.setFirstY(y); + } if (coordinate.getFirstX() >= 0 && coordinate.getFirstX() <= width && coordinate.getFirstY() >= 0 && coordinate.getFirstY() <= height) { float pointTemperature = temperatureMatrix[coordinate.getFirstY()][coordinate.getFirstX()]; // 注意:y是行,x是列 @@ -436,23 +450,27 @@ public class HikVisionServiceImpl implements HikVisionService { //存储框选矩阵温度值 List values = new ArrayList<>(); if (ObjectUtil.isNotEmpty(coordinate.getFirstX()) && ObjectUtil.isNotEmpty(coordinate.getSecondX())) { - //倍数计算 - double xMultiple = (coordinate.getImgWidth().doubleValue() / width) * 100 / 100.0; - double yMultiple = (coordinate.getImgHeight().doubleValue() / height) * 100 / 100.0; - - //比例缩放像素 获取底图温度值 - int x1 = (int) (coordinate.getFirstX() / xMultiple); - int y1 = (int) (coordinate.getFirstY() / yMultiple); - int x2 = (int) (coordinate.getSecondX() / xMultiple); - int y2 = (int) (coordinate.getSecondY() / yMultiple); - coordinate.setFirstX(x1); - coordinate.setFirstY(y1); - coordinate.setSecondX(x2); - coordinate.setSecondY(y2); + if (coordinate.getImgType()!=3) {//无人机不需要对比底图 + //倍数计算 + double xMultiple = (coordinate.getImgWidth().doubleValue() / width) * 100 / 100.0; + double yMultiple = (coordinate.getImgHeight().doubleValue() / height) * 100 / 100.0; + + //比例缩放像素 获取底图温度值 + int x1 = (int) (coordinate.getFirstX() / xMultiple); + int y1 = (int) (coordinate.getFirstY() / yMultiple); + int x2 = (int) (coordinate.getSecondX() / xMultiple); + int y2 = (int) (coordinate.getSecondY() / yMultiple); + coordinate.setFirstX(x1); + coordinate.setFirstY(y1); + coordinate.setSecondX(x2); + coordinate.setSecondY(y2); + } if (coordinate.getSecondX() != 0 && coordinate.getSecondY() != 0) { for (int j = coordinate.getFirstY(); j <= coordinate.getSecondY(); j++) { // 列 + if (j < 0 || j >= temperatureMatrix.length) continue; for (int i = coordinate.getFirstX(); i <= coordinate.getSecondX(); i++) { // 行(固定列,遍历行) + if (i < 0 || i >= temperatureMatrix[j].length) continue; values.add(temperatureMatrix[j][i]); } } @@ -529,8 +547,8 @@ public class HikVisionServiceImpl implements HikVisionService { } - //图片标注 - public String ImageOverlays(Coordinate coordinate, InfraredInfo infraredInfo) { + //图片标注__nvr + public String ImageOverlaysNvr(Coordinate coordinate, InfraredInfo infraredInfo) { Coordinate[] coordinates = coordinate.getCoordinates(); @@ -539,8 +557,12 @@ public class HikVisionServiceImpl implements HikVisionService { String markPicName = sf.format(date); // 图片路径(请替换为实际路径) String imagePath = coordinate.getFilePath(); + // 获取文件名(不含扩展名) + File file = new File(imagePath); + String fileName = file.getName(); + String pureName = fileName.substring(0, fileName.lastIndexOf('.')); // String imagePath = picPath+"/pic/hw/20250409141218PLAY_CELLPHONE_.jpg"; - String outputPath = picPath + "/pic/hw/mark/" + markPicName + ".jpg"; + String outputPath = picPath + "/pic/hw/mark/" +pureName+"_"+ markPicName + ".jpg"; try { // 加载原始图片 BufferedImage originalImage = ImageIO.read(new File(imagePath)); @@ -555,7 +577,6 @@ public class HikVisionServiceImpl implements HikVisionService { originalImage.getHeight(), BufferedImage.TYPE_INT_RGB ); - // 绘制原始图片 Graphics2D g2d = annotatedImage.createGraphics(); g2d.drawImage(originalImage, 0, 0, null); @@ -566,6 +587,7 @@ public class HikVisionServiceImpl implements HikVisionService { for (Coordinate c : coordinates) { + //点标注 if (c.getFirstX() != null && c.getFirstY() != null && c.getSecondX() == null && c.getSecondY() == null) { g2d.setFont(font); // 标注点坐标 @@ -586,6 +608,7 @@ public class HikVisionServiceImpl implements HikVisionService { } } + //矩阵标注 if (c.getSecondX() != null && c.getSecondY() != null) { // 标注矩形(每两个点确定一个矩形) g2d.setColor(Color.RED); @@ -610,12 +633,14 @@ public class HikVisionServiceImpl implements HikVisionService { coordinate.setFirstY(y1); coordinate.setSecondX(x2); coordinate.setSecondY(y2); + InfraredInfo drawStringMatrix = matrixTemperatureShow(coordinate, infraredInfo.getMatrixWidth(), infraredInfo.getMatrixHeight(), infraredInfo.getTemperatureMatrix()); - drawStringMatrix.getFrameAverage(); + infraredInfo.setFrameMax(Math.round(drawStringMatrix.getFrameMax() * 100) / 100f); // 添加矩形标签 g2d.drawString("平均温度:" + String.format("%.2f", drawStringMatrix.getFrameAverage()) + - "最大温度:" + String.format("%.2f", drawStringMatrix.getFrameMax()) + - "最小温度:" + String.format("%.2f", drawStringMatrix.getFrameMin()), rectX + 5, rectY + 15); + "最高温度:" + String.format("%.2f", drawStringMatrix.getFrameMax()) + + "最低温度:" + String.format("%.2f", drawStringMatrix.getFrameMin()), rectX + 5, rectY + 15); + } } } @@ -635,6 +660,206 @@ public class HikVisionServiceImpl implements HikVisionService { return outputPath; } + //图片标注_iVS1800 + public String ImageOverlaysIvs(Coordinate coordinate, InfraredInfo infraredInfo) { + + Coordinate[] coordinates = coordinate.getCoordinates(); + + Date date = new Date(); + SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); + String markPicName = sf.format(date); + // 图片路径(请替换为实际路径) + String imagePath = coordinate.getFilePath(); + // 获取文件名(不含扩展名) + File file = new File(imagePath); + String fileName = file.getName(); + String pureName = fileName.substring(0, fileName.lastIndexOf('.')); +// String imagePath = picPath+"/pic/hw/20250409141218PLAY_CELLPHONE_.jpg"; + String outputPath = picPath + "/pic/hw/mark/" +pureName+"_"+ markPicName + ".jpg"; + try { + // 加载原始图片 + BufferedImage originalImage = ImageIO.read(new File(imagePath)); + if (originalImage == null) { + System.err.println("无法加载图片: " + imagePath); + return null; + } + + // 创建可编辑的图片副本 + BufferedImage annotatedImage = new BufferedImage( + originalImage.getWidth(), + originalImage.getHeight(), + BufferedImage.TYPE_INT_RGB + ); + // 绘制原始图片 + Graphics2D g2d = annotatedImage.createGraphics(); + g2d.drawImage(originalImage, 0, 0, null); + + // 设置标注样式 + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + Font font = new Font("微软雅黑", Font.BOLD, 17); + + for (Coordinate c : coordinates) { + g2d.setFont(font); + //矩阵标注 + if (c.getSecondX() != null && c.getSecondY() != null) { + // 标注矩形(每两个点确定一个矩形) + g2d.setColor(Color.RED); + int x1 = c.getFirstX(); + int y1 = c.getFirstY(); + int x2 = c.getSecondX(); + int y2 = c.getSecondY(); + + // 确保坐标在图片范围内 + if (x1 >= 0 && x1 < originalImage.getWidth() && y1 >= 0 && y1 < originalImage.getHeight() && + x2 >= 0 && x2 < originalImage.getWidth() && y2 >= 0 && y2 < originalImage.getHeight()) { + + // 确保x1,y1是左上角,x2,y2是右下角 + int rectX = Math.min(x1, x2); + int rectY = Math.min(y1, y2); + int width = Math.abs(x2 - x1); + int height = Math.abs(y2 - y1); + // 绘制矩形 + g2d.drawRect(rectX, rectY, width, height); + + coordinate.setFirstX(x1); + coordinate.setFirstY(y1); + coordinate.setSecondX(x2); + coordinate.setSecondY(y2); + // 添加矩形标签 + //图片路径取值 + String[] dividePath = Paths.get(imagePath).getFileName().toString().replace(".jpg", "").split("_"); + String max = dividePath[dividePath.length - 3]; // 最大值 + String min = dividePath[dividePath.length - 2]; // 最小值 + String avg = dividePath[dividePath.length - 1]; // 平均值 + infraredInfo.setFrameMax(Float.parseFloat(max)); + g2d.drawString("平均温度:" +avg + "最高温度:" + max + "最低温度:" + min, rectX + 5, rectY + 15); + } + } + } + g2d.dispose(); + // 保存标注后的图片 + File outputFile = new File(outputPath); + // 确保输出目录存在 + outputFile.getParentFile().mkdirs(); + ImageIO.write(annotatedImage, "jpg", outputFile); + } catch (IOException e) { + System.err.println("处理图片时出错: " + e.getMessage()); + e.printStackTrace(); + } + return outputPath; + } + + + //图片标注__无人机 + public String ImageOverlaysUav(Coordinate coordinate, InfraredInfo infraredInfo) { + + Coordinate[] coordinates = coordinate.getCoordinates(); + + Date date = new Date(); + SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); + String markPicName = sf.format(date); + // 图片路径(请替换为实际路径) + String imagePath = coordinate.getFilePath(); + // 获取文件名(不含扩展名) + File file = new File(imagePath); + String fileName = file.getName(); + String pureName = fileName.substring(0, fileName.lastIndexOf('.')); +// String imagePath = picPath+"/pic/hw/20250409141218PLAY_CELLPHONE_.jpg"; + String outputPath = picPath + "/pic/hw/mark/" +pureName+"_"+markPicName + ".jpg"; + try { + // 加载原始图片 + BufferedImage originalImage = ImageIO.read(new File(imagePath)); + if (originalImage == null) { + System.err.println("无法加载图片: " + imagePath); + return null; + } + + // 创建可编辑的图片副本 + BufferedImage annotatedImage = new BufferedImage( + originalImage.getWidth(), + originalImage.getHeight(), + BufferedImage.TYPE_INT_RGB + ); + // 绘制原始图片 + Graphics2D g2d = annotatedImage.createGraphics(); + g2d.drawImage(originalImage, 0, 0, null); + + // 设置标注样式 + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + Font font = new Font("微软雅黑", Font.BOLD, 17); + + for (Coordinate c : coordinates) { + + //点标注 + if (c.getFirstX() != null && c.getFirstY() != null && c.getSecondX() == null && c.getSecondY() == null) { + g2d.setFont(font); + // 标注点坐标 + g2d.setColor(Color.GREEN); + int x = c.getFirstX(); + int y = c.getFirstY(); + + // 确保坐标在图片范围内 + if (x >= 0 && x < originalImage.getWidth() && + y >= 0 && y < originalImage.getHeight()) { + // 绘制点(用实心圆表示) + g2d.fillOval(x - 3, y - 3, 6, 6); + coordinate.setFirstX(x); + coordinate.setFirstY(y); + InfraredInfo drawStringPoint = PointTemperatureShow(coordinate, infraredInfo.getMatrixWidth(), infraredInfo.getMatrixHeight(), infraredInfo.getTemperatureMatrix()); + // 添加坐标标签 + g2d.drawString("(" + String.format("%.2f", drawStringPoint.getPointTemperature()) + ")", x + 8, y - 8); + } + } + + //矩阵标注 + if (c.getSecondX() != null && c.getSecondY() != null) { + // 标注矩形(每两个点确定一个矩形) + g2d.setColor(Color.GREEN); + int x1 = c.getFirstX(); + int y1 = c.getFirstY(); + int x2 = c.getSecondX(); + int y2 = c.getSecondY(); + + // 确保坐标在图片范围内 + if (x1 >= 0 && x1 < originalImage.getWidth() && y1 >= 0 && y1 < originalImage.getHeight() && + x2 >= 0 && x2 < originalImage.getWidth() && y2 >= 0 && y2 < originalImage.getHeight()) { + + // 确保x1,y1是左上角,x2,y2是右下角 + int rectX = Math.min(x1, x2); + int rectY = Math.min(y1, y2); + int width = Math.abs(x2 - x1); + int height = Math.abs(y2 - y1); + // 绘制矩形 + g2d.drawRect(rectX, rectY, width, height); + coordinate.setFirstX(x1); + coordinate.setFirstY(y1); + coordinate.setSecondX(x2); + coordinate.setSecondY(y2); + + InfraredInfo drawStringMatrix = matrixTemperatureShow(coordinate, infraredInfo.getMatrixWidth(), infraredInfo.getMatrixHeight(), infraredInfo.getTemperatureMatrix()); + infraredInfo.setFrameMax(Math.round(drawStringMatrix.getFrameMax() * 100) / 100f); + // 添加矩形标签 + g2d.drawString("平均温度:" + String.format("%.2f", drawStringMatrix.getFrameAverage()) + + "最高温度:" + String.format("%.2f", drawStringMatrix.getFrameMax()) + + "最低温度:" + String.format("%.2f", drawStringMatrix.getFrameMin()), rectX + 5, rectY + 15); + } + } + } + g2d.dispose(); + // 保存标注后的图片 + File outputFile = new File(outputPath); + // 确保输出目录存在 + outputFile.getParentFile().mkdirs(); + + ImageIO.write(annotatedImage, "jpg", outputFile); +// System.out.println("标注后的图片已保存到: " + outputPath); + + } catch (IOException e) { + System.err.println("处理图片时出错: " + e.getMessage()); + e.printStackTrace(); + } + return outputPath; + } @Override