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.awt.image.BufferedImage;
14 import java.io.ByteArrayInputStream;
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.Vector;
18
19 import javax.imageio.ImageIO;
20 import javax.swing.DefaultComboBoxModel;
21 import javax.swing.JButton;
22 import javax.swing.JComboBox;
23 import javax.swing.JFrame;
24 import javax.swing.JLabel;
25 import javax.swing.JOptionPane;
26 import javax.swing.JPanel;
27 import javax.swing.SwingUtilities;
28 import javax.swing.UIManager;
29 import javax.swing.border.EmptyBorder;
30
31 import com.netsdk.common.*;
32 import com.netsdk.demo.module.*;
33 import com.netsdk.lib.*;
34 import com.netsdk.lib.NetSDKLib.LLong;
35
36 import com.sun.jna.CallbackThreadInitializer;
37 import com.sun.jna.Native;
38 import com.sun.jna.Pointer;
39
40 /**
41  * Capture Picture Demo
42  */
43 class CapturePictureFrame extends JFrame{
44     private static final long serialVersionUID = 1L;
45     
46     // device channel list
47     private Vector<String> chnlist = new Vector<String>(); 
48
49     // This field indicates whether the device is playing
50     private boolean bRealPlay = false;
51     
52     // This field indicates whether the device is timing capture
53     private boolean bTimerCapture = false;    
54     
55     // device disconnect callback instance
56     private static DisConnect disConnect       = new DisConnect(); 
57     
58     // device reconnect callback instance
59     private static HaveReConnect haveReConnect = new HaveReConnect(); 
60     
61     // realplay handle
62     public static LLong m_hPlayHandle = new LLong(0);
63     
64     // capture picture frame (this)
65     private static JFrame frame = new JFrame();   
66     
67     public CapturePictureFrame() {
68         setTitle(Res.string().getCapturePicture());
69         setLayout(new BorderLayout());
70         pack();
71         setSize(800, 560);
72         setResizable(false);
73         setLocationRelativeTo(null);
74         LoginModule.init(disConnect, haveReConnect);   // init sdk
75         
76         try {
77             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
78         } catch (Exception e) {
79             e.printStackTrace();
80         } 
81      
82         loginPanel = new LoginPanel();
83         realPanel = new RealPanel();
84         picPanel = new PICPanel();
85         
86         add(loginPanel, BorderLayout.NORTH);
87         add(realPanel, BorderLayout.CENTER);
88         add(picPanel, BorderLayout.EAST);
89         
90         loginPanel.addLoginBtnActionListener(new ActionListener() {
91             @Override
92             public void actionPerformed(ActionEvent e) {               
93                 if(loginPanel.checkLoginText()) {
94                     if(login()) {
95                         frame = ToolKits.getFrame(e);
96                         frame.setTitle(Res.string().getCapturePicture() + " : " + Res.string().getOnline());
97                     }
98                 }
99             }
100         });
101         
102         loginPanel.addLogoutBtnActionListener(new ActionListener() {        
103             @Override
104             public void actionPerformed(ActionEvent e) {
105                 frame.setTitle(Res.string().getCapturePicture());
106                 logout();    
107             }
108         });
109         
110         addWindowListener(new WindowAdapter() {
111             public void windowClosing(WindowEvent e) {
112                 RealPlayModule.stopRealPlay(m_hPlayHandle);
113                 LoginModule.logout();
114                 LoginModule.cleanup();
115                 dispose();    
116                 
117                 SwingUtilities.invokeLater(new Runnable() {
118                     public void run() {
119                         FunctionList demo = new FunctionList();
120                         demo.setVisible(true);
121                     }
122                 });
123             }
124         });
125     }
126     
127     /////////////////function///////////////////
128     // device disconnect callback class 
129     // set it's instance by call CLIENT_Init, when device disconnect sdk will call it.
130     private static class DisConnect implements NetSDKLib.fDisConnect {
131         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
132             System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
133             
134             SwingUtilities.invokeLater(new Runnable() {
135                 public void run() {
136                     frame.setTitle(Res.string().getCapturePicture() + " : " + Res.string().getDisConnectReconnecting());
137                 }
138             });
139         }
140     }
141     
142     // device reconnect(success) callback class
143     // set it's instance by call CLIENT_SetAutoReconnect, when device reconnect success sdk will call it.
144     private static class HaveReConnect implements NetSDKLib.fHaveReConnect {
145         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
146             System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
147             
148             SwingUtilities.invokeLater(new Runnable() {
149                 public void run() {
150                     frame.setTitle(Res.string().getCapturePicture() + " : " + Res.string().getOnline());
151                 }
152             });
153         }
154     }
155     
156     public boolean login() {
157         Native.setCallbackThreadInitializer(m_CaptureReceiveCB, 
158                                             new CallbackThreadInitializer(false, false, "snapPicture callback thread")); 
159         if(LoginModule.login(loginPanel.ipTextArea.getText(), 
160                         Integer.parseInt(loginPanel.portTextArea.getText()), 
161                         loginPanel.nameTextArea.getText(), 
162                         new String(loginPanel.passwordTextArea.getPassword()))) {
163     
164             loginPanel.setButtonEnable(true);
165             setButtonEnable(true);
166             
167             for(int i = 1; i < LoginModule.m_stDeviceInfo.byChanNum + 1; i++) {
168                 chnlist.add(Res.string().getChannel() + " " + String.valueOf(i));
169             }
170             
171             chnComboBox.setModel(new DefaultComboBoxModel(chnlist));
172         
173             CapturePictureModule.setSnapRevCallBack(m_CaptureReceiveCB);        
174         } else {
175             JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
176             return false;
177         }
178         return true;
179     }
180     
181     public void logout() {
182         if (bTimerCapture) {
183             CapturePictureModule.stopCapturePicture(chnComboBox.getSelectedIndex());
184         }
185         RealPlayModule.stopRealPlay(m_hPlayHandle);
186         LoginModule.logout();
187         
188         loginPanel.setButtonEnable(false);
189         setButtonEnable(false);
190         realPlayWindow.repaint();    
191         pictureShowWindow.setOpaque(true);
192         pictureShowWindow.repaint();
193         
194         bRealPlay = false;
195         realplayBtn.setText(Res.string().getStartRealPlay());
196         
197         for(int i = 0; i < LoginModule.m_stDeviceInfo.byChanNum; i++) {
198             chnlist.clear();
199         }
200         
201         chnComboBox.setModel(new DefaultComboBoxModel());
202         
203         bTimerCapture = false;
204         timerCaptureBtn.setText(Res.string().getTimerCapture());
205     }
206     
207     /*
208      * realplay show and control panel
209      */
210     private class RealPanel extends JPanel {
211         private static final long serialVersionUID = 1L;
212         
213         public RealPanel() {
214             BorderEx.set(this, Res.string().getRealplay(), 2);
215             setLayout(new BorderLayout());
216             
217             channelPanel = new Panel();
218             realplayPanel = new JPanel();
219             
220             add(channelPanel, BorderLayout.SOUTH);
221             add(realplayPanel, BorderLayout.CENTER);
222             
223             /************ realplay panel **************/
224             realplayPanel.setLayout(new BorderLayout());
225             realplayPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
226             realPlayWindow = new Panel();
227             realPlayWindow.setBackground(Color.GRAY);
228             realplayPanel.add(realPlayWindow, BorderLayout.CENTER);
229             
230             /************ channel and stream panel **************/
231             chnlabel = new JLabel(Res.string().getChannel());
232             chnComboBox = new JComboBox();                 
233
234             streamLabel = new JLabel(Res.string().getStreamType());
235             String[] stream = {Res.string().getMasterStream(), Res.string().getSubStream()};
236             streamComboBox = new JComboBox(stream);     
237             
238             realplayBtn = new JButton(Res.string().getStartRealPlay());
239             
240             channelPanel.setLayout(new FlowLayout());            
241             channelPanel.add(chnlabel);
242             channelPanel.add(chnComboBox);
243             channelPanel.add(streamLabel);
244             channelPanel.add(streamComboBox);
245             channelPanel.add(realplayBtn);
246             
247             chnComboBox.setPreferredSize(new Dimension(90, 20)); 
248             streamComboBox.setPreferredSize(new Dimension(90, 20)); 
249             realplayBtn.setPreferredSize(new Dimension(120, 20)); 
250             
251             realPlayWindow.setEnabled(false);
252              chnComboBox.setEnabled(false);
253             streamComboBox.setEnabled(false);
254             realplayBtn.setEnabled(false);
255             
256             realplayBtn.addActionListener(new ActionListener() {            
257                 @Override
258                 public void actionPerformed(ActionEvent e) {
259                     realplay();
260                 }
261             });
262         }
263     }
264     
265     public void realplay() {
266         if(!bRealPlay) {
267             m_hPlayHandle = RealPlayModule.startRealPlay(chnComboBox.getSelectedIndex(), 
268                     streamComboBox.getSelectedIndex()==0? 0:3,
269                     realPlayWindow);
270             if(m_hPlayHandle.longValue() != 0) {
271                 realPlayWindow.repaint();
272                 bRealPlay = true;
273                 chnComboBox.setEnabled(false);
274                 streamComboBox.setEnabled(false);
275                 realplayBtn.setText(Res.string().getStopRealPlay());
276             } 
277         } else {
278             RealPlayModule.stopRealPlay(m_hPlayHandle);
279             realPlayWindow.repaint();
280             bRealPlay = false;
281             chnComboBox.setEnabled(true && !bTimerCapture);
282             streamComboBox.setEnabled(true);
283             realplayBtn.setText(Res.string().getStartRealPlay());
284         }    
285     }
286     
287     /*
288      * capture picture panel 
289      */
290     private class PICPanel extends JPanel {
291         private static final long serialVersionUID = 1L;
292         
293         public PICPanel() {
294             
295             setPreferredSize(new Dimension(350, 600));
296             BorderEx.set(this, Res.string().getCapturePicture(), 2);
297             setLayout(new BorderLayout());
298             
299             pictureShowPanel = new JPanel();  
300             capturePanel = new JPanel();
301             
302             add(pictureShowPanel, BorderLayout.CENTER);
303             add(capturePanel, BorderLayout.SOUTH);
304             
305             /************** capture picture button ************/
306             capturePanel.setLayout(new GridLayout(3, 1));
307             
308             localCaptureBtn = new JButton(Res.string().getLocalCapture());
309             remoteCaptureBtn = new JButton(Res.string().getRemoteCapture());
310             timerCaptureBtn = new JButton(Res.string().getTimerCapture());
311             
312             localCaptureBtn.setPreferredSize(new Dimension(150, 20));
313             remoteCaptureBtn.setPreferredSize(new Dimension(150, 20));
314             timerCaptureBtn.setPreferredSize(new Dimension(150, 20));
315             
316             capturePanel.add(localCaptureBtn);
317             capturePanel.add(remoteCaptureBtn);
318             capturePanel.add(timerCaptureBtn);
319             
320             localCaptureBtn.setEnabled(false);
321             remoteCaptureBtn.setEnabled(false);
322             timerCaptureBtn.setEnabled(false);
323             
324             /************** picture show panel ************/
325             pictureShowPanel.setLayout(new BorderLayout());
326             pictureShowPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
327             pictureShowWindow = new PaintPanel();
328             pictureShowPanel.add(pictureShowWindow, BorderLayout.CENTER);
329             
330             localCaptureBtn.addActionListener(new ActionListener() {    
331                 @Override
332                 public void actionPerformed(ActionEvent arg0) {
333                     if (!bRealPlay) {
334                         JOptionPane.showMessageDialog(null, Res.string().getNeedStartRealPlay(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
335                         return;
336                     }
337                     String strFileName = SavePath.getSavePath().getSaveCapturePath();
338                     System.out.println("strFileName = " + strFileName);
339                     
340                     if(!CapturePictureModule.localCapturePicture(m_hPlayHandle, strFileName)) {
341                         JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
342                         return;
343                     }
344                     
345                     BufferedImage bufferedImage = null;       
346                     try {
347                         bufferedImage = ImageIO.read(new File(strFileName)); 
348                         if(bufferedImage == null) {
349                             return;
350                         }
351                     } catch (IOException e) {
352                         e.printStackTrace();
353                     }    
354                     pictureShowWindow.setOpaque(false);
355                     pictureShowWindow.setImage(bufferedImage);
356                     pictureShowWindow.repaint();    
357                     
358                 }
359             });
360             
361             remoteCaptureBtn.addActionListener(new ActionListener() {    
362                 @Override
363                 public void actionPerformed(ActionEvent arg0) {
364                     if(!CapturePictureModule.remoteCapturePicture(chnComboBox.getSelectedIndex())) {
365                         JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
366                     }
367                 }
368             });
369             
370             timerCaptureBtn.addActionListener(new ActionListener() {    
371                 @Override
372                 public void actionPerformed(ActionEvent arg0) {
373                     if (!bTimerCapture) {
374                         
375                         if(!CapturePictureModule.timerCapturePicture(chnComboBox.getSelectedIndex())) {
376                             JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
377                         }else{
378                             bTimerCapture = true;
379                             timerCaptureBtn.setText(Res.string().getStopCapture());
380                             chnComboBox.setEnabled(false);
381                             remoteCaptureBtn.setEnabled(false);
382                         }
383                     }else {
384                         if(!CapturePictureModule.stopCapturePicture(chnComboBox.getSelectedIndex())) {
385                             JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
386                         }else{
387                             bTimerCapture = false;
388                             timerCaptureBtn.setText(Res.string().getTimerCapture());
389                             chnComboBox.setEnabled(true && !bRealPlay);
390                             remoteCaptureBtn.setEnabled(true);
391                         }
392                     }
393                 }
394             });
395         }
396     }
397     
398     public fCaptureReceiveCB  m_CaptureReceiveCB = new fCaptureReceiveCB();
399     public class fCaptureReceiveCB implements NetSDKLib.fSnapRev{
400         BufferedImage bufferedImage = null;
401         public void invoke( LLong lLoginID, Pointer pBuf, int RevLen, int EncodeType, int CmdSerial, Pointer dwUser) {    
402             if(pBuf != null && RevLen > 0) {                    
403                 String strFileName = SavePath.getSavePath().getSaveCapturePath(); 
404
405                 System.out.println("strFileName = " + strFileName);
406
407                 byte[] buf = pBuf.getByteArray(0, RevLen);
408                 ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
409                 try {
410                     bufferedImage = ImageIO.read(byteArrInput);
411                     if(bufferedImage == null) {
412                         return;
413                     }
414                     ImageIO.write(bufferedImage, "jpg", new File(strFileName));    
415                 } catch (IOException e) {
416                     e.printStackTrace();
417                 }    
418                 
419                 // show picture     
420                 SwingUtilities.invokeLater(new Runnable() {    
421                     @Override
422                     public void run() {            
423                         pictureShowWindow.setOpaque(false);
424                         pictureShowWindow.setImage(bufferedImage);
425                         pictureShowWindow.repaint();                
426                     }
427                 });
428             }
429         }
430     }
431     
432     private void setButtonEnable(boolean bln) {
433         localCaptureBtn.setEnabled(bln);
434         remoteCaptureBtn.setEnabled(bln);
435         timerCaptureBtn.setEnabled(bln);
436         realPlayWindow.setEnabled(bln);
437         chnComboBox.setEnabled(bln);
438         streamComboBox.setEnabled(bln);
439         realplayBtn.setEnabled(bln);
440     }
441     
442     private LoginPanel loginPanel;
443     
444     private RealPanel realPanel;
445     private JPanel realplayPanel;
446     private Panel realPlayWindow;
447     private Panel channelPanel;
448     private JLabel chnlabel;
449     private JComboBox chnComboBox;    
450     private JLabel streamLabel;
451     private JComboBox streamComboBox;
452     private JButton realplayBtn;
453     
454     private PICPanel picPanel;
455     private JPanel pictureShowPanel;
456     private JPanel capturePanel;
457     private PaintPanel pictureShowWindow;
458     private JButton localCaptureBtn;
459     private JButton remoteCaptureBtn;
460     private JButton timerCaptureBtn;
461
462 }
463
464 public class CapturePicture {  
465     public static void main(String[] args) {    
466         SwingUtilities.invokeLater(new Runnable() {
467             public void run() {
468                 CapturePictureFrame demo = new CapturePictureFrame();    
469                 demo.setVisible(true);
470             }
471         });        
472     }
473 };
474
475