dengzedong
2024-12-24 c9dd12a0042b50af36edf2ae710d33430542385d
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package com.iailab.netsdk.demo.frame.vto;
 
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
 
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
 
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
 
import com.iailab.netsdk.common.Base64;
import com.iailab.netsdk.common.Res;
import com.iailab.netsdk.demo.module.LoginModule;
import com.iailab.netsdk.lib.NetSDKLib;
import com.iailab.netsdk.lib.ToolKits;
 
public class CollectionFingerPrint extends JDialog {
 
    private final JPanel contentPanel = new JPanel();
    private JTextField ipTextField;
    private JTextField portTextField;
    private JTextField usernameTextField;
    private JPasswordField passwordField;
    private JLabel collectionResult;
 
    private JButton btnLogin;
    private JButton btnLogout;
    private JButton btnCollection;
 
    private byte[] packageData;
    private int packageLen;
    private boolean bcollectionResult = false;
    private boolean isListen;
 
    public byte[] getPackageData() {
        return packageData;
    }
 
    public void setPackageData(byte[] packageData) {
        this.packageData = packageData;
 
    }
 
    public void setLabelResult(byte[] packageData) {
        collectionResult.setText(Base64.getEncoder().encodeToString(packageData));
    }
 
    public int getPackageLen() {
        return packageLen;
    }
 
    public void setPackageLen(int packageLen) {
        this.packageLen = packageLen;
    }
 
    public boolean isCollectionResult() {
        return bcollectionResult;
    }
 
    public void setCollectionResult(boolean bcollectionResult) {
        this.bcollectionResult = bcollectionResult;
        //显示结果
        collectionResult.setText(this.bcollectionResult ? "success" : "failed");
    }
 
    public void stopListen() {
        //获取到信息,停止监听
        if (loginHandler != null && loginHandler.longValue() != 0) {
            stopListen(loginHandler);
        }
        //获取按钮使能
        btnCollection.setEnabled(true);
        //设置监听状态
        isListen = false;
    }
 
    private NetSDKLib.LLong loginHandler;
    private NetSDKLib.NET_DEVICEINFO_Ex deviceinfoEx = new NetSDKLib.NET_DEVICEINFO_Ex();
 
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            CollectionFingerPrint dialog = new CollectionFingerPrint();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * Create the dialog.
     */
    public CollectionFingerPrint() {
        setBounds(100, 100, 304, 397);
        setTitle(Res.string().getVTOOperateCollectionFingerPrintTitle());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            JPanel panel = new JPanel();
            panel.setBorder(new TitledBorder(null, Res.string().getLogin(), TitledBorder.LEADING,
                    TitledBorder.TOP, null, null));
            panel.setBounds(10, 10, 268, 167);
            contentPanel.add(panel);
            panel.setLayout(null);
            {
                JLabel lblNewLabel = new JLabel(Res.string().getIp());
                lblNewLabel.setBounds(10, 23, 66, 15);
                panel.add(lblNewLabel);
            }
            {
                ipTextField = new JTextField();
                ipTextField.setText("172.23.32.61");
                ipTextField.setBounds(103, 20, 155, 21);
                panel.add(ipTextField);
                ipTextField.setColumns(10);
            }
            {
                JLabel lblPort = new JLabel(Res.string().getPort());
                lblPort.setBounds(10, 48, 83, 15);
                panel.add(lblPort);
            }
            {
                portTextField = new JTextField();
                portTextField.setText("37777");
                portTextField.setColumns(10);
                portTextField.setBounds(103, 45, 155, 21);
                panel.add(portTextField);
            }
            {
                JLabel lblName = new JLabel(Res.string().getUserName());
                lblName.setBounds(10, 73, 83, 15);
                panel.add(lblName);
            }
            {
                usernameTextField = new JTextField();
                usernameTextField.setText("admin");
                usernameTextField.setColumns(10);
                usernameTextField.setBounds(103, 70, 155, 21);
                panel.add(usernameTextField);
            }
            {
                JLabel lblPassword = new JLabel(Res.string().getPassword());
                lblPassword.setBounds(10, 98, 90, 15);
                panel.add(lblPassword);
            }
 
            passwordField = new JPasswordField();
            passwordField.setBounds(103, 95, 155, 18);
            passwordField.setText("admin123");
            panel.add(passwordField);
            {
                btnLogin = new JButton(Res.string().getLogin());
                btnLogin.setBounds(7, 134, 111, 23);
                panel.add(btnLogin);
                btnLogin.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (login()) {
                            btnCollection.setEnabled(true);
                        }
                    }
                });
            }
            {
                btnLogout = new JButton(Res.string().getLogout());
                btnLogout.setBounds(153, 134, 105, 23);
                panel.add(btnLogout);
                btnLogout.setEnabled(false);
                btnLogout.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        logout();
                    }
                });
            }
        }
        {
            JPanel panel = new JPanel();
            panel.setBorder(new TitledBorder(null, Res.string().getOperate(), TitledBorder.LEADING, TitledBorder.TOP,
                    null, null));
            panel.setBounds(10, 187, 268, 129);
            contentPanel.add(panel);
            panel.setLayout(null);
            {
                btnCollection = new JButton(Res.string().getStartCollection());
                btnCollection.setBounds(10, 26, 227, 41);
                panel.add(btnCollection);
 
                collectionResult = new JLabel(Res.string().getCollectionResult());
                collectionResult.setBounds(10, 77, 227, 26);
                panel.add(collectionResult);
                btnCollection.setEnabled(false);
                final CollectionFingerPrint print = this;
                btnCollection.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        isListen = startListen(loginHandler, VTOMessageCallBack.getINSTANCE(null, print));
                        if (isListen) {
                            //下发采集信息指令
                            if (!collectionFinger()) {
                                stopListen(loginHandler);
                            } else {
                                // 使其失效
                                btnCollection.setEnabled(false);
                            }
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        Thread.sleep(20000);
                                        //等待二十秒,如果没有获取到信息,则停止获取
                                        if (isListen) {
                                            stopListen();
                                        }
 
                                    } catch (InterruptedException interruptedException) {
                                        interruptedException.printStackTrace();
                                    }
                                }
                            }).start();
                        }
 
                    }
                });
                addWindowListener(new WindowAdapter() {
 
                    @Override
                    public void windowClosed(WindowEvent e) {
                        super.windowClosed(e);
                        //已经登录
                        if (loginHandler != null && loginHandler.longValue() != 0) {
                            if (isListen) {
                                //停止监听
                                stopListen(loginHandler);
                            }
                            //登出
                            logout();
                        }
                        //按钮复位
                        btnLogin.setEnabled(true);
                        btnLogout.setEnabled(false);
                        btnCollection.setEnabled(false);
                        //清除信息数据
                        bcollectionResult=false;
                        packageData=null;
                        //清除label显示
                        collectionResult.setText(Res.string().getCollectionResult());
                    }
                });
            }
 
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
                okButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        setVisible(false);
                    }
                });
 
            }
        }
    }
 
    /**
     * 登录
     */
    public boolean login() {
        if (login(ipTextField.getText(), Integer.parseInt(portTextField.getText()), usernameTextField.getText(),
                new String(passwordField.getPassword()))) {
            btnLogin.setEnabled(false);
            btnLogout.setEnabled(true);
            btnCollection.setEnabled(true);
        } else {
            JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(),
                    Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
 
    /**
     * \if ENGLISH_LANG Login Device \else 登录设备 \endif
     */
    public boolean login(String m_strIp, int m_nPort, String m_strUser, String m_strPassword) {
        // IntByReference nError = new IntByReference(0);
        // 入参
        NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY();
        pstInParam.nPort = m_nPort;
        pstInParam.szIP = m_strIp.getBytes();
        pstInParam.szPassword = m_strPassword.getBytes();
        pstInParam.szUserName = m_strUser.getBytes();
        // 出参
        NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
        pstOutParam.stuDeviceInfo = deviceinfoEx;
        loginHandler = LoginModule.netsdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);
        if (loginHandler.longValue() == 0) {
            JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(),
                    Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
        } else {
            System.out.println("Login Success [ " + m_strIp + " ]");
        }
 
        return loginHandler.longValue() == 0 ? false : true;
    }
 
    public long getLoginHandler() {
        if (loginHandler != null) {
            return loginHandler.longValue();
        }
        return 0;
    }
 
    /**
     * \if ENGLISH_LANG Logout Device \else 登出设备 \endif
     */
    public boolean logout() {
        if (loginHandler.longValue() == 0) {
            return false;
        }
 
        boolean bRet = LoginModule.netsdk.CLIENT_Logout(loginHandler);
        if (bRet) {
            loginHandler.setValue(0);
            btnLogin.setEnabled(true);
            btnLogout.setEnabled(false);
        } else {
            JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(),
                    Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
        }
 
        return bRet;
    }
 
    /**
     * 下发采集信息指令
     *
     * @return
     */
    public boolean collectionFinger() {
        NetSDKLib.NET_CTRL_CAPTURE_FINGER_PRINT capture = new NetSDKLib.NET_CTRL_CAPTURE_FINGER_PRINT();
        capture.nChannelID = 0;
        System.arraycopy("1".getBytes(), 0, capture.szReaderID, 0, "1".getBytes().length);
        Pointer pointer = new Memory(capture.size());
        ToolKits.SetStructDataToPointer(capture, pointer, 0);
        boolean ret = LoginModule.netsdk.CLIENT_ControlDevice(loginHandler, NetSDKLib.CtrlType.CTRLTYPE_CTRL_CAPTURE_FINGER_PRINT, pointer, 100000);
        if (!ret) {
            JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(),
                    Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
 
    }
 
    public boolean startListen(NetSDKLib.LLong loginHandler, NetSDKLib.fMessCallBack cbMessage) {
 
        LoginModule.netsdk.CLIENT_SetDVRMessCallBack(cbMessage, null); // set alarm listen callback
 
        if (!LoginModule.netsdk.CLIENT_StartListenEx(loginHandler)) {
            JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(),
                    Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return false;
        } else {
            System.out.println("CLIENT_StartListenEx success.");
        }
        return true;
    }
 
    public boolean stopListen(NetSDKLib.LLong loginHandler) {
        if (!LoginModule.netsdk.CLIENT_StopListen(loginHandler)) {
            JOptionPane.showMessageDialog(null, Res.string().getStopListenFailed()+","+ ToolKits.getErrorCodeShow(),
                    Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return false;
        } else {
            System.out.println("CLIENT_StopListen success.");
        }
        return true;
    }
 
    /**
     * 清除获取信息的状态
     */
    public void clearStatus() {
        this.setPackageData(null);
        this.setCollectionResult(false);
        this.setPackageLen(0);
    }
}