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