1
zhanghua
2024-09-26 c775c6953d9759e70f08acbfa8f6d7490aaae3d1
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
package com.netsdk.demo.example.trafficParking;
 
 
import com.netsdk.demo.util.EventTaskHandler;
import com.netsdk.demo.util.QueueGeneration;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.NetSDKLib.EVENT_COMM_INFO;
import com.netsdk.lib.NetSDKLib.LLong;
import com.netsdk.lib.NetSDKLib.NET_MSG_OBJECT;
import com.netsdk.lib.NetSDKLib.NET_TIME_EX;
import com.netsdk.lib.NetSDKLib.NET_PIC_INFO;
import com.netsdk.lib.ToolKits;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
 
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.util.Vector;
 
import static com.netsdk.lib.Utils.getOsPrefix;
 
 
class ITSParkingEventMsg {
 
    static NetSDKLib netsdkApi = NetSDKLib.NETSDK_INSTANCE;
    static NetSDKLib configApi = NetSDKLib.CONFIG_INSTANCE;
 
    private SDKEnvironment sdkEnv;
 
    // 登录参数
    private String address = "172.24.1.162";
    private Integer port = new Integer("37777");
    private String username = "admin";
    private String password = "admin123";
 
    // 设备信息
    private NetSDKLib.NET_DEVICEINFO_Ex deviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex();
    // 句柄信息
    private LLong loginHandle = new LLong(0); // 登录句柄
    private LLong playHandle = new LLong(0);  // 预览句柄
    private LLong attachHandle = new LLong(0); // 智能事件订阅句柄
    // 事件回调
    private fAnalyzerDataCB m_AnalyzerDataCB = new fAnalyzerDataCB(); // 智能事件回调
    // Flags
    private boolean bTriggerBtnClick = false;
    private boolean bAttachFlag = false;
    private boolean bRealplayFlags = false;
 
    // 通道
    private Vector<String> chnlist = new Vector<String>();
 
    public ITSParkingEventMsg() {
        sdkEnv = new SDKEnvironment();
        sdkEnv.init();     // 初始化
 
        // 启动回调的消息队列
        eventCBQueueService.init();
 
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                mainFrame = new JFrame("有车无车事件处理Demo");
                mainFrame.setSize(900, 600);
                mainFrame.setLayout(new BorderLayout());
                mainFrame.setLocationRelativeTo(null);
                mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                mainFrame.setVisible(true);
 
                loginJPanel = new LoginPanel(); // 登录面板
                messagePanel = new MessagePanel(); // 事件信息提示
                realPlayPanel = new RealPlayPanel(); // 实时预览
 
                mainFrame.add(loginJPanel, BorderLayout.NORTH);
                mainFrame.add(realPlayPanel, BorderLayout.WEST);
                mainFrame.add(messagePanel, BorderLayout.CENTER);
 
                WindowAdapter closeWindowAdapter = new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.out.println("Window Closing");
                        logoutBtnPerformed(null); // 登出
                        sdkEnv.cleanup();
                        mainFrame.dispose();
                    }
                };
                mainFrame.addWindowListener(closeWindowAdapter);
            }
        });
    }
 
    /**
     * NetSDK 库初始化相关
     */
    private class SDKEnvironment {
 
        private boolean bInit = false;
        private boolean bLogopen = false;
 
        private DisConnect disConnect = new DisConnect();  // 设备断线通知回调
        private HaveReConnect haveReConnect = new HaveReConnect(); // 网络连接恢复
 
        // 初始化
        public boolean init() {
 
            // SDK 库初始化, 并设置断线回调
            bInit = netsdkApi.CLIENT_Init(disConnect, null);
            if (!bInit) {
                System.err.println("Initialize SDK failed");
                return false;
            }
 
            // 打开日志,可选
            NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
 
            File path = new File(".");
            String logPath = path.getAbsoluteFile().getParent() + "\\sdk_log\\ITSParkingEventMsg_" + System.currentTimeMillis() + ".log";
 
            setLog.bSetFilePath = 1;
            System.arraycopy(logPath.getBytes(), 0, setLog.szLogFilePath, 0, logPath.getBytes().length);
 
            setLog.bSetPrintStrategy = 1;
            setLog.nPrintStrategy = 0;
            bLogopen = netsdkApi.CLIENT_LogOpen(setLog);
            if (!bLogopen) {
                System.err.println("Failed to open NetSDK log !!!");
            }
 
            // 获取版本, 可选操作
            System.out.printf("NetSDK Version [%d]\n", netsdkApi.CLIENT_GetSDKVersion());
 
            // 设置断线重连回调接口,设置过断线重连成功回调函数后,当设备出现断线情况,SDK内部会自动进行重连操作
            // 此操作为可选操作,但建议用户进行设置
            netsdkApi.CLIENT_SetAutoReconnect(haveReConnect, null);
 
            // 设置登录超时时间和尝试次数 , 此操作为可选操作
            int waitTime = 5000;   // 登录请求响应超时时间设置为 5s
            int tryTimes = 1;      // 登录时尝试建立链接3次
            netsdkApi.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.nPicBufSize = 8 * 1024 * 1024;
            netsdkApi.CLIENT_SetNetworkParam(netParam);
 
            return true;
        }
 
        // 清除环境
        public void cleanup() {
            if (bLogopen) {
                netsdkApi.CLIENT_LogClose();
            }
 
            if (bInit) {
                netsdkApi.CLIENT_Cleanup();
            }
        }
 
        // 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
        public class DisConnect implements NetSDKLib.fDisConnect {
            public void invoke(LLong lLoginID, String pchDVRIP, int nDVRPort, Pointer dwUser) {
                System.out.printf("Device[%s] Port[%d] Disconnect!\n", pchDVRIP, nDVRPort);
                messageTextArea.append(String.format("Device[%s] Port[%d] Disconnect!\n", pchDVRIP, nDVRPort));
 
                // 取消订阅
                detachIVSEvent();
            }
        }
 
        // 网络连接恢复,设备重连成功回调
        // 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数
        public class HaveReConnect implements NetSDKLib.fHaveReConnect {
            public void invoke(LLong lLoginID, String pchDVRIP, int nDVRPort, Pointer dwUser) {
                System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
                messageTextArea.append(String.format("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort));
 
                // 重新订阅
                attachIVSEvent();
            }
        }
    }
 
    ///////////////// 事件动作相关接口 ///////////////////////////
 
    /**
     * 登录按钮
     */
    private void loginBtnPerformed(ActionEvent e) {
        address = ipTextArea.getText();
        port = Integer.parseInt(portTextArea.getText());
        username = nameTextArea.getText();
        password = new String(passwordTextArea.getPassword());
 
        System.out.println("设备地址:" + address
                + "\n端口号:" + port
                + "\n用户名:" + username
                + "\n密码:" + password);
        messageTextArea.append("设备地址:" + address
                + "\n端口号:" + port
                + "\n用户名:" + username
                + "\n密码:" + password + "\n");
 
        // 登录设备
        IntByReference nError = new IntByReference(0);
 
        NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstlnParam =
                new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY() {{
                    szIP = address.getBytes();
                    nPort = port;
                    szUserName = username.getBytes();
                    szPassword = password.getBytes();
                }};   // 输入结构体参数
        NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam =
                new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();    // 输结构体参数
        pstOutParam.stuDeviceInfo = deviceInfo;
 
        // 高安全登陆
        loginHandle = netsdkApi.CLIENT_LoginWithHighLevelSecurity(pstlnParam, pstOutParam);
        //  loginHandle = netsdkApi.CLIENT_LoginEx2(address, port, username, password, 0, null, deviceInfo, nError);
        if (loginHandle.longValue() == 0) {
            System.err.printf("Login Device[%s] Port[%d]Failed. Last Error[%x]\n", address, port, netsdkApi.CLIENT_GetLastError());
            JOptionPane.showMessageDialog(mainFrame, "登录失败");
        } else {
            System.out.println("Login Success [ " + address + " ]");
            messageTextArea.append(String.format("Login Success [ %s ]\n", address));
 
            JOptionPane.showMessageDialog(mainFrame, "登录成功");
            loginJPanel.enableCompents(true);
 
            for (int i = 0; i < deviceInfo.byChanNum; i++) {
                chnlist.add("通道 " + i);
            }
            // 预选 0 通道
            if (deviceInfo.byChanNum > 0) {
                chnComboBox.setSelectedIndex(0);
            }
        }
    }
 
    /**
     * 登出按钮
     */
    private void logoutBtnPerformed(ActionEvent e) {
        if (loginHandle.longValue() != 0) {
            System.out.println("LogOut Button Action");
            messageTextArea.append(String.format("LogOut Button Action\n"));
 
            // 确保关闭预览
            stopRealPlay();
            realplayButton.setText("预览");
            bRealplayFlags = false;
 
            // 停止订阅事件
            detachIVSEvent();
            attachButton.setText("订阅");
            bAttachFlag = false;
 
            if (netsdkApi.CLIENT_Logout(loginHandle)) {
                System.out.println("Logout Success [ " + address + " ]");
                messageTextArea.append(String.format("Logout Success [%s]\n", address));
                loginHandle.setValue(0);
                loginJPanel.enableCompents(true);
                bTriggerBtnClick = false;
                chnlist.clear();
                chnComboBox.setModel(new DefaultComboBoxModel(chnlist));
            }
        }
    }
 
    /**
     * 开始实时预览按钮事件
     */
    private void startRealPlay() {
        if (loginHandle.longValue() == 0) {
            System.err.println("Please login first");
            JOptionPane.showMessageDialog(mainFrame, "请先登录");
            return;
        }
 
        int channel = chnComboBox.getSelectedIndex(); // 预览通道号
        int playType = NetSDKLib.NET_RealPlayType.NET_RType_Realplay; // 实时预览
 
        playHandle = netsdkApi.CLIENT_RealPlayEx(loginHandle, channel, Native.getComponentPointer(realplayWindow), playType);
        if (playHandle.longValue() == 0) {
            int error = netsdkApi.CLIENT_GetLastError();
            JOptionPane.showMessageDialog(mainFrame, "开始实时预览失败,错误码:" + String.format("[0x%x]", error));
        } else {
            System.out.println("[通道" + channel + "] 拉流成功!");
            messageTextArea.append(String.format("[通道 %2d ] 拉流成功!\n", channel));
        }
    }
 
    /**
     * 结束实时预览按钮事件
     */
    private void stopRealPlay() {
 
        if (playHandle.longValue() == 0) {
            System.out.println("Make sure the realplay Handle is valid");
            messageTextArea.append("Make sure the realplay Handle is valid\n");
            return;
        }
 
        if (netsdkApi.CLIENT_StopRealPlayEx(playHandle)) {
            System.out.println("Success to stop realplay");
            messageTextArea.append("Success to stop realplay\n");
 
            playHandle.setValue(0);
            realplayWindow.repaint();
        }
    }
 
    // 订阅实时上传智能分析数据
    private void attachIVSEvent() {
 
        /**
         * 说明:
         *     通道数可以在有登录是返回的信息 m_stDeviceInfo.byChanNum 获取
         *  下列仅订阅了 0 通道的智能事件.
         */
        int bNeedPicture = 1; // 是否需要图片
        int ChannelId = chnComboBox.getSelectedIndex(); //  通道
 
        attachHandle = netsdkApi.CLIENT_RealLoadPictureEx(loginHandle, ChannelId, NetSDKLib.EVENT_IVS_ALL, bNeedPicture, m_AnalyzerDataCB, null, null);
        if (attachHandle.longValue() != 0) {
            System.out.println("[通道" + ChannelId + "] 订阅成功!");
            messageTextArea.append(String.format("[通道 %2d ] 订阅成功!\n", ChannelId));
 
        } else {
            System.out.printf("CLIENT_RealLoadPictureEx Failed!LastError = %x\n", netsdkApi.CLIENT_GetLastError());
            messageTextArea.append(String.format("CLIENT_RealLoadPictureEx Failed!LastError = %x\n", netsdkApi.CLIENT_GetLastError()));
        }
 
        eventCBQueueService.activeService(); // 启动队列
    }
 
    // 停止上传智能分析数据-图片
    private void detachIVSEvent() {
        if (0 != attachHandle.longValue()) {
            netsdkApi.CLIENT_StopLoadPic(attachHandle);
            System.out.println("Stop detach IVS event");
            messageTextArea.append("Stop detach IVS even\n");
 
            attachHandle.setValue(0);
        }
 
        eventCBQueueService.destroy();  // 关闭队列
    }
 
    /* 智能报警事件回调 */
    public class fAnalyzerDataCB implements NetSDKLib.fAnalyzerDataCallBack/*, StdCallCallback*/ {
        private final String m_imagePath = "./PlateNumber/";
        NetSDKLib.NET_MSG_OBJECT plateObject; // 车牌信息
        NetSDKLib.NET_MSG_OBJECT vehicleObject; // 车辆信息
        NetSDKLib.NET_TIME_EX utc; // 事件时间
 
        int lane = 0; // 车道号
        String EventMsg; // 事件信息
        int nConfide = 0; // 置信度, 只有特定设备才支持,一般设备默认都是0不填充
 
        private String encode;
 
        public fAnalyzerDataCB() {
            File path = new File(m_imagePath);
            if (!path.exists()) {
                path.mkdir();
            }
            initRecord();
 
            initEncodeType();
 
        }
 
        // 解码格式
        private void initEncodeType() {
            String osPrefix = getOsPrefix();
            if (osPrefix.toLowerCase().startsWith("win32-amd64")) {
                encode = "GBK";
            } else if (osPrefix.toLowerCase().startsWith("linux-amd64")) {
                encode = "UTF-8";
            }
        }
 
        // 初始化
        private void initRecord() {
            plateObject = new NET_MSG_OBJECT(); // 车牌信息
            vehicleObject = new NET_MSG_OBJECT(); // 车辆信息
            utc = new NET_TIME_EX(); // 事件时间
            lane = -1; // 车道号
            EventMsg = ""; // 事件信息
            nConfide = 0; // 置信度, 只有特定设备才支持,一般设备默认都是0不填充
        }
 
        // 回调
        public int invoke(LLong lAnalyzerHandle, int dwAlarmType,
                          Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize,
                          Pointer dwUser, int nSequence, Pointer reserved) {
            initRecord(); // init record
            if (lAnalyzerHandle.longValue() == 0) return -1;
            if (pAlarmInfo == null) return 0;
 
            System.out.println("--------dwAlarmType-------------" + dwAlarmType);
            messageTextArea.append("--------------Event Received----------------\n");
            // 获取事件信息
            NetSDKLib.NET_EVENT_FILE_INFO fileInfo = new NetSDKLib.NET_EVENT_FILE_INFO();
 
            switch (dwAlarmType) {
                case NetSDKLib.EVENT_IVS_TRAFFIC_PARKINGSPACEPARKING: /// 车位有车事件
                {
                    messageTextArea.append("<Event> TRAFFIC PARKING SPACE [ PARKING ]\n");
 
                    NetSDKLib.DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO msg = new NetSDKLib.DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);
                    plateObject = msg.stuObject;
                    vehicleObject = msg.stuVehicle;
                    utc = msg.UTC;
                    lane = msg.nLane;
                    fileInfo = msg.stuFileInfo;
 
                    // 抓拍到物体的数据
                    NET_MSG_OBJECT msgObject = msg.stuObject;
                    NET_PIC_INFO msgObjPicInfo = msgObject.stPicInfo;
                    if (msgObjPicInfo != null) {
                        int msgPicOffset = msgObjPicInfo.dwOffSet;
                        int msgPicLength = msgObjPicInfo.dwFileLenth;
                        String msgPicName = String.format("%s_%s_%s.jpg", "Parking", utc.toStringTime().replaceAll("[^0-9]", "-"), "plate");
                        String msgPicPath = m_imagePath + msgPicName;
                        eventCBQueueService.addEvent(new SaveTrafficPicHandler(pBuffer, msgPicOffset, msgPicLength, msgPicPath));
                    }
 
                    // 公共信息图片
                    EVENT_COMM_INFO commInfo = msg.stCommInfo;
                    int nPictureNum = commInfo.nPictureNum;    // 原始图片张数
 
                    for (int i = 0; i < nPictureNum; i++) {
                        int length = commInfo.stuPicInfos[i].nLength;
                        int offSet = commInfo.stuPicInfos[i].nOffset;
                        String fileName = String.format("%s_%s_%02d.jpg", "Parking", utc.toStringTime().replaceAll("[^0-9]", "-"), i);
                        String savePath = m_imagePath + fileName;
                        // ToolKits.savePicture(pBuffer, offSet, length, savePath);
 
                        eventCBQueueService.addEvent(new SaveTrafficPicHandler(pBuffer, offSet, length, savePath));
                    }
 
                    printInfo(fileInfo, msg.stTrafficCar);
 
                    break;
                }
                case NetSDKLib.EVENT_IVS_TRAFFIC_PARKINGSPACENOPARKING: /// 车位无车事件
                {
                    messageTextArea.append("<Event> TRAFFIC PARKING SPACE [ NO PARKING ]\n");
 
                    NetSDKLib.DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO msg = new NetSDKLib.DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO();
                    ToolKits.GetPointerData(pAlarmInfo, msg);
                    plateObject = msg.stuObject;
                    vehicleObject = msg.stuVehicle;
                    utc = msg.UTC;
                    lane = msg.nLane;
                    fileInfo = msg.stuFileInfo;
                    EventMsg = "[ " + utc.toStringTime() + " ] " + "车位无车事件";
 
                    // 抓拍到物体的图片
                    NET_MSG_OBJECT msgObject = msg.stuObject;
                    NET_PIC_INFO msgObjPicInfo = msgObject.stPicInfo;
                    if (msgObjPicInfo != null) {
                        int msgPicOffset = msgObjPicInfo.dwOffSet;
                        int msgPicLength = msgObjPicInfo.dwFileLenth;
                        String msgPicName = String.format("%s_%s_%s.jpg", "NoParking", utc.toStringTime().replaceAll("[^0-9]", "-"), "plate");
                        String msgPicPath = m_imagePath + msgPicName;
                        eventCBQueueService.addEvent(new SaveTrafficPicHandler(pBuffer, msgPicOffset, msgPicLength, msgPicPath));
                    }
                    // 公共图片
                    EVENT_COMM_INFO commInfo = msg.stCommInfo;
                    int nPictureNum = commInfo.nPictureNum;
 
                    for (int i = 0; i < nPictureNum; i++) {
                        int length = commInfo.stuPicInfos[i].nLength;
                        int offSet = commInfo.stuPicInfos[i].nOffset;
                        String fileName = String.format("%s_%s_%02d.jpg", "NoParking", utc.toStringTime().replaceAll("[^0-9]", "-"), i);
                        String savePath = m_imagePath + fileName;
                        // ToolKits.savePicture(pBuffer, offSet, length, savePath);
 
                        eventCBQueueService.addEvent(new SaveTrafficPicHandler(pBuffer, offSet, length, savePath));
                    }
 
                    printInfo(fileInfo, msg.stTrafficCar);
 
                    break;
                }
                default:
                    System.out.printf("Get Event 0x%x\n", dwAlarmType);
                    EventMsg = "未处理事件 dwAlarmType = " + String.format("0x%x", dwAlarmType);
                    break;
            }
            return 0;
        }
 
        private void printInfo(NetSDKLib.NET_EVENT_FILE_INFO fileInfo, NetSDKLib.DEV_EVENT_TRAFFIC_TRAFFICCAR_INFO stTrafficCar) {
            // Todo 展示 EVENT_FILE_INFO 的内容
 
            String message = null;
            try {
                message = "SnapShotTime: " + stTrafficCar.stSnapTime.toStringTime()
                        + "\nParkingPlaceNo:" + new String(stTrafficCar.szCustomParkNo, encode).trim()
                        + "\nVehicleDirection: " + stTrafficCar.byDirection
                        + "\nVehicleSize: " + stTrafficCar.nVehicleSize
                        + "\nszPlateColor: " + new String(stTrafficCar.szPlateColor).trim()
                        + "\nszVehicleColor: " + new String(stTrafficCar.szVehicleColor).trim();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            System.out.println(message);
            messageTextArea.append(message + "\n");
        }
    }
 
    ///////////////////////////////////// 异步图片存储队列 ////////////////////////////////////
 
    private QueueGeneration eventCBQueueService = new QueueGeneration();
 
    private class SaveTrafficPicHandler implements EventTaskHandler {
        private static final long serialVersionUID = 1L;
 
        private final byte[] imgBuffer;
        private final int length;
        private final String savePath;
 
        public SaveTrafficPicHandler(Pointer pBuf, int dwBufOffset, int dwBufSize, String sDstFile) {
 
            this.imgBuffer = pBuf.getByteArray(dwBufOffset, dwBufSize);
            this.length = dwBufSize;
            this.savePath = sDstFile;
        }
 
        @Override
        public void eventCallBackProcess() {
 
            System.out.println("保存图片中...路径:" + savePath);
            messageTextArea.append("保存图片中...路径:" + savePath + "\n");
 
            try {
                DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)));
                out.write(imgBuffer, 0, length);
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    /////////////////////////////////界面组件/////////////////////////////////////////////////
    ///////////////////////////// 界面组件相关成员变量  ////////////////////////////////////////
 
    /**
     * 设置边框
     */
    private void setBorderEx(JComponent object, String title, int width) {
        Border innerBorder = BorderFactory.createTitledBorder(title);
        Border outerBorder = BorderFactory.createEmptyBorder(width, width, width, width);
        object.setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));
    }
 
    /**
     * 登录面板
     */
    private class LoginPanel extends JPanel {
        private static final long serialVersionUID = 1L;
 
        public LoginPanel() {
            loginBtn = new JButton("登入");
            logoutBtn = new JButton("登出");
            nameLabel = new JLabel("用户名");
            passwordLabel = new JLabel("密码");
            nameTextArea = new JTextField(username, 8);
            passwordTextArea = new JPasswordField(password, 8);
            ipLabel = new JLabel("设备地址");
            portLabel = new JLabel("端口号");
            ipTextArea = new JTextField(address, 15);
            portTextArea = new JTextField("37777", 6);
 
            setLayout(new FlowLayout());
            setBorderEx(this, "登录", 2);
 
            add(ipLabel);
            add(ipTextArea);
            add(portLabel);
            add(portTextArea);
            add(nameLabel);
            add(nameTextArea);
            add(passwordLabel);
            add(passwordTextArea);
            add(loginBtn);
            add(logoutBtn);
 
            enableCompents(false);
 
            // 登录按钮. 监听事件
            loginBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    loginBtnPerformed(e);
                }
            });
 
            // 登出按钮. 监听事件
            logoutBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    logoutBtnPerformed(e);
                    enableCompents(false);
                }
            });
        }
 
        public void enableCompents(boolean enable) {
            loginBtn.setEnabled(!enable);
            logoutBtn.setEnabled(enable);
        }
 
        private JButton loginBtn;
        private JButton logoutBtn;
        private JLabel nameLabel;
        private JLabel passwordLabel;
        private JLabel ipLabel;
        private JLabel portLabel;
    }
 
    /**
     * 事件信息显示面板
     */
    private class MessagePanel extends JPanel {
 
        private static final long serialVersionUID = 1L;
 
        public MessagePanel() {
            setBorderEx(this, "事件信息提示", 2);
 
            Dimension dim = getPreferredSize();
            dim.height = 226;
            setPreferredSize(dim);
            setLayout(new BorderLayout());
 
            messageTextArea = new JTextArea();
 
            add(new JScrollPane(messageTextArea), BorderLayout.CENTER);
        }
    }
 
    /**
     * 预览面板
     */
    private class RealPlayPanel extends JPanel {
 
        private static final long serialVersionUID = 1L;
 
        public RealPlayPanel() {
            setBorderEx(this, "实时预览", 2);
            setLayout(new BorderLayout());
            Dimension dim = getPreferredSize();
            dim.height = 367;
            dim.width = 480;
            setPreferredSize(dim);
 
            realplayWindow = new Panel();
            chnComboBox = new JComboBox(chnlist);
            attachButton = new JButton("订阅");
            realplayButton = new JButton("预览");
 
            realplayWindow.setBackground(new java.awt.Color(153, 240, 255));
            realplayWindow.setForeground(new java.awt.Color(0, 0, 0));
            realplayWindow.setBounds(5, 5, 350, 290);
            realplayWindow.setSize(358, 294);
 
            JPanel btnPanel = new JPanel();
            btnPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
            btnPanel.setLayout(new GridLayout(1, 3, 5, 5));
            btnPanel.add(chnComboBox);
            btnPanel.add(attachButton);
            btnPanel.add(realplayButton);
 
            add(realplayWindow, BorderLayout.CENTER);
            add(btnPanel, BorderLayout.SOUTH);
 
            // 订阅按钮动作监听
            attachButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (!bAttachFlag) {
                        attachIVSEvent();
                        attachButton.setText("停止订阅");
                        bAttachFlag = true;
                    } else {
                        detachIVSEvent();
                        attachButton.setText("订阅");
                        bAttachFlag = false;
                    }
                }
            });
 
            // 预览按钮监听
            realplayButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (!bRealplayFlags) {
                        startRealPlay();
                        realplayButton.setText("停止预览");
                        bRealplayFlags = true;
                    } else {
                        stopRealPlay();
                        realplayButton.setText("预览");
                        bRealplayFlags = false;
                    }
                }
            });
        }
    }
 
    /**
     * 主界面组件
     */
    private JFrame mainFrame;
 
    /**
     * 登录条组件
     */
    private JTextField nameTextArea;
    private JPasswordField passwordTextArea;
 
    private JTextField ipTextArea;
    private JTextField portTextArea;
 
    private LoginPanel loginJPanel;
 
    /**
     * 事件信息显示组件
     */
    private MessagePanel messagePanel;
    private JTextArea messageTextArea;
 
    /**
     * 实时预览组件
     */
    private RealPlayPanel realPlayPanel;
    private java.awt.Panel realplayWindow;
    private JButton attachButton;
    private JButton realplayButton;
    private JComboBox chnComboBox;
}
 
public class TrafficParkingEvent {
 
    public static void main(String[] args) throws SecurityException, IllegalArgumentException {
 
        ITSParkingEventMsg demo = new ITSParkingEventMsg();
    }
}