dongyukun
2024-11-05 e8ad669f7c97d45cd23630dc101180a130d6c17e
提交 | 用户 | 时间
ce910c 1 package com.netsdk.demo.frame;
H 2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.GridLayout;
8 import java.awt.Panel;
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.util.Vector;
14
15 import javax.swing.DefaultComboBoxModel;
16 import javax.swing.JButton;
17 import javax.swing.JComboBox;
18 import javax.swing.JFrame;
19 import javax.swing.JLabel;
20 import javax.swing.JOptionPane;
21 import javax.swing.JPanel;
22 import javax.swing.SwingUtilities;
23 import javax.swing.UIManager;
24 import javax.swing.border.EmptyBorder;
25
26 import com.sun.jna.Pointer;
27
28 import com.netsdk.common.*;
29 import com.netsdk.demo.module.*;
30 import com.netsdk.lib.NetSDKLib;
31 import com.netsdk.lib.NetSDKLib.LLong;
32 import com.netsdk.lib.ToolKits;
33
34 class RealPlayFrame extends JFrame{
35     private static final long serialVersionUID = 1L;
36     
37     private Vector<String> chnlist = new Vector<String>(); 
38
39     private boolean isrealplayOne = false;
40     private boolean isrealplayTwo = false;
41
42     // 设备断线通知回调
43     private static DisConnect disConnect       = new DisConnect(); 
44     
45     // 网络连接恢复
46     private static HaveReConnect haveReConnect = new HaveReConnect(); 
47     
48     // 预览句柄
49     public static LLong m_hPlayHandleOne = new LLong(0);
50     
51     public static LLong m_hPlayHandleTwo = new LLong(0);
52     
53     // 获取界面窗口
54     private static JFrame frame = new JFrame();   
55     
56     public RealPlayFrame() {
57         setTitle(Res.string().getRealplay());
58         setLayout(new BorderLayout());
59         pack();
60         setSize(800, 560);
61         setResizable(false);
62         setLocationRelativeTo(null);
63         LoginModule.init(disConnect, haveReConnect);   // 打开工程,初始化
64         
65         try {
66             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
67         } catch (Exception e) {
68             e.printStackTrace();
69         } 
70      
71         loginPanel = new LoginPanel();
72         realPanel = new JPanel();
73         
74         add(loginPanel, BorderLayout.NORTH);
75         add(realPanel, BorderLayout.CENTER);
76         
77         // 预览面板
78         realPanelOne = new RealPanelOne();
79         realPanelTwo = new RealPanelTwo();
80         
81         realPanel.setLayout(new GridLayout(1,  2));
82         realPanel.add(realPanelOne);
83         realPanel.add(realPanelTwo);
84         
85         loginPanel.addLoginBtnActionListener(new ActionListener() {
86             @Override
87             public void actionPerformed(ActionEvent e) {               
88                 if(loginPanel.checkLoginText()) {
89                     if(login()) {
90                         frame = ToolKits.getFrame(e);
91                         frame.setTitle(Res.string().getRealplay() + " : " + Res.string().getOnline());
92                     }
93                 }
94             }
95         });
96         
97         loginPanel.addLogoutBtnActionListener(new ActionListener() {        
98             @Override
99             public void actionPerformed(ActionEvent e) {
100                 frame.setTitle(Res.string().getRealplay());
101                 logout();    
102             }
103         });
104         
105         addWindowListener(new WindowAdapter() {
106             public void windowClosing(WindowEvent e) {
107                 RealPlayModule.stopRealPlay(m_hPlayHandleOne);
108                 RealPlayModule.stopRealPlay(m_hPlayHandleTwo);
109                 LoginModule.logout();
110                 LoginModule.cleanup();   // 关闭工程,释放资源
111                 dispose();    
112                 
113                 SwingUtilities.invokeLater(new Runnable() {
114                     public void run() {
115                         FunctionList demo = new FunctionList();
116                         demo.setVisible(true);
117                     }
118                 });
119             }
120         });
121     }
122     
123     /////////////////面板///////////////////
124     // 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
125     private static class DisConnect implements NetSDKLib.fDisConnect {
126         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
127             System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
128             // 断线提示
129             SwingUtilities.invokeLater(new Runnable() {
130                 public void run() {
131                     frame.setTitle(Res.string().getRealplay() + " : " + Res.string().getDisConnectReconnecting());
132                 }
133             });
134         }
135     }
136     
137     // 网络连接恢复,设备重连成功回调
138     // 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数
139     private static class HaveReConnect implements NetSDKLib.fHaveReConnect {
140         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
141             System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
142             
143             // 重连提示
144             SwingUtilities.invokeLater(new Runnable() {
145                 public void run() {
146                     frame.setTitle(Res.string().getRealplay() + " : " + Res.string().getOnline());
147                 }
148             });
149         }
150     }
151     
152     // 登录
153     public boolean login() {
154         if(LoginModule.login(loginPanel.ipTextArea.getText(), 
155                         Integer.parseInt(loginPanel.portTextArea.getText()), 
156                         loginPanel.nameTextArea.getText(), 
157                         new String(loginPanel.passwordTextArea.getPassword()))) {
158     
159             loginPanel.setButtonEnable(true);
160             setButtonEnable(true);
161             
162             for(int i = 1; i < LoginModule.m_stDeviceInfo.byChanNum + 1; i++) {
163                 chnlist.add(Res.string().getChannel() + " " + String.valueOf(i));
164             }
165             
166             // 登陆成功,将通道添加到控件
167             chnComboBoxOne.setModel(new DefaultComboBoxModel(chnlist));
168             chnComboBoxTwo.setModel(new DefaultComboBoxModel(chnlist));
169             
170         } else {
171             JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
172             return false;
173         }
174         return true;
175     }
176     
177     // 登出
178     public void logout() {
179         RealPlayModule.stopRealPlay(m_hPlayHandleOne);
180         RealPlayModule.stopRealPlay(m_hPlayHandleTwo);
181         LoginModule.logout();
182         
183         loginPanel.setButtonEnable(false);
184         setButtonEnable(false);
185         realPlayWindowOne.repaint();    
186         realPlayWindowTwo.repaint();   
187
188         isrealplayOne = false;
189         realplayBtnOne.setText(Res.string().getStartRealPlay());
190         
191         isrealplayTwo = false;
192         realplayBtnTwo.setText(Res.string().getStartRealPlay());
193         
194         for(int i = 0; i < LoginModule.m_stDeviceInfo.byChanNum; i++) {
195             chnlist.clear();
196         }
197         
198         chnComboBoxOne.setModel(new DefaultComboBoxModel());
199         chnComboBoxTwo.setModel(new DefaultComboBoxModel());
200     }
201     
202     /*
203      * 预览界面通道、码流设置  以及抓图面板
204      */
205     private class RealPanelOne extends JPanel {
206         private static final long serialVersionUID = 1L;
207         
208         public RealPanelOne() {
209             BorderEx.set(this, Res.string().getRealplay(), 2);
210             setLayout(new BorderLayout());
211             
212             channelPanelOne = new Panel();
213             realplayPanelOne = new JPanel();
214             
215             add(channelPanelOne, BorderLayout.NORTH);
216             add(realplayPanelOne, BorderLayout.CENTER);
217             
218             /************ 预览面板 **************/
219             realplayPanelOne.setLayout(new BorderLayout());
220             realplayPanelOne.setBorder(new EmptyBorder(5, 5, 5, 5));
221             realPlayWindowOne = new Panel();
222             realPlayWindowOne.setBackground(Color.GRAY);
223             realplayPanelOne.add(realPlayWindowOne, BorderLayout.CENTER);
224             
225             /************ 通道、码流面板 **************/
226             chnlabelOne = new JLabel(Res.string().getChn());
227             chnComboBoxOne = new JComboBox();                 
228
229             streamLabelOne = new JLabel(Res.string().getStreamType());
230             String[] stream = {Res.string().getMasterStream(), Res.string().getSubStream()};
231             streamComboBoxOne = new JComboBox(stream);     
232             
233             realplayBtnOne = new JButton(Res.string().getStartRealPlay());
234             
235             channelPanelOne.setLayout(new FlowLayout());            
236             channelPanelOne.add(chnlabelOne);
237             channelPanelOne.add(chnComboBoxOne);
238             channelPanelOne.add(streamLabelOne);
239             channelPanelOne.add(streamComboBoxOne);
240             channelPanelOne.add(realplayBtnOne);
241             
242             chnComboBoxOne.setPreferredSize(new Dimension(80, 20)); 
243             streamComboBoxOne.setPreferredSize(new Dimension(95, 20)); 
244             realplayBtnOne.setPreferredSize(new Dimension(115, 20)); 
245             
246             realPlayWindowOne.setEnabled(false);
247              chnComboBoxOne.setEnabled(false);
248             streamComboBoxOne.setEnabled(false);
249             realplayBtnOne.setEnabled(false);
250             
251             realplayBtnOne.addActionListener(new ActionListener() {            
252                 @Override
253                 public void actionPerformed(ActionEvent e) {
254                     realplayOne();
255                 }
256             });
257         }
258     }
259     
260     // 预览
261     public void realplayOne() {
262         if(!isrealplayOne) {
263             m_hPlayHandleOne = RealPlayModule.startRealPlay(chnComboBoxOne.getSelectedIndex(), 
264                     streamComboBoxOne.getSelectedIndex()==0? 0:3,
265                     realPlayWindowOne);
266             if(m_hPlayHandleOne.longValue() != 0) {
267                 realPlayWindowOne.repaint();
268                 isrealplayOne = true;
269                 chnComboBoxOne.setEnabled(false);
270                 streamComboBoxOne.setEnabled(false);
271                 realplayBtnOne.setText(Res.string().getStopRealPlay());
272             } else {
273                 JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
274             }
275         } else {
276             RealPlayModule.stopRealPlay(m_hPlayHandleOne);
277             realPlayWindowOne.repaint();
278             isrealplayOne = false;
279             chnComboBoxOne.setEnabled(true);
280             streamComboBoxOne.setEnabled(true);
281             realplayBtnOne.setText(Res.string().getStartRealPlay());
282         }    
283     }
284     
285     
286     private class RealPanelTwo extends JPanel {
287         private static final long serialVersionUID = 1L;
288         
289         public RealPanelTwo() {
290             BorderEx.set(this, Res.string().getRealplay(), 2);
291             setLayout(new BorderLayout());
292             
293             channelPanelTwo = new Panel();
294             realplayPanelTwo = new JPanel();
295             
296             add(channelPanelTwo, BorderLayout.NORTH);
297             add(realplayPanelTwo, BorderLayout.CENTER);
298             
299             /************ 预览面板 **************/
300             realplayPanelTwo.setLayout(new BorderLayout());
301             realplayPanelTwo.setBorder(new EmptyBorder(5, 5, 5, 5));
302             realPlayWindowTwo = new Panel();
303             realPlayWindowTwo.setBackground(Color.GRAY);
304             realplayPanelTwo.add(realPlayWindowTwo, BorderLayout.CENTER);
305             
306             /************ 通道、码流面板 **************/
307             chnlabelTwo = new JLabel(Res.string().getChn());
308             chnComboBoxTwo = new JComboBox();                
309
310             streamLabelTwo = new JLabel(Res.string().getStreamType());
311             String[] stream = {Res.string().getMasterStream(), Res.string().getSubStream()};
312             streamComboBoxTwo = new JComboBox(stream);    
313             
314             realplayBtnTwo = new JButton(Res.string().getStartRealPlay());
315             
316             channelPanelTwo.setLayout(new FlowLayout());            
317             channelPanelTwo.add(chnlabelTwo);
318             channelPanelTwo.add(chnComboBoxTwo);
319             channelPanelTwo.add(streamLabelTwo);
320             channelPanelTwo.add(streamComboBoxTwo);
321             channelPanelTwo.add(realplayBtnTwo);
322             
323             chnComboBoxTwo.setPreferredSize(new Dimension(80, 20)); 
324             streamComboBoxTwo.setPreferredSize(new Dimension(95, 20)); 
325             realplayBtnTwo.setPreferredSize(new Dimension(115, 20)); 
326             
327             realPlayWindowTwo.setEnabled(false);
328              chnComboBoxTwo.setEnabled(false);
329             streamComboBoxTwo.setEnabled(false);
330             realplayBtnTwo.setEnabled(false);
331             
332             realplayBtnTwo.addActionListener(new ActionListener() {            
333                 @Override
334                 public void actionPerformed(ActionEvent e) {
335                     realplayTwo();
336                 }
337             });
338         }
339     }
340     
341     // 预览
342     public void realplayTwo() {
343         if(!isrealplayTwo) {
344             m_hPlayHandleTwo = RealPlayModule.startRealPlay(chnComboBoxTwo.getSelectedIndex(), 
345                     streamComboBoxTwo.getSelectedIndex()==0? 0:3,
346                     realPlayWindowTwo);
347             if(m_hPlayHandleTwo.longValue() != 0) {
348                 realPlayWindowTwo.repaint();
349                 isrealplayTwo = true;
350                 chnComboBoxTwo.setEnabled(false);
351                 streamComboBoxTwo.setEnabled(false);
352                 realplayBtnTwo.setText(Res.string().getStopRealPlay());
353             } else {
354                 JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
355             }
356         } else {
357             RealPlayModule.stopRealPlay(m_hPlayHandleTwo);
358             realPlayWindowTwo.repaint();
359             isrealplayTwo = false;
360             chnComboBoxTwo.setEnabled(true);
361             streamComboBoxTwo.setEnabled(true);
362             realplayBtnTwo.setText(Res.string().getStartRealPlay());
363         }    
364     }
365     private void setButtonEnable(boolean bln) {
366         realPlayWindowOne.setEnabled(bln);
367         chnComboBoxOne.setEnabled(bln);
368         streamComboBoxOne.setEnabled(bln);
369         realplayBtnOne.setEnabled(bln);
370         
371         realPlayWindowTwo.setEnabled(bln);
372         chnComboBoxTwo.setEnabled(bln);
373         streamComboBoxTwo.setEnabled(bln);
374         realplayBtnTwo.setEnabled(bln);
375     }
376     
377     /*
378      * 登录
379      */
380     private LoginPanel loginPanel;    
381     
382     private JPanel realPanel;
383     
384     /*
385      * 预览
386      */
387     private RealPanelOne realPanelOne;
388     private JPanel realplayPanelOne;
389     private Panel realPlayWindowOne;
390     private Panel channelPanelOne;
391     
392     private JLabel chnlabelOne;
393     private JComboBox chnComboBoxOne;    
394     private JLabel streamLabelOne;
395     private JComboBox streamComboBoxOne;
396     private JButton realplayBtnOne;
397
398     // 
399     private RealPanelTwo realPanelTwo;
400     private JPanel realplayPanelTwo;
401     private Panel realPlayWindowTwo;
402     private Panel channelPanelTwo;
403     
404     private JLabel chnlabelTwo;
405     private JComboBox chnComboBoxTwo;    
406     private JLabel streamLabelTwo;
407     private JComboBox streamComboBoxTwo;
408     private JButton realplayBtnTwo;
409 }
410
411 public class RealPlay {
412     public static void main(String[] args) {    
413         SwingUtilities.invokeLater(new Runnable() {
414             public void run() {
415                 RealPlayFrame demo = new RealPlayFrame();    
416                 demo.setVisible(true);
417             }
418         });        
419     }
420 }