Przeglądaj źródła

视频告警图片

Zimo 7 godzin temu
rodzic
commit
2a4bf57cda

+ 37 - 2
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/alarm/IotVideoAlarmController.java

@@ -3,12 +3,11 @@ package cn.iocoder.yudao.module.pms.controller.admin.alarm;
 import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotVideoAlarmPageReqVO;
 import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotVideoAlarmRespVO;
 import cn.iocoder.yudao.module.pms.controller.admin.alarm.vo.IotVideoAlarmSaveReqVO;
-import cn.iocoder.yudao.module.pms.controller.admin.inspect.order.vo.IotInspectOrderRespVO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.alarm.IotVideoAlarmDO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.sip.device.channel.YfSipDeviceChannelDO;
 import cn.iocoder.yudao.module.pms.dal.mysql.yanfan.sip.device.channel.YfSipDeviceChannelMapper;
+import cn.iocoder.yudao.module.pms.hik.HikIsapiService;
 import cn.iocoder.yudao.module.pms.service.alarm.IotVideoAlarmService;
-import cn.iocoder.yudao.module.pms.service.yanfan.sip.device.channel.YfSipDeviceChannelService;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.validation.annotation.Validated;
@@ -48,6 +47,8 @@ public class IotVideoAlarmController {
     private IotVideoAlarmService iotVideoAlarmService;
     @Resource
     private YfSipDeviceChannelMapper yfsipDeviceChannelMapper;
+    @Resource
+    private HikIsapiService hikIsapiService;
 
     @PostMapping("/create")
     @Operation(summary = "创建资料")
@@ -115,4 +116,38 @@ public class IotVideoAlarmController {
                         BeanUtils.toBean(list, IotVideoAlarmRespVO.class));
     }
 
+
+    @GetMapping("/pic/{id}")
+    @Operation(summary = "获得视频告警图片")
+    @PreAuthorize("@ss.hasPermission('rq:iot-video-alarm:query')")
+    public CommonResult<String> getAlarmPic(@PathVariable Long id) throws IOException {
+        IotVideoAlarmDO iotVideoAlarm = iotVideoAlarmService.getIotVideoAlarm(id);
+        String url = "";
+        if (iotVideoAlarm != null) {
+            if (StringUtils.isNotBlank(iotVideoAlarm.getUrl())) {
+                url = iotVideoAlarm.getUrl();
+            } else if (StringUtils.isNotBlank(iotVideoAlarm.getImageUrl())){
+                url = iotVideoAlarm.getImageUrl();
+            }
+        }
+        byte[] picInfo = new byte[0];
+        if (StringUtils.isNotBlank(url)) {
+            picInfo = hikIsapiService.getPicInfo("nvr-001", url);
+        }
+//        // 2. 设置响应头,告诉前端这是图片流
+//        HttpHeaders headers = new HttpHeaders();
+//        // 根据实际图片格式调整(海康通常返回jpeg)
+//        headers.setContentType(MediaType.IMAGE_JPEG);
+//        // 设置内容长度,提升传输效率
+//        headers.setContentLength(picInfo.length);
+//        // 禁止缓存(可选,根据业务需求)
+//        headers.setCacheControl("no-cache, no-store, must-revalidate");
+//
+//        // 3. 返回二进制流和响应头
+//        return success(new ByteArrayInputStream(picInfo));
+        String base64Pic = Base64.getEncoder().encodeToString(picInfo);
+
+        // 4. 用CommonResult封装返回
+        return success(base64Pic);
+    }
 }

+ 33 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/hik/HikIsapiService.java

@@ -69,6 +69,39 @@ public class HikIsapiService {
         return response;
     }
 
+    /**
+     * 获取图片
+     * @param deviceId
+     * @return
+     * @throws IOException
+     */
+    public byte[] getPicInfo(String deviceId, String url) throws IOException {
+        HikvisionProperties.IsapiConfig.DeviceConfig device = getDeviceConfig(deviceId);
+
+        Map<String, String> headers = new HashMap<>();
+        // 注意:图片接口不需要Content-Type为application/json,这是错误的,建议删除或修改
+        // headers.put("Content-Type", "application/json");
+        // 正确的请求头应该适配图片类型
+        headers.put("Accept", "image/jpeg,image/png");
+
+        // 调用httpClient的doGet方法获取String响应
+        String response = httpClient.doGet(url, device.getUsername(), device.getPassword(),
+                device.getTimeout(), headers);
+
+        // 核心:将String转换为byte[](指定编码,推荐UTF-8)
+        // !!重要提醒:如果response是图片二进制的字符乱码,转换后也无法恢复!!
+        byte[] resultBytes = response.getBytes("UTF-8");
+
+        return resultBytes;
+
+//        Map<String, String> headers = new HashMap<>();
+//        headers.put("Content-Type", "application/json");
+//
+//        String response = httpClient.doGet(url, device.getUsername(), device.getPassword(),
+//                device.getTimeout(), headers);
+//        return response;
+    }
+
     /**
      * 构建订阅JSON
      */