dongyukun
8 天以前 a23e66f179586eeffc55d116f5a3bb53eab9cb09
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.demo.frame.Gate;
H 2
3 import java.awt.AWTEvent;
4 import java.awt.BorderLayout;
5 import java.awt.Dimension;
6 import java.awt.EventQueue;
7 import java.awt.FlowLayout;
8 import java.awt.Toolkit;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.awt.event.WindowAdapter;
12 import java.awt.event.WindowEvent;
13 import java.awt.image.BufferedImage;
14 import java.io.ByteArrayInputStream;
15 import java.io.File;
16 import java.io.IOException;
17 import java.io.UnsupportedEncodingException;
18 import java.util.Vector;
19
20 import javax.imageio.ImageIO;
21 import javax.swing.BorderFactory;
22 import javax.swing.DefaultComboBoxModel;
23 import javax.swing.JButton;
24 import javax.swing.JComboBox;
25 import javax.swing.JFrame;
26 import javax.swing.JLabel;
27 import javax.swing.JOptionPane;
28 import javax.swing.JPanel;
29 import javax.swing.JTextField;
30 import javax.swing.SwingUtilities;
31 import javax.swing.UIManager;
32
33 import com.iailab.netsdk.common.BorderEx;
34 import com.iailab.netsdk.common.FunctionList;
35 import com.iailab.netsdk.common.LoginPanel;
36 import com.iailab.netsdk.common.PaintPanel;
37 import com.iailab.netsdk.common.Res;
38 import com.iailab.netsdk.demo.module.GateModule;
39 import com.iailab.netsdk.demo.module.LoginModule;
40 import com.iailab.netsdk.lib.NetSDKLib;
41 import com.iailab.netsdk.lib.NetSDKLib.*;
42 import com.iailab.netsdk.lib.ToolKits;
43
44 import com.sun.jna.Pointer;
45
46 class GateFrame extends JFrame {
47     private static final long serialVersionUID = 1L;
48     
49     // 获取界面窗口
50     private static JFrame frame = new JFrame();
51     
52     // 设备断线通知回调
53     private static DisConnect disConnect       = new DisConnect(); 
54     
55     // 网络连接恢复
56     private static HaveReConnect haveReConnect = new HaveReConnect(); 
57     
58     // 订阅句柄
59     public static LLong m_hAttachHandle = new LLong(0);
60     
61     private Vector<String> chnList = new Vector<String>(); 
62     
63     private AnalyzerDataCB analyzerCallback = new AnalyzerDataCB();
64
65     private java.awt.Component target = this;
66     
67     private boolean isAttach = false;
68     
69     public GateFrame() {
70         setTitle(Res.string().getGate());
71         setLayout(new BorderLayout());
72         pack();
73         setSize(800, 400);
74         setResizable(false);
75         setLocationRelativeTo(null);
76         LoginModule.init(disConnect, haveReConnect);   // 打开工程,初始化
77         
78         try {
79             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
80         } catch (Exception e) {
81             e.printStackTrace();
82         } 
83      
84         loginPanel = new LoginPanel();
85         GatePanel gatePanel = new GatePanel();
86     
87
88         add(loginPanel, BorderLayout.NORTH);
89         add(gatePanel, BorderLayout.CENTER);
90
91         
92         loginPanel.addLoginBtnActionListener(new ActionListener() {
93             @Override
94             public void actionPerformed(ActionEvent e) {    
95                 if(loginPanel.checkLoginText()) {
96                     if(login()) {
97                         frame = ToolKits.getFrame(e);
98                         frame.setTitle(Res.string().getGate() + " : " + Res.string().getOnline());
99                     }
100                 }    
101             }
102         });
103         
104         loginPanel.addLogoutBtnActionListener(new ActionListener() {        
105             @Override
106             public void actionPerformed(ActionEvent e) {
107                 frame.setTitle(Res.string().getGate());
108                 logout();                
109             }
110         });
111         
112         addWindowListener(new WindowAdapter() {
113             public void windowClosing(WindowEvent e) {
114                 GateModule.stopRealLoadPic(m_hAttachHandle);
115                 LoginModule.logout();
116                 LoginModule.cleanup();   // 关闭工程,释放资源
117                     
118                 dispose();
119
120                 SwingUtilities.invokeLater(new Runnable() {
121                     public void run() {
122                         FunctionList demo = new FunctionList();
123                         demo.setVisible(true);
124                     }
125                 });
126             }
127         });
128     }
129     
130     /////////////////面板///////////////////
131     // 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
132     private static class DisConnect implements NetSDKLib.fDisConnect {
133         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
134             System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
135             // 断线提示
136             SwingUtilities.invokeLater(new Runnable() {
137                 public void run() {
138                     frame.setTitle(Res.string().getGate() + " : " + Res.string().getDisConnectReconnecting());
139                 }
140             });
141         }
142     }
143     
144     // 网络连接恢复,设备重连成功回调
145     // 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数
146     private static class HaveReConnect implements NetSDKLib.fHaveReConnect {
147         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
148             System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
149             
150             // 重连提示
151             SwingUtilities.invokeLater(new Runnable() {
152                 public void run() {
153                     frame.setTitle(Res.string().getGate() + " : " + Res.string().getOnline());
154                 }
155             });
156         }
157     }
158     
159     // 登录
160     public boolean login() {
161         if(LoginModule.login(loginPanel.ipTextArea.getText(), 
162                         Integer.parseInt(loginPanel.portTextArea.getText()), 
163                         loginPanel.nameTextArea.getText(), 
164                         new String(loginPanel.passwordTextArea.getPassword()))) {    
165             
166             for(int i = 1; i < LoginModule.m_stDeviceInfo.byChanNum + 1; i++) {
167                 chnList.add(Res.string().getChannel() + " " + String.valueOf(i));
168             }
169             
170             // 登陆成功,将通道添加到控件
171             chnComboBox.setModel(new DefaultComboBoxModel(chnList));
172             
173             loginPanel.setButtonEnable(true);
174             setEnable(true);
175             
176         } else {
177             JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
178             return false;
179         }
180         return true;
181     }
182     
183     // 登出
184     public void logout() {
185         GateModule.stopRealLoadPic(m_hAttachHandle);
186         LoginModule.logout();
187
188         loginPanel.setButtonEnable(false);
189         
190         for(int i = 0; i < LoginModule.m_stDeviceInfo.byChanNum; i++) {
191             chnList.clear();
192         }
193         
194         chnComboBox.setModel(new DefaultComboBoxModel());    
195         setEnable(false);
196         detachBtn.setEnabled(false);
197         
198         isAttach = false;
199         
200         clearPanel();
201     }
202     
203     /**
204      * 闸机界面面板
205      */
206     private class GatePanel extends JPanel {
207
208         /**
209          * 
210          */
211         private static final long serialVersionUID = 1L;
212         
213         public GatePanel() {
214             BorderEx.set(this, "", 4);
215             setLayout(new BorderLayout());
216             
217             JPanel gateOperatePanel = new JPanel();
218             JPanel gateShowPanel = new JPanel();
219             
220             add(gateOperatePanel, BorderLayout.WEST);
221             add(gateShowPanel, BorderLayout.CENTER);
222             
223             /**
224              *  闸机操作面板
225              */
226             gateOperatePanel.setLayout(new BorderLayout());
227             gateOperatePanel.setPreferredSize(new Dimension(250, 70));
228             
229             JPanel channelPanel = new JPanel();
230             JPanel operatePanel = new JPanel();        
231             gateOperatePanel.add(channelPanel, BorderLayout.NORTH);
232             gateOperatePanel.add(operatePanel, BorderLayout.CENTER);
233             
234             // 通道面板
235             channelPanel.setBorder(BorderFactory.createTitledBorder(""));
236             channelPanel.setPreferredSize(new Dimension(220, 70));
237             channelPanel.setLayout(new FlowLayout());
238             
239             JLabel channelLabel = new JLabel(Res.string().getChannel());
240             chnComboBox = new JComboBox(); 
241             
242             chnComboBox.setPreferredSize(new Dimension(100, 20));
243             
244             channelPanel.add(channelLabel);
245             channelPanel.add(chnComboBox);
246             
247             // 按钮面板
248             operatePanel.setBorder(BorderFactory.createTitledBorder(Res.string().getOperate()));
249             operatePanel.setLayout(new FlowLayout());
250             
251             attachBtn = new JButton(Res.string().getAttach());
252             detachBtn = new JButton(Res.string().getDetach());
253             cardOperateBtn = new JButton(Res.string().getCardOperate());
254             JLabel nullJLabel = new JLabel("");
255             
256             nullJLabel.setPreferredSize(new Dimension(205, 40));
257             attachBtn.setPreferredSize(new Dimension(100, 20));
258             detachBtn.setPreferredSize(new Dimension(100, 20));
259             cardOperateBtn.setPreferredSize(new Dimension(205, 20));
260
261             operatePanel.add(attachBtn);
262             operatePanel.add(detachBtn);
263             operatePanel.add(nullJLabel);
264             operatePanel.add(cardOperateBtn);
265             
266             setEnable(false);
267             detachBtn.setEnabled(false);
268             
269             /**
270              * 闸机订阅展示面板
271              */
272             gateShowPanel.setBorder(BorderFactory.createTitledBorder(""));
273             gateShowPanel.setLayout(new BorderLayout());
274             
275             personPaintPanel = new PaintPanel();
276             JPanel cardInfoPanel = new JPanel();
277             
278             personPaintPanel.setPreferredSize(new Dimension(250, 70));            
279             
280             gateShowPanel.add(personPaintPanel, BorderLayout.WEST);
281             gateShowPanel.add(cardInfoPanel, BorderLayout.CENTER);
282             
283             //
284             cardInfoPanel.setLayout(new FlowLayout());
285             
286             JLabel timeLable = new JLabel(Res.string().getTime() + ":", JLabel.CENTER);
287             JLabel openStatusLable = new JLabel(Res.string().getOpenStatus() + ":", JLabel.CENTER);
288             JLabel openMethodLable = new JLabel(Res.string().getOpenMethod() + ":", JLabel.CENTER);
289             JLabel cardNameLable = new JLabel(Res.string().getCardName() + ":", JLabel.CENTER);
290             JLabel cardNoLable = new JLabel(Res.string().getCardNo() + ":", JLabel.CENTER);
291             JLabel userIdLable = new JLabel(Res.string().getUserId() + ":", JLabel.CENTER);
292             JLabel tempLable = new JLabel(Res.string().getTemp() + ":", JLabel.CENTER);
293             JLabel maskstutasLable = new JLabel(Res.string().getMaskstutas() + ":", JLabel.CENTER);
294             
295             
296             
297             timeLable.setPreferredSize(new Dimension(80, 20));
298             openStatusLable.setPreferredSize(new Dimension(80, 20));
299             openMethodLable.setPreferredSize(new Dimension(80, 20));
300             cardNameLable.setPreferredSize(new Dimension(80, 20));
301             cardNoLable.setPreferredSize(new Dimension(80, 20));
302             userIdLable.setPreferredSize(new Dimension(80, 20));
303             tempLable.setPreferredSize(new Dimension(80, 20));
304             maskstutasLable.setPreferredSize(new Dimension(80, 20));
305             
306             
307             timeTextField = new JTextField("");
308             openStatusTextField = new JTextField("");
309             openMethodTextField = new JTextField("");
310             cardNameTextField = new JTextField("");
311             cardNoTextField = new JTextField("");
312             userIdTextField = new JTextField("");
313             tempTextField = new JTextField("");
314             maskStatusTextField = new JTextField("");
315             
316             
317             Dimension dimension = new Dimension();
318             dimension.width = 150;
319             dimension.height = 20;
320             timeTextField.setPreferredSize(dimension);
321             openStatusTextField.setPreferredSize(dimension);
322             openMethodTextField.setPreferredSize(dimension);
323             cardNameTextField.setPreferredSize(dimension);
324             cardNoTextField.setPreferredSize(dimension);
325             userIdTextField.setPreferredSize(dimension);
326             tempTextField.setPreferredSize(dimension);
327             maskStatusTextField.setPreferredSize(dimension);            
328             
329             
330             timeTextField.setHorizontalAlignment(JTextField.CENTER);
331             openStatusTextField.setHorizontalAlignment(JTextField.CENTER);
332             openMethodTextField.setHorizontalAlignment(JTextField.CENTER);
333             cardNameTextField.setHorizontalAlignment(JTextField.CENTER);
334             cardNoTextField.setHorizontalAlignment(JTextField.CENTER);
335             userIdTextField.setHorizontalAlignment(JTextField.CENTER);
336             tempTextField.setHorizontalAlignment(JTextField.CENTER);
337             maskStatusTextField.setHorizontalAlignment(JTextField.CENTER);            
338             
339             
340             timeTextField.setEditable(false);
341             openStatusTextField.setEditable(false);
342             openMethodTextField.setEditable(false);
343             cardNameTextField.setEditable(false);
344             cardNoTextField.setEditable(false);
345             userIdTextField.setEditable(false);
346             tempTextField.setEditable(false);
347             maskStatusTextField.setEditable(false);            
348             
349             
350             cardInfoPanel.add(timeLable);
351             cardInfoPanel.add(timeTextField);
352             cardInfoPanel.add(openStatusLable);
353             cardInfoPanel.add(openStatusTextField);
354             cardInfoPanel.add(openMethodLable);
355             cardInfoPanel.add(openMethodTextField);
356             cardInfoPanel.add(cardNameLable);
357             cardInfoPanel.add(cardNameTextField);
358             cardInfoPanel.add(cardNoLable);
359             cardInfoPanel.add(cardNoTextField);
360             cardInfoPanel.add(userIdLable);
361             cardInfoPanel.add(userIdTextField);
362             cardInfoPanel.add(tempLable);
363             cardInfoPanel.add(tempTextField);
364             cardInfoPanel.add(maskstutasLable);
365             cardInfoPanel.add(maskStatusTextField);
366             
367             setOnClickListener();
368         }
369     }
370     
371     // 监听
372     private void setOnClickListener() {
373         // 订阅
374         attachBtn.addActionListener(new ActionListener() {        
375             @Override
376             public void actionPerformed(ActionEvent arg0) {
377                 m_hAttachHandle = GateModule.realLoadPic(chnComboBox.getSelectedIndex(), analyzerCallback);
378                 if(m_hAttachHandle.longValue() != 0) {
379                     isAttach = true;
380                     attachBtn.setEnabled(false);
381                     detachBtn.setEnabled(true);
382                 } else {
383                     JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
384                 }
385             }
386         });
387         
388         // 取消订阅
389         detachBtn.addActionListener(new ActionListener() {        
390             @Override
391             public void actionPerformed(ActionEvent arg0) {
392                 GateModule.stopRealLoadPic(m_hAttachHandle);
393                 synchronized (this) {
394                     isAttach = false;
395                 }
396                 attachBtn.setEnabled(true);
397                 detachBtn.setEnabled(false);
398                 
399                 clearPanel();
400             }
401         });
402         
403         
404         // 卡操作
405         cardOperateBtn.addActionListener(new ActionListener() {        
406             @Override
407             public void actionPerformed(ActionEvent arg0) {        
408                 CardManegerDialog dialog = new CardManegerDialog();
409                 dialog.setVisible(true);
410             }
411         });
412     }
413     
414     private void setEnable(boolean bln) {
415         chnComboBox.setEnabled(bln);
416         attachBtn.setEnabled(bln);
417         cardOperateBtn.setEnabled(bln);
418     }
419     
420     private void clearPanel() {
421           personPaintPanel.setOpaque(true); 
422         personPaintPanel.repaint();
423         
424         timeTextField.setText("");
425         openStatusTextField.setText("");
426         openMethodTextField.setText("");
427         cardNameTextField.setText("");
428         cardNoTextField.setText("");
429         userIdTextField.setText("");
430         tempTextField.setText("");
431         maskStatusTextField.setText("");
432     }
433     
434     private class AnalyzerDataCB implements NetSDKLib.fAnalyzerDataCallBack {  
435         private BufferedImage gateBufferedImage = null;
436         
437         public int invoke(LLong lAnalyzerHandle, int dwAlarmType,
438                          Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize,
439                          Pointer dwUser, int nSequence, Pointer reserved) 
440         {
441             if (lAnalyzerHandle.longValue() == 0 || pAlarmInfo == null) {
442                 return -1;
443             }   
444             
445             File path = new File("./GateSnapPicture/");
446             if (!path.exists()) {
447                 path.mkdir();
448             }
449             
450             ///< 门禁事件
451             if(dwAlarmType == NetSDKLib.EVENT_IVS_ACCESS_CTL) {
452                 DEV_EVENT_ACCESS_CTL_INFO msg = new DEV_EVENT_ACCESS_CTL_INFO();
453                 ToolKits.GetPointerData(pAlarmInfo, msg);  
454             
455                 // 保存图片,获取图片缓存
456                 String snapPicPath = path + "\\" + System.currentTimeMillis() + "GateSnapPicture.jpg";  // 保存图片地址
457                 byte[] buffer = pBuffer.getByteArray(0, dwBufSize);
458                 ByteArrayInputStream byteArrInputGlobal = new ByteArrayInputStream(buffer);
459                 
460                 try {
461                     gateBufferedImage = ImageIO.read(byteArrInputGlobal);
462                     if(gateBufferedImage != null) {
463                         ImageIO.write(gateBufferedImage, "jpg", new File(snapPicPath));
464                     }                        
465                 } catch (IOException e2) {
466                     e2.printStackTrace();
467                 }
468                 
469                  // 图片以及门禁信息界面显示                                          
470                  EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
471                  if (eventQueue != null) {
472                  eventQueue.postEvent( new AccessEvent(target,
473                                                        gateBufferedImage, 
474                                                       msg));
475                  }   
476             }
477                    
478             return 0;           
479         }
480     }
481     
482     class AccessEvent extends AWTEvent {
483         /**
484          * 
485          */
486         private static final long serialVersionUID = 1L;
487         public static final int EVENT_ID = AWTEvent.RESERVED_ID_MAX + 1;
488         
489         private BufferedImage gateBufferedImage = null;
490         private DEV_EVENT_ACCESS_CTL_INFO msg = null;
491         
492         public AccessEvent(Object target,
493                             BufferedImage gateBufferedImage, 
494                            DEV_EVENT_ACCESS_CTL_INFO msg) {
495             super(target, EVENT_ID);
496             this.gateBufferedImage = gateBufferedImage;
497             this.msg = msg;
498         }
499         
500         public BufferedImage getGateBufferedImage() {
501             return gateBufferedImage;
502         }
503         
504         public DEV_EVENT_ACCESS_CTL_INFO getAccessInfo() {
505             return msg;
506         }
507     }
508     
509     @Override
510     protected void processEvent(AWTEvent event) {
511         if (event instanceof AccessEvent) {  // 门禁事件处理
512             AccessEvent ev = (AccessEvent) event;
513
514             BufferedImage gateBufferedImage = ev.getGateBufferedImage();        
515             DEV_EVENT_ACCESS_CTL_INFO msg = ev.getAccessInfo();
516
517             if(!isAttach) {
518                  return;
519             }
520              
521             // 图片显示
522             if(gateBufferedImage != null) {
523                 personPaintPanel.setImage(gateBufferedImage);
524                 personPaintPanel.setOpaque(false); 
525                 personPaintPanel.repaint();
526             } else {
527                 personPaintPanel.setOpaque(true); 
528                 personPaintPanel.repaint();
529             }
530             
531               // 时间
532             if(msg.UTC == null || msg.UTC.toString().isEmpty()) {
533                 timeTextField.setText("");
534             } else {
535                 msg.UTC.setTime(msg.UTC.dwYear, msg.UTC.dwMonth, msg.UTC.dwDay, msg.UTC.dwHour+8, msg.UTC.dwMinute, msg.UTC.dwSecond);
536                 timeTextField.setText(msg.UTC.toString());
537             }
538             
539             // 开门状态
540             if(msg.bStatus == 1) {
541                 openStatusTextField.setText(Res.string().getSucceed());
542             } else {
543                 openStatusTextField.setText(Res.string().getFailed());
544             }        
545             
546             // 开门方式
547             openMethodTextField.setText(Res.string().getOpenMethods(msg.emOpenMethod));
548             
549             // 卡名
550             try {
551                 cardNameTextField.setText(new String(msg.szCardName, "GBK").trim());
552             } catch (UnsupportedEncodingException e) {
553                 e.printStackTrace();
554             }
555             
556             // 卡号
557             cardNoTextField.setText(new String(msg.szCardNo).trim());
558             
559             // 用户ID
560             userIdTextField.setText(new String(msg.szUserID).trim());
561             
562             // 口罩状态
563             maskStatusTextField.setText(Res.string().getMaskStatus(msg.emMask)); 
564             
565             //温度
566             if(msg.stuManTemperatureInfo.emTemperatureUnit==0) {
567                 tempTextField.setText(String.valueOf(msg.stuManTemperatureInfo.fCurrentTemperature+"℃"));
568             }else if(msg.stuManTemperatureInfo.emTemperatureUnit==1){
569                 tempTextField.setText(String.valueOf(msg.stuManTemperatureInfo.fCurrentTemperature+"℉"));
570             }else if(msg.stuManTemperatureInfo.emTemperatureUnit==2) {
571                 tempTextField.setText(String.valueOf(msg.stuManTemperatureInfo.fCurrentTemperature+"K"));
572             }
573             
574             
575         } else {
576             super.processEvent(event);   
577         }
578     } 
579     
580     /*
581      * 登录控件
582      */
583     private LoginPanel loginPanel;    
584     
585     private JComboBox chnComboBox;
586     private JButton attachBtn;
587     private JButton detachBtn;
588     private JButton cardOperateBtn;
589     
590     private PaintPanel personPaintPanel;
591     
592     private JTextField timeTextField;
593     private JTextField openStatusTextField;
594     private JTextField openMethodTextField;
595     private JTextField cardNameTextField;
596     private JTextField cardNoTextField;
597     private JTextField userIdTextField;
598     private JTextField tempTextField;
599     private JTextField maskStatusTextField;
600 }
601
602 public class Gate {
603     public static void main(String[] args) {    
604         SwingUtilities.invokeLater(new Runnable() {
605             public void run() {
606                 GateFrame demo = new GateFrame();    
607                 demo.setVisible(true);
608             }
609         });        
610     }
611 }