提交 | 用户 | 时间
149dd0 1 package com.iailab.module.data.video.dahua;
H 2
3 import com.alibaba.fastjson.JSON;
4 import com.iailab.module.data.common.utils.HttpRequest;
5 import com.iailab.module.data.video.dahua.frame.DHCapturePictureFrame;
6 import com.iailab.netsdk.common.Res;
7 import com.iailab.netsdk.demo.module.LoginModule;
8 import com.iailab.netsdk.demo.module.RealPlayModule;
9 import com.iailab.netsdk.lib.NetSDKLib;
10 import com.iailab.netsdk.lib.ToolKits;
11 import com.sun.jna.CallbackThreadInitializer;
12 import com.sun.jna.Native;
13 import com.sun.jna.ptr.IntByReference;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
21dc7e 16 import org.springframework.beans.factory.annotation.Value;
149dd0 17
H 18 import java.awt.*;
19 import java.io.File;
20 import java.util.*;
21
22 /**
23  * @author PanZhibao
24  * @Description
25  * @createTime 2024年04月08日
26  */
27 public class DHCapturePictureClient {
28     private Logger logger = LoggerFactory.getLogger(getClass());
29
21dc7e 30 //    private String url = "http://localhost:48080/admin-api/data/video/camera/DHCaptureCallback";
H 31 //    @Value("${video.dahua.callback.url}")
32 //    public String url;
149dd0 33
H 34     // 登陆句柄
35     public static NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE;
36
37     // 设备信息
38     public static NetSDKLib.NET_DEVICEINFO_Ex m_stDeviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex();
39
40     private static boolean bInit = false;
41
42     private static boolean bLogopen = false;
43
44     private boolean bLogin = false;
45
46     private boolean bRealPlay = false;
47
48     private Panel realPlayWindow = new Panel();
49
50     private String ip;
51
52     private Vector<String> chnlist = new Vector<String>();
53
54     private DisConnect disConnect = new DisConnect();
55
56     private HaveReConnect haveReConnect = new HaveReConnect();
57
58     private DHCaptureReceiveCB m_CaptureReceiveCB = new DHCaptureReceiveCB();
59
60     public static boolean isInit () {
61         return bInit;
62     }
63
64     public boolean isLogin () {
65         return bLogin;
66     }
67
68     public String getIp () {
69         return ip;
70     }
71
72     public Vector<String> getChnList() {
73        return  chnlist;
74     }
75
76     // 登陆句柄
77     public NetSDKLib.LLong m_hLoginHandle = new NetSDKLib.LLong(0);
78
79     // realplay handle
80     public NetSDKLib.LLong m_hPlayHandle = new NetSDKLib.LLong(0);
81
82     private DHCapturePictureFrame cpf;
83
84     DHCapturePictureClient() {
85         cpf = new DHCapturePictureFrame(realPlayWindow);
86         cpf.setVisible(true);
87     }
88
89     /**
90      * 初始化SDK
91      *
92      * @return
93      */
94     public boolean initSDK() {
95
96         bInit = netsdk.CLIENT_Init(disConnect, null);
97         if(!bInit) {
98             System.out.println("Initialize SDK failed");
99             return false;
100         }
101
102         //打开日志,可选
103         NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
104         File path = new File("./sdklog/");
105         if (!path.exists()) {
106             path.mkdir();
107         }
108         String logPath = path.getAbsoluteFile().getParent() + "\\sdklog\\" + ToolKits.getDate() + ".log";
109         setLog.nPrintStrategy = 0;
110         setLog.bSetFilePath = 1;
111         System.arraycopy(logPath.getBytes(), 0, setLog.szLogFilePath, 0, logPath.getBytes().length);
112         System.out.println(logPath);
113         setLog.bSetPrintStrategy = 1;
114         bLogopen = netsdk.CLIENT_LogOpen(setLog);
115         if(!bLogopen ) {
116             System.err.println("Failed to open NetSDK log");
117         }
118
119         // 设置断线重连回调接口,设置过断线重连成功回调函数后,当设备出现断线情况,SDK内部会自动进行重连操作
120         // 此操作为可选操作,但建议用户进行设置
121         netsdk.CLIENT_SetAutoReconnect(haveReConnect, null);
122
123         //设置登录超时时间和尝试次数,可选
124         int waitTime = 5000; //登录请求响应超时时间设置为5S
125         int tryTimes = 1;    //登录时尝试建立链接1次
126         netsdk.CLIENT_SetConnectTime(waitTime, tryTimes);
127
128
129         // 设置更多网络参数,NET_PARAM的nWaittime,nConnectTryNum成员与CLIENT_SetConnectTime
130         // 接口设置的登录设备超时时间和尝试次数意义相同,可选
131         NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
132         netParam.nConnectTime = 10000;      // 登录时尝试建立链接的超时时间
133         netParam.nGetConnInfoTime = 3000;   // 设置子连接的超时时间
134         netParam.nGetDevInfoTime = 3000;//获取设备信息超时时间,为0默认1000ms
135         netsdk.CLIENT_SetNetworkParam(netParam);
136         if (!bInit) {
137             logger.info("Initialize SDK Failed");
138         } else {
139             logger.info("Initialize SDK Success");
140         }
141         return bInit;
142     }
143
144     /**
145      * \if ENGLISH_LANG
146      * CleanUp
147      * \else
148      * 清除环境
149      * \endif
150      */
151     public static void cleanup() {
152         if(bLogopen) {
153             netsdk.CLIENT_LogClose();
154         }
155
156         if(bInit) {
157             netsdk.CLIENT_Cleanup();
158         }
159     }
160
161     /**
162      * 登录设备
163      *
164      * @param ip
165      * @param port
166      * @param username
167      * @param password
168      */
169     public boolean login(String ip, int port, String username, String password) {
170         this.ip = ip;
171         if (!bInit) {
172             return false;
173         }
174         Native.setCallbackThreadInitializer(m_CaptureReceiveCB,
175                 new CallbackThreadInitializer(false, false, "snapPicture callback thread"));
176
177         //IntByReference nError = new IntByReference(0);
178         //入参
179         NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam=new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY();
180         pstInParam.nPort=port;
181         pstInParam.szIP=ip.getBytes();
182         pstInParam.szPassword=password.getBytes();
183         pstInParam.szUserName=username.getBytes();
184         //出参
185         NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam=new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
186         pstOutParam.stuDeviceInfo=m_stDeviceInfo;
187         //m_hLoginHandle = netsdk.CLIENT_LoginEx2(m_strIp, m_nPort, m_strUser, m_strPassword, 0, null, m_stDeviceInfo, nError);
188         m_hLoginHandle=netsdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);
189         boolean ret = m_hLoginHandle.longValue() == 0? false:true;
190
191         if (ret) {
192             bLogin = true;
193             for (int i = 1; i < LoginModule.m_stDeviceInfo.byChanNum + 1; i++) {
194                 chnlist.add(Res.string().getChannel() + " " + i);
195             }
196             NetSDKLib.NETSDK_INSTANCE.CLIENT_SetSnapRevCallBack(m_CaptureReceiveCB, null);
197         } else {
198             bLogin = false;
199         };
200         return bLogin;
201     }
202
203     /**
204      * 登出设备
205      */
206     public void logout() {
207         if(m_hLoginHandle.longValue() == 0) {
208             return;
209         }
210         RealPlayModule.stopRealPlay(m_hPlayHandle);
211         boolean bRet = netsdk.CLIENT_Logout(m_hLoginHandle);
212         if(bRet) {
213             m_hLoginHandle.setValue(0);
214         }
215         for (int i = 0; i < LoginModule.m_stDeviceInfo.byChanNum; i++) {
216             chnlist.clear();
217         }
218         bInit = false;
219     }
220
221     /**
222      * 远程抓图
223      *
224      * @param channel
225      * @param serial
226      * @return
227      */
228     public boolean remoteCapturePicture(int channel, String serial) {
229         if (!bLogin) {
230             logger.info("not login!");
231             return false;
232         }
233         // send caputre picture command to device
234         NetSDKLib.SNAP_PARAMS stuSnapParams = new NetSDKLib.SNAP_PARAMS();
235         stuSnapParams.Channel = channel;              // channel
236         stuSnapParams.mode = 0;                        // capture picture mode
237         stuSnapParams.Quality = 3;                    // picture quality
238         stuSnapParams.InterSnap = 0;                 // timer capture picture time interval
239         stuSnapParams.CmdSerial = Integer.parseInt(serial);              // request serial
240         IntByReference reserved = new IntByReference(0);
241         return NetSDKLib.NETSDK_INSTANCE.CLIENT_SnapPictureEx(this.m_hLoginHandle, stuSnapParams, reserved);
242     }
243
244     /**
245      * 开始预览
246      *
247      * @param channel
248      * @param stream
249      * @param realPlayWindow
250      * @return
251      */
252     public void startRealPlay(int channel, int stream, Panel realPlayWindow) {
253         this.m_hPlayHandle = netsdk.CLIENT_RealPlayEx(this.m_hLoginHandle, channel, Native.getComponentPointer(realPlayWindow), stream);
254
255         if(m_hPlayHandle.longValue() == 0) {
256             System.err.println("开始实时预览失败,错误码" + ToolKits.getErrorCodePrint());
257         } else {
258             System.out.println("Success to start realplay");
259         }
260         bRealPlay = true;
261     }
262
263     /**
264      * 停止预览
265      */
266     public void stopRealPlay() {
267         if(m_hPlayHandle.longValue() == 0) {
268             return;
269         }
270
271         boolean bRet = netsdk.CLIENT_StopRealPlayEx(m_hPlayHandle);
272         if(bRet) {
273             m_hPlayHandle.setValue(0);
274         }
275
276         realPlayWindow.repaint();
277         bRealPlay = false;
278     }
279
280     /**
281      * 本地抓图
282      *
283      * @param channel
284      * @param cmdSerial
285      * @return
286      */
287     public synchronized boolean localCapturePicture(int channel, String cmdSerial) {
288         System.out.println("localCapturePicture:");
289         try {
290             this.startRealPlay(channel, 0, realPlayWindow);
291             if(!bRealPlay) {
292                 return false;
293             }
294             Thread.sleep(2000);
295             Calendar calendar = Calendar.getInstance();
296             String strFileName = DHSavePath.getSavePath().getSaveCapturePath(cmdSerial, calendar.getTime());
297             if (!netsdk.CLIENT_CapturePictureEx(this.m_hPlayHandle, strFileName, NetSDKLib.NET_CAPTURE_FORMATS.NET_CAPTURE_JPEG)) {
298                 System.err.printf("CLIENT_CapturePicture Failed!" + ToolKits.getErrorCodePrint());
299                 return false;
300             } else {
301                 System.out.println("CLIENT_CapturePicture success");
302                 // 回调处理图片
303                 Map<String, String> params = new HashMap<>();
304                 params.put("strFileName", strFileName);
305                 params.put("cmdSerial", cmdSerial);
21dc7e 306                 HttpRequest.doPost(DHConfiguration.staticUrl, JSON.toJSONString(params), "utf-8", "");
149dd0 307             }
H 308             return true;
309         } catch (Exception ex) {
310             ex.printStackTrace();
311             return false;
312         } finally {
313             this.stopRealPlay();
314         }
315     }
316 }