From 43fc617eb7b94c7e1cc66884cbb9672da8b3bc32 Mon Sep 17 00:00:00 2001
From: ‘liusuyi’ <1951119284@qq.com>
Date: 星期三, 14 六月 2023 17:13:18 +0800
Subject: [PATCH] 优化海康sdk录像

---
 ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java |  191 ++++++++++++++++++++---------------------------
 1 files changed, 80 insertions(+), 111 deletions(-)

diff --git a/ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java b/ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java
index 646c238..1d5a6cd 100644
--- a/ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java
+++ b/ard-work/src/main/java/com/ruoyi/device/hiksdk/service/impl/HikClientServiceImpl.java
@@ -35,7 +35,6 @@
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
 import java.util.*;
-
 import static com.ruoyi.device.hiksdk.util.hikSdkUtil.HCNetSDK.*;
 
 /**
@@ -184,7 +183,7 @@
         System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length());
         m_strLoginInfo.wPort = m_sPort;
         m_strLoginInfo.byVerifyMode = 0;
-        m_strLoginInfo.byLoginMode=0;
+        m_strLoginInfo.byLoginMode = 0;
         //鏄惁寮傛鐧诲綍锛�0- 鍚︼紝1- 鏄�  windowsSDK閲屾槸true鍜宖alse
         m_strLoginInfo.bUseAsynLogin = true;
         //寮傛鐧诲綍鍥炶皟
@@ -663,7 +662,8 @@
         NET_DVR_PTZPOS m_ptzPosCurrent = new NET_DVR_PTZPOS();
         m_ptzPosCurrent.wAction = 1;
         try {
-            double[] cameraPositon = cmd.getCamPosition();
+            ArdCameras cameras = ardCamerasMapper.selectArdCamerasById(cameraId);
+            double[] cameraPositon = new double[]{cameras.getLongitude(), cameras.getLatitude(), cameras.getAltitude()};
             double[] targetPositions = cmd.getTargetPosition();
             double[] cameraPTZ = GisUtil.getCameraPTZ(cameraPositon, targetPositions, 20, 150);
             String p = String.valueOf((int) (cameraPTZ[0] * 10));
@@ -681,7 +681,7 @@
             }
             return bool;
         } catch (Exception ex) {
-            log.error(ex.getMessage());
+            log.error("寮曞寮傚父:" + ex.getMessage());
             return false;
         }
     }
@@ -1232,98 +1232,74 @@
      * @淇敼浜哄拰鍏跺畠淇℃伅
      */
     @Override
-    @SdkOperate
     public String record(CameraCmd cmd) {
-        String cameraId = cmd.getCameraId();
-        Integer channelNum = cmd.getChannelNum();
-        boolean enable = cmd.isEnable();
-        if (!GlobalVariable.loginMap.containsKey(cameraId)) {
-            return "";
-        }
-        Integer userId = GlobalVariable.loginMap.get(cameraId);
-        String path = "";
-        //棰勮鍙傛暟
-        NET_DVR_PREVIEWINFO previewinfo = new NET_DVR_PREVIEWINFO();
-        previewinfo.read();
-        previewinfo.lChannel = channelNum;
-        previewinfo.dwStreamType = 0;//鐮佹祦绫诲瀷锛�0-涓荤爜娴侊紝1-瀛愮爜娴侊紝2-涓夌爜娴侊紝3-铏氭嫙鐮佹祦锛屼互姝ょ被鎺�
-        previewinfo.dwLinkMode = 0;//杩炴帴鏂瑰紡锛�0-TCP鏂瑰紡锛�1-UDP鏂瑰紡锛�2-澶氭挱鏂瑰紡锛�3-RTP鏂瑰紡锛�4-RTP/RTSP锛�5-RTP/HTTP锛�6-HRUDP锛堝彲闈犱紶杈擄級锛�7-RTSP/HTTPS锛�8-NPQ
-        previewinfo.hPlayWnd = null;//鎾斁绐楀彛鐨勫彞鏌勶紝涓篘ULL琛ㄧず涓嶈В鐮佹樉绀恒��
-        previewinfo.bBlocked = 0;//0- 闈為樆濉炲彇娴侊紝1-闃诲鍙栨祦
-        previewinfo.byNPQMode = 0;//NPQ妯″紡锛�0-鐩磋繛妯″紡锛�1-杩囨祦濯掍綋妯″紡
-        previewinfo.write();
-        int lRealHandle;
-        if (enable) {
-            if (!GlobalVariable.user_real_Map.containsKey(userId)) {
-                lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
-                if (lRealHandle == -1) {
-                    int iErr = hCNetSDK.NET_DVR_GetLastError();
-                    log.error("鍙栨祦澶辫触" + iErr);
-                    return "";
-                }
-                log.info("鍙栨祦鎴愬姛");
-
-                File file = new File("D:/record/temp.mp4");
-                if (!file.exists()) {
-                    try {
-                        File fileParent = file.getParentFile();
-                        if (!fileParent.exists()) {
-                            fileParent.mkdirs();
-                        }
-                        file.createNewFile();
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                    }
-                }
-                try {
-                    path = file.getCanonicalPath();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-                recordInfo info = new recordInfo();
-                info.setLRealHandle(lRealHandle);
-                info.setRecordPath(path);
-                GlobalVariable.user_real_Map.put(userId, info);
-            }
-            recordInfo info = GlobalVariable.user_real_Map.get(userId);
-            if (!hCNetSDK.NET_DVR_SaveRealData_V30(info.getLRealHandle(), 1, info.getRecordPath())) {
-                log.error("淇濆瓨瑙嗛鏂囦欢鍒颁复鏃舵枃浠跺す澶辫触 閿欒鐮佷负:  " + hCNetSDK.NET_DVR_GetLastError());
-                return "淇濆瓨瑙嗛鏂囦欢鍒颁复鏃舵枃浠跺す澶辫触 閿欒鐮佷负:" + hCNetSDK.NET_DVR_GetLastError();
-            }
-            log.info("褰曞儚寮�濮�");
-            //return info.getRecordPath();
-            return "";
-        } else {
-            recordInfo info = GlobalVariable.user_real_Map.get(userId);
-            if (StringUtils.isNull(info)) {
+        try {
+            String cameraId = cmd.getCameraId();
+            Integer channelNum = cmd.getChannelNum();
+            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
+            boolean enable = cmd.isEnable();
+            if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                 return "";
             }
-            hCNetSDK.NET_DVR_StopRealPlay(info.getLRealHandle());
-            log.info("褰曞儚鍋滄");
-            //瀛樺叆minio
-            String BucketName = "record";
-            String uuid = UUID.randomUUID().toString().replace("-", "");
-            String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
-            String ObjectName = cameraId + "/" + time + "/" + uuid + ".mp4";
-            String ContentType = "video/MP4";
-            FileInputStream stream = null;
-            try {
-                stream = new FileInputStream(info.getRecordPath());
-            } catch (FileNotFoundException e) {
-                e.printStackTrace();
+            Integer userId = GlobalVariable.loginMap.get(cameraId);
+            //寮哄埗I甯х粨鏋勪綋瀵硅薄
+            HCNetSDK.NET_DVR_I_FRAME netDvrIFrame = new HCNetSDK.NET_DVR_I_FRAME();   //鏂板缓缁撴瀯浣撳璞�
+            netDvrIFrame.read();
+            netDvrIFrame.dwChannel = channelNum;//鍥犱负涓婃枃浠g爜涓缃簡閫氶亾鍙凤紝鎸夌収涓婃枃涓殑璁剧疆
+            netDvrIFrame.byStreamType = 0;
+            netDvrIFrame.dwSize = netDvrIFrame.size();
+            netDvrIFrame.write();
+            if (!hCNetSDK.NET_DVR_RemoteControl(userId, 3402, netDvrIFrame.getPointer(), netDvrIFrame.dwSize)) {
+                log.error("寮哄埗I甯� 閿欒鐮佷负:  " + hCNetSDK.NET_DVR_GetLastError());
             }
+            //棰勮鍙傛暟
+            NET_DVR_PREVIEWINFO previewinfo = new NET_DVR_PREVIEWINFO();
+            previewinfo.read();
+            previewinfo.lChannel = channelNum;
+            previewinfo.dwStreamType = 0;//鐮佹祦绫诲瀷锛�0-涓荤爜娴侊紝1-瀛愮爜娴侊紝2-涓夌爜娴侊紝3-铏氭嫙鐮佹祦锛屼互姝ょ被鎺�
+            previewinfo.dwLinkMode = 0;//杩炴帴鏂瑰紡锛�0-TCP鏂瑰紡锛�1-UDP鏂瑰紡锛�2-澶氭挱鏂瑰紡锛�3-RTP鏂瑰紡锛�4-RTP/RTSP锛�5-RTP/HTTP锛�6-HRUDP锛堝彲闈犱紶杈擄級锛�7-RTSP/HTTPS锛�8-NPQ
+            previewinfo.hPlayWnd = null;//鎾斁绐楀彛鐨勫彞鏌勶紝涓篘ULL琛ㄧず涓嶈В鐮佹樉绀恒��
+            previewinfo.bBlocked = 0;//0- 闈為樆濉炲彇娴侊紝1-闃诲鍙栨祦
+            previewinfo.byNPQMode = 0;//NPQ妯″紡锛�0-鐩磋繛妯″紡锛�1-杩囨祦濯掍綋妯″紡
+            previewinfo.write();
             String url = "";
-            try {
+            if (enable) {
+                if (!GlobalVariable.previewMap.containsKey(cameraId)) {
+                    int lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
+                    if (lRealHandle == -1) {
+                        log.error("鍙栨祦澶辫触" + hCNetSDK.NET_DVR_GetLastError());
+                        return "";
+                    }
+                    log.info("鍙栨祦鎴愬姛");
+                    GlobalVariable.previewMap.put(cameraId, lRealHandle);
+                }
+                if (!hCNetSDK.NET_DVR_SaveRealData_V30(GlobalVariable.previewMap.get(cameraId), 2, path)) {
+                    log.error("淇濆瓨瑙嗛鏂囦欢鍒颁复鏃舵枃浠跺す澶辫触 閿欒鐮佷负:  " + hCNetSDK.NET_DVR_GetLastError());
+                    return "";
+                }
+                log.info("褰曞儚寮�濮�");
+            } else {
+                if (GlobalVariable.previewMap.containsKey(cameraId)) {
+                    Integer lRealHandle = GlobalVariable.previewMap.get(cameraId);
+                    hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
+                    GlobalVariable.previewMap.remove(cameraId);
+                }
+                log.info("褰曞儚鍋滄");
+                //瀛樺叆minio
+                String BucketName = cmd.getRecordBucketName();
+                String ObjectName = cmd.getRecordObjectName();
+                String ContentType = "video/MP4";
+                FileInputStream stream = new FileInputStream(path);
                 boolean b = MinioUtils.uploadObject(BucketName, ObjectName, stream, stream.available(), ContentType);
                 if (b) {
                     url = MinioUtils.getBucketObjectUrl(BucketName, ObjectName);
-                    log.info("涓婁紶鏂囦欢鎴愬姛!" + url);
+                    log.info("涓婁紶鏂囦欢鎴愬姛!" + MinioClientSingleton.domainUrl + "/" + BucketName + "/" + ObjectName);
                 }
-            } catch (IOException ex) {
-                log.error("涓婁紶鏂囦欢寮傚父锛�" + ex.getMessage());
             }
-            GlobalVariable.user_real_Map.remove(userId);
             return url;
+        } catch (Exception ex) {
+            log.error("褰曞儚寮傚父" + ex.getMessage());
+            return "";
         }
     }
 
@@ -1332,6 +1308,7 @@
         try {
             String cameraId = cmd.getCameraId();
             Integer channelNum = cmd.getChannelNum();
+            String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
             boolean enable = cmd.isEnable();
             if (!GlobalVariable.loginMap.containsKey(cameraId)) {
                 return;
@@ -1357,46 +1334,38 @@
             previewinfo.bBlocked = 0;//0- 闈為樆濉炲彇娴侊紝1-闃诲鍙栨祦
             previewinfo.byNPQMode = 0;//NPQ妯″紡锛�0-鐩磋繛妯″紡锛�1-杩囨祦濯掍綋妯″紡
             previewinfo.write();
-            int lRealHandle;
             if (enable) {
-                if (!GlobalVariable.user_real_Map.containsKey(userId)) {
-                    lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
+                if (!GlobalVariable.previewMap.containsKey(cameraId)) {
+                    int lRealHandle = hCNetSDK.NET_DVR_RealPlay_V40(userId, previewinfo, null, null);
                     if (lRealHandle == -1) {
                         log.error("鍙栨祦澶辫触" + hCNetSDK.NET_DVR_GetLastError());
                         return;
                     }
                     log.info("鍙栨祦鎴愬姛");
-
-                    String path = FileUtils.createFile("D:/recordTemp/" + cameraId + ".mp4");
-                    recordInfo info = new recordInfo();
-                    info.setLRealHandle(lRealHandle);
-                    info.setRecordPath(path);
-                    GlobalVariable.user_real_Map.put(userId, info);
+                    GlobalVariable.previewMap.put(cameraId, lRealHandle);
                 }
-                recordInfo info = GlobalVariable.user_real_Map.get(userId);
-                if (!hCNetSDK.NET_DVR_SaveRealData_V30(info.getLRealHandle(), 2, info.getRecordPath())) {
+
+                if (!hCNetSDK.NET_DVR_SaveRealData_V30(GlobalVariable.previewMap.get(cameraId), 2, path)) {
                     log.error("淇濆瓨瑙嗛鏂囦欢鍒颁复鏃舵枃浠跺す澶辫触 閿欒鐮佷负:  " + hCNetSDK.NET_DVR_GetLastError());
                     return;
                 }
                 log.info("褰曞儚寮�濮�");
             } else {
-                recordInfo info = GlobalVariable.user_real_Map.get(userId);
-                if (StringUtils.isNull(info)) {
-                    return;
+                if (GlobalVariable.previewMap.containsKey(cameraId)) {
+                    Integer lRealHandle = GlobalVariable.previewMap.get(cameraId);
+                    hCNetSDK.NET_DVR_StopRealPlay(lRealHandle);
+                    GlobalVariable.previewMap.remove(cameraId);
                 }
-                hCNetSDK.NET_DVR_StopRealPlay(info.getLRealHandle());
                 log.info("褰曞儚鍋滄");
-                if (cmd.isUploadMinio()) {
-                    //瀛樺叆minio
-                    String BucketName = cmd.getRecordBucketName();
-                    String ObjectName = cmd.getRecordObjectName();
-                    String ContentType = "video/MP4";
-                    FileInputStream stream = new FileInputStream(info.getRecordPath());
-                    boolean b = MinioUtils.uploadObject(BucketName, ObjectName, stream, stream.available(), ContentType);
-                    if (b) {
-                        log.info("涓婁紶鏂囦欢鎴愬姛!" + MinioClientSingleton.domainUrl + "/" + BucketName + "/" + ObjectName);
-                        GlobalVariable.user_real_Map.remove(userId);
-                    }
+
+                //瀛樺叆minio
+                String BucketName = cmd.getRecordBucketName();
+                String ObjectName = cmd.getRecordObjectName();
+                String ContentType = "video/MP4";
+                FileInputStream stream = new FileInputStream(path);
+                boolean b = MinioUtils.uploadObject(BucketName, ObjectName, stream, stream.available(), ContentType);
+                if (b) {
+                    log.info("涓婁紶鏂囦欢鎴愬姛!" + MinioClientSingleton.domainUrl + "/" + BucketName + "/" + ObjectName);
                 }
             }
         } catch (Exception ex) {

--
Gitblit v1.9.3