Jay
2024-11-08 02722a3f9eca857ce7fffea352e9f7ee692a1b71
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
package com.iailab.netsdk.demo.frame;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
 
import javax.imageio.ImageIO;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
 
import com.iailab.netsdk.common.*;
import com.iailab.netsdk.demo.module.*;
import com.iailab.netsdk.lib.*;
import com.iailab.netsdk.lib.NetSDKLib.LLong;
 
import com.sun.jna.CallbackThreadInitializer;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
 
/**
 * Capture Picture Demo
 */
class CapturePictureFrame extends JFrame{
    private static final long serialVersionUID = 1L;
    
    // device channel list
    private Vector<String> chnlist = new Vector<String>(); 
 
    // This field indicates whether the device is playing
    private boolean bRealPlay = false;
    
    // This field indicates whether the device is timing capture
    private boolean bTimerCapture = false;    
    
    // device disconnect callback instance
    private static DisConnect disConnect       = new DisConnect(); 
    
    // device reconnect callback instance
    private static HaveReConnect haveReConnect = new HaveReConnect(); 
    
    // realplay handle
    public static LLong m_hPlayHandle = new LLong(0);
    
    // capture picture frame (this)
    private static JFrame frame = new JFrame();   
    
    public CapturePictureFrame() {
        setTitle(Res.string().getCapturePicture());
        setLayout(new BorderLayout());
        pack();
        setSize(800, 560);
        setResizable(false);
        setLocationRelativeTo(null);
        LoginModule.init(disConnect, haveReConnect);   // init sdk
        
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        } 
     
        loginPanel = new LoginPanel();
        realPanel = new RealPanel();
        picPanel = new PICPanel();
        
        add(loginPanel, BorderLayout.NORTH);
        add(realPanel, BorderLayout.CENTER);
        add(picPanel, BorderLayout.EAST);
        
        loginPanel.addLoginBtnActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {               
                if(loginPanel.checkLoginText()) {
                    if(login()) {
                        frame = ToolKits.getFrame(e);
                        frame.setTitle(Res.string().getCapturePicture() + " : " + Res.string().getOnline());
                    }
                }
            }
        });
        
        loginPanel.addLogoutBtnActionListener(new ActionListener() {        
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setTitle(Res.string().getCapturePicture());
                logout();    
            }
        });
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                RealPlayModule.stopRealPlay(m_hPlayHandle);
                LoginModule.logout();
                LoginModule.cleanup();
                dispose();    
                
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        FunctionList demo = new FunctionList();
                        demo.setVisible(true);
                    }
                });
            }
        });
    }
    
    /////////////////function///////////////////
    // device disconnect callback class 
    // set it's instance by call CLIENT_Init, when device disconnect sdk will call it.
    private static class DisConnect implements NetSDKLib.fDisConnect {
        public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
            System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
            
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    frame.setTitle(Res.string().getCapturePicture() + " : " + Res.string().getDisConnectReconnecting());
                }
            });
        }
    }
    
    // device reconnect(success) callback class
    // set it's instance by call CLIENT_SetAutoReconnect, when device reconnect success sdk will call it.
    private static class HaveReConnect implements NetSDKLib.fHaveReConnect {
        public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
            System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
            
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    frame.setTitle(Res.string().getCapturePicture() + " : " + Res.string().getOnline());
                }
            });
        }
    }
    
    public boolean login() {
        Native.setCallbackThreadInitializer(m_CaptureReceiveCB, 
                                            new CallbackThreadInitializer(false, false, "snapPicture callback thread")); 
        if(LoginModule.login(loginPanel.ipTextArea.getText(), 
                        Integer.parseInt(loginPanel.portTextArea.getText()), 
                        loginPanel.nameTextArea.getText(), 
                        new String(loginPanel.passwordTextArea.getPassword()))) {
    
            loginPanel.setButtonEnable(true);
            setButtonEnable(true);
            
            for(int i = 1; i < LoginModule.m_stDeviceInfo.byChanNum + 1; i++) {
                chnlist.add(Res.string().getChannel() + " " + String.valueOf(i));
            }
            
            chnComboBox.setModel(new DefaultComboBoxModel(chnlist));
        
            CapturePictureModule.setSnapRevCallBack(m_CaptureReceiveCB);        
        } else {
            JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
    
    public void logout() {
        if (bTimerCapture) {
            CapturePictureModule.stopCapturePicture(chnComboBox.getSelectedIndex());
        }
        RealPlayModule.stopRealPlay(m_hPlayHandle);
        LoginModule.logout();
        
        loginPanel.setButtonEnable(false);
        setButtonEnable(false);
        realPlayWindow.repaint();    
        pictureShowWindow.setOpaque(true);
        pictureShowWindow.repaint();
        
        bRealPlay = false;
        realplayBtn.setText(Res.string().getStartRealPlay());
        
        for(int i = 0; i < LoginModule.m_stDeviceInfo.byChanNum; i++) {
            chnlist.clear();
        }
        
        chnComboBox.setModel(new DefaultComboBoxModel());
        
        bTimerCapture = false;
        timerCaptureBtn.setText(Res.string().getTimerCapture());
    }
    
    /*
     * realplay show and control panel
     */
    private class RealPanel extends JPanel {
        private static final long serialVersionUID = 1L;
        
        public RealPanel() {
            BorderEx.set(this, Res.string().getRealplay(), 2);
            setLayout(new BorderLayout());
            
            channelPanel = new Panel();
            realplayPanel = new JPanel();
            
            add(channelPanel, BorderLayout.SOUTH);
            add(realplayPanel, BorderLayout.CENTER);
            
            /************ realplay panel **************/
            realplayPanel.setLayout(new BorderLayout());
            realplayPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
            realPlayWindow = new Panel();
            realPlayWindow.setBackground(Color.GRAY);
            realplayPanel.add(realPlayWindow, BorderLayout.CENTER);
            
            /************ channel and stream panel **************/
            chnlabel = new JLabel(Res.string().getChannel());
            chnComboBox = new JComboBox();                 
 
            streamLabel = new JLabel(Res.string().getStreamType());
            String[] stream = {Res.string().getMasterStream(), Res.string().getSubStream()};
            streamComboBox = new JComboBox(stream);     
            
            realplayBtn = new JButton(Res.string().getStartRealPlay());
            
            channelPanel.setLayout(new FlowLayout());            
            channelPanel.add(chnlabel);
            channelPanel.add(chnComboBox);
            channelPanel.add(streamLabel);
            channelPanel.add(streamComboBox);
            channelPanel.add(realplayBtn);
            
            chnComboBox.setPreferredSize(new Dimension(90, 20)); 
            streamComboBox.setPreferredSize(new Dimension(90, 20)); 
            realplayBtn.setPreferredSize(new Dimension(120, 20)); 
            
            realPlayWindow.setEnabled(false);
             chnComboBox.setEnabled(false);
            streamComboBox.setEnabled(false);
            realplayBtn.setEnabled(false);
            
            realplayBtn.addActionListener(new ActionListener() {            
                @Override
                public void actionPerformed(ActionEvent e) {
                    realplay();
                }
            });
        }
    }
    
    public void realplay() {
        if(!bRealPlay) {
            m_hPlayHandle = RealPlayModule.startRealPlay(chnComboBox.getSelectedIndex(), 
                    streamComboBox.getSelectedIndex()==0? 0:3,
                    realPlayWindow);
            if(m_hPlayHandle.longValue() != 0) {
                realPlayWindow.repaint();
                bRealPlay = true;
                chnComboBox.setEnabled(false);
                streamComboBox.setEnabled(false);
                realplayBtn.setText(Res.string().getStopRealPlay());
            } 
        } else {
            RealPlayModule.stopRealPlay(m_hPlayHandle);
            realPlayWindow.repaint();
            bRealPlay = false;
            chnComboBox.setEnabled(true && !bTimerCapture);
            streamComboBox.setEnabled(true);
            realplayBtn.setText(Res.string().getStartRealPlay());
        }    
    }
    
    /*
     * capture picture panel 
     */
    private class PICPanel extends JPanel {
        private static final long serialVersionUID = 1L;
        
        public PICPanel() {
            
            setPreferredSize(new Dimension(350, 600));
            BorderEx.set(this, Res.string().getCapturePicture(), 2);
            setLayout(new BorderLayout());
            
            pictureShowPanel = new JPanel();  
            capturePanel = new JPanel();
            
            add(pictureShowPanel, BorderLayout.CENTER);
            add(capturePanel, BorderLayout.SOUTH);
            
            /************** capture picture button ************/
            capturePanel.setLayout(new GridLayout(3, 1));
            
            localCaptureBtn = new JButton(Res.string().getLocalCapture());
            remoteCaptureBtn = new JButton(Res.string().getRemoteCapture());
            timerCaptureBtn = new JButton(Res.string().getTimerCapture());
            
            localCaptureBtn.setPreferredSize(new Dimension(150, 20));
            remoteCaptureBtn.setPreferredSize(new Dimension(150, 20));
            timerCaptureBtn.setPreferredSize(new Dimension(150, 20));
            
            capturePanel.add(localCaptureBtn);
            capturePanel.add(remoteCaptureBtn);
            capturePanel.add(timerCaptureBtn);
            
            localCaptureBtn.setEnabled(false);
            remoteCaptureBtn.setEnabled(false);
            timerCaptureBtn.setEnabled(false);
            
            /************** picture show panel ************/
            pictureShowPanel.setLayout(new BorderLayout());
            pictureShowPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
            pictureShowWindow = new PaintPanel();
            pictureShowPanel.add(pictureShowWindow, BorderLayout.CENTER);
            
            localCaptureBtn.addActionListener(new ActionListener() {    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if (!bRealPlay) {
                        JOptionPane.showMessageDialog(null, Res.string().getNeedStartRealPlay(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    String strFileName = SavePath.getSavePath().getSaveCapturePath();
                    System.out.println("strFileName = " + strFileName);
                    
                    if(!CapturePictureModule.localCapturePicture(m_hPlayHandle, strFileName)) {
                        JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    
                    BufferedImage bufferedImage = null;       
                    try {
                        bufferedImage = ImageIO.read(new File(strFileName)); 
                        if(bufferedImage == null) {
                            return;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }    
                    pictureShowWindow.setOpaque(false);
                    pictureShowWindow.setImage(bufferedImage);
                    pictureShowWindow.repaint();    
                    
                }
            });
            
            remoteCaptureBtn.addActionListener(new ActionListener() {    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if(!CapturePictureModule.remoteCapturePicture(chnComboBox.getSelectedIndex())) {
                        JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                    }
                }
            });
            
            timerCaptureBtn.addActionListener(new ActionListener() {    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if (!bTimerCapture) {
                        
                        if(!CapturePictureModule.timerCapturePicture(chnComboBox.getSelectedIndex())) {
                            JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                        }else{
                            bTimerCapture = true;
                            timerCaptureBtn.setText(Res.string().getStopCapture());
                            chnComboBox.setEnabled(false);
                            remoteCaptureBtn.setEnabled(false);
                        }
                    }else {
                        if(!CapturePictureModule.stopCapturePicture(chnComboBox.getSelectedIndex())) {
                            JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                        }else{
                            bTimerCapture = false;
                            timerCaptureBtn.setText(Res.string().getTimerCapture());
                            chnComboBox.setEnabled(true && !bRealPlay);
                            remoteCaptureBtn.setEnabled(true);
                        }
                    }
                }
            });
        }
    }
    
    public fCaptureReceiveCB  m_CaptureReceiveCB = new fCaptureReceiveCB();
    public class fCaptureReceiveCB implements NetSDKLib.fSnapRev{
        BufferedImage bufferedImage = null;
        public void invoke( LLong lLoginID, Pointer pBuf, int RevLen, int EncodeType, int CmdSerial, Pointer dwUser) {    
            if(pBuf != null && RevLen > 0) {                    
                String strFileName = SavePath.getSavePath().getSaveCapturePath(); 
 
                System.out.println("strFileName = " + strFileName);
 
                byte[] buf = pBuf.getByteArray(0, RevLen);
                ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
                try {
                    bufferedImage = ImageIO.read(byteArrInput);
                    if(bufferedImage == null) {
                        return;
                    }
                    ImageIO.write(bufferedImage, "jpg", new File(strFileName));    
                } catch (IOException e) {
                    e.printStackTrace();
                }    
                
                // show picture     
                SwingUtilities.invokeLater(new Runnable() {    
                    @Override
                    public void run() {            
                        pictureShowWindow.setOpaque(false);
                        pictureShowWindow.setImage(bufferedImage);
                        pictureShowWindow.repaint();                
                    }
                });
            }
        }
    }
    
    private void setButtonEnable(boolean bln) {
        localCaptureBtn.setEnabled(bln);
        remoteCaptureBtn.setEnabled(bln);
        timerCaptureBtn.setEnabled(bln);
        realPlayWindow.setEnabled(bln);
        chnComboBox.setEnabled(bln);
        streamComboBox.setEnabled(bln);
        realplayBtn.setEnabled(bln);
    }
    
    private LoginPanel loginPanel;
    
    private RealPanel realPanel;
    private JPanel realplayPanel;
    private Panel realPlayWindow;
    private Panel channelPanel;
    private JLabel chnlabel;
    private JComboBox chnComboBox;    
    private JLabel streamLabel;
    private JComboBox streamComboBox;
    private JButton realplayBtn;
    
    private PICPanel picPanel;
    private JPanel pictureShowPanel;
    private JPanel capturePanel;
    private PaintPanel pictureShowWindow;
    private JButton localCaptureBtn;
    private JButton remoteCaptureBtn;
    private JButton timerCaptureBtn;
 
}
 
public class CapturePicture {  
    public static void main(String[] args) {    
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CapturePictureFrame demo = new CapturePictureFrame();    
                demo.setVisible(true);
            }
        });        
    }
};