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