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