潘志宝
2024-11-11 aa1aa68141e3ee33f98cdd785ddc5c244fedc592
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.demo.frame.TargetRecognition;
H 2
3 import com.iailab.netsdk.common.*;
4 import com.iailab.netsdk.demo.module.LoginModule;
5 import com.iailab.netsdk.demo.module.RealPlayModule;
6 import com.iailab.netsdk.demo.module.TargetRecognitionModule;
7 import com.iailab.netsdk.lib.NetSDKLib;
8 import com.iailab.netsdk.lib.NetSDKLib.*;
9 import com.iailab.netsdk.lib.ToolKits;
10 import com.sun.jna.Pointer;
11
12 import javax.imageio.ImageIO;
13 import javax.swing.*;
14 import javax.swing.border.EmptyBorder;
15 import java.awt.*;
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ActionListener;
18 import java.awt.event.WindowAdapter;
19 import java.awt.event.WindowEvent;
20 import java.awt.image.BufferedImage;
21 import java.io.*;
22 import java.util.Vector;
23
24 class TargetRecognitionFrame extends JFrame {
25   private static final long serialVersionUID = 1L;
26
27   private Vector<String> chnList = new Vector<String>();
28
29   private boolean isRealplay = false;
30   private static boolean isAttach = false;
31
32   // 设备断线通知回调
33   private static DisConnect disConnect = new DisConnect();
34
35   // 网络连接恢复
36   private static HaveReConnect haveReConnect = new HaveReConnect();
37
38   // 预览句柄
39   public static LLong m_hPlayHandle = new LLong(0);
40
41   // 订阅句柄
42   public static LLong m_hAttachHandle = new LLong(0);
43
44   // 获取界面窗口
45   private static JFrame frame = new JFrame();
46
47   // 人脸库界面
48   private GroupOperateDialog groupOperateDialog = null;
49
50   // 全景图
51   private static BufferedImage globalBufferedImage = null;
52
53   // 人脸图
54   private static BufferedImage personBufferedImage = null;
55
56   // 候选人图
57   private static BufferedImage candidateBufferedImage = null;
58
59   // 用于目标检测
60   private static int groupId = 0;
61
62   private static int index = -1;
63
64   public TargetRecognitionFrame() {
65     setTitle(Res.string().getTargetRecognition());
66     setLayout(new BorderLayout());
67     pack();
68     setSize(800, 560);
69     setResizable(false);
70     setLocationRelativeTo(null);
71     LoginModule.init(disConnect, haveReConnect); // 打开工程,初始化
72
73     try {
74       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
75     } catch (Exception e) {
76       e.printStackTrace();
77     }
78
79     loginPanel = new LoginPanel();
80     TargetRecognitionEventPanel facePanel = new TargetRecognitionEventPanel();
81
82     add(loginPanel, BorderLayout.NORTH);
83     add(facePanel, BorderLayout.CENTER);
84
85     loginPanel.addLoginBtnActionListener(
86         new ActionListener() {
87           @Override
88           public void actionPerformed(ActionEvent e) {
89             if (loginPanel.checkLoginText()) {
90               if (login()) {
91                 frame = ToolKits.getFrame(e);
92                 frame.setTitle(
93                     Res.string().getTargetRecognition() + " : " + Res.string().getOnline());
94               }
95             }
96           }
97         });
98
99     loginPanel.addLogoutBtnActionListener(
100         new ActionListener() {
101           @Override
102           public void actionPerformed(ActionEvent e) {
103             frame.setTitle(Res.string().getTargetRecognition());
104             logout();
105           }
106         });
107
108     addWindowListener(
109         new WindowAdapter() {
110           public void windowClosing(WindowEvent e) {
111             TargetRecognitionModule.renderPrivateData(m_hPlayHandle, 0);
112             RealPlayModule.stopRealPlay(m_hPlayHandle);
113             TargetRecognitionModule.stopRealLoadPicture(m_hAttachHandle);
114             LoginModule.logout();
115             LoginModule.cleanup(); // 关闭工程,释放资源
116
117             dispose();
118
119             SwingUtilities.invokeLater(
120                 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 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(
137           new Runnable() {
138             public void run() {
139               frame.setTitle(
140                   Res.string().getTargetRecognition()
141                       + " : "
142                       + Res.string().getDisConnectReconnecting());
143             }
144           });
145     }
146   }
147
148   // 网络连接恢复,设备重连成功回调
149   // 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数
150   private static class HaveReConnect implements fHaveReConnect {
151     public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
152       System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
153
154       // 重连提示
155       SwingUtilities.invokeLater(
156           new Runnable() {
157             public void run() {
158               frame.setTitle(Res.string().getTargetRecognition() + " : " + Res.string().getOnline());
159             }
160           });
161     }
162   }
163
164   // 登录
165   public boolean login() {
166     if (LoginModule.login(
167         loginPanel.ipTextArea.getText(),
168         Integer.parseInt(loginPanel.portTextArea.getText()),
169         loginPanel.nameTextArea.getText(),
170         new String(loginPanel.passwordTextArea.getPassword()))) {
171
172       loginPanel.setButtonEnable(true);
173       setEnable(true);
174
175       for (int i = 1; i < LoginModule.m_stDeviceInfo.byChanNum + 1; i++) {
176         chnList.add(Res.string().getChannel() + " " + String.valueOf(i));
177       }
178
179       // 登陆成功,将通道添加到控件
180       chnComboBox.setModel(new DefaultComboBoxModel(chnList));
181     } else {
182       JOptionPane.showMessageDialog(
183           null,
184           Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(),
185           Res.string().getErrorMessage(),
186           JOptionPane.ERROR_MESSAGE);
187       return false;
188     }
189     return true;
190   }
191
192   // 登出
193   public void logout() {
194     TargetRecognitionModule.renderPrivateData(m_hPlayHandle, 0);
195     RealPlayModule.stopRealPlay(m_hPlayHandle);
196     TargetRecognitionModule.stopRealLoadPicture(m_hAttachHandle);
197     LoginModule.logout();
198
199     loginPanel.setButtonEnable(false);
200     setEnable(false);
201     realplayWindowPanel.repaint();
202
203     isRealplay = false;
204     realplayBtn.setText(Res.string().getStartRealPlay());
205
206     isAttach = false;
207
208     attachBtn.setText(Res.string().getAttach());
209     globalPicLabel.setText(
210         Res.string().getGlobalPicture() + " ------ [" + Res.string().getEventType() + "]");
211
212     globalPicShowPanel.setOpaque(true);
213     globalPicShowPanel.repaint();
214
215     personPicShowPanel.setOpaque(true);
216     personPicShowPanel.repaint();
217
218     candidatePicShowPanel.setOpaque(true);
219     candidatePicShowPanel.repaint();
220
221     timeTextField.setText("");
222     sexTextField.setText("");
223     ageTextField.setText("");
224     eyeTextField.setText("");
225     mouthTextField.setText("");
226     maskTextField.setText("");
227     beardTextField.setText("");
228
229     nameTextField.setText("");
230     sexTextField2.setText("");
231     birthdayTextField.setText("");
232     idNoTextField.setText("");
233     groupIdTextField.setText("");
234     groupNameTextField.setText("");
235     similaryTextField.setText("");
236
237     for (int i = 0; i < LoginModule.m_stDeviceInfo.byChanNum; i++) {
238       chnList.clear();
239     }
240
241     chnComboBox.setModel(new DefaultComboBoxModel());
242
243     groupId = 0;
244     globalBufferedImage = null;
245     personBufferedImage = null;
246     candidateBufferedImage = null;
247   }
248
249   public class TargetRecognitionEventPanel extends JPanel {
250     /** */
251     private static final long serialVersionUID = 1L;
252
253     public TargetRecognitionEventPanel() {
254       BorderEx.set(this, "", 2);
255       setLayout(new BorderLayout());
256
257       JPanel operatePanel = new JPanel(); // 通道、预览、订阅
258       JPanel panel = new JPanel();
259
260       add(operatePanel, BorderLayout.NORTH);
261       add(panel, BorderLayout.CENTER);
262
263       /*
264        * 操作面板:通道、预览、订阅按钮
265        */
266       chnlabel = new JLabel(Res.string().getChannel());
267       chnComboBox = new JComboBox();
268
269       realplayBtn = new JButton(Res.string().getStartRealPlay());
270       attachBtn = new JButton(Res.string().getAttach());
271       faceDataBaseBtn = new JButton(Res.string().getGroupOperate());
272       faceEventRecordBtn = new JButton("查找事件记录");
273       /** 以图搜图按钮 */
274       searchByPicBtn = new JButton(Res.string().getSearchByPic());
275
276       operatePanel.setLayout(new FlowLayout());
277       operatePanel.add(chnlabel);
278       operatePanel.add(chnComboBox);
279       operatePanel.add(realplayBtn);
280       operatePanel.add(attachBtn);
281       operatePanel.add(faceDataBaseBtn);
282       operatePanel.add(searchByPicBtn);
283       //            operatePanel.add(faceEventRecordBtn);
284
285       Dimension dim = new Dimension();
286       dim.width = 120;
287       dim.height = 20;
288
289       chnComboBox.setPreferredSize(new Dimension(80, 20));
290       attachBtn.setPreferredSize(dim);
291       realplayBtn.setPreferredSize(dim);
292       faceDataBaseBtn.setPreferredSize(dim);
293       faceEventRecordBtn.setPreferredSize(dim);
294       searchByPicBtn.setPreferredSize(dim);
295
296       chnComboBox.setEnabled(false);
297       realplayBtn.setEnabled(false);
298       attachBtn.setEnabled(false);
299       faceDataBaseBtn.setEnabled(false);
300       faceEventRecordBtn.setEnabled(false);
301       searchByPicBtn.setEnabled(false);
302
303       /*
304        * 预览、图片面板
305        */
306       JPanel realplayPanel = new JPanel();
307       JPanel globalPicPanel = new JPanel();
308       JPanel personPicPanel = new JPanel();
309       JPanel candidatePanel = new JPanel();
310
311       realplayPanel.setBorder(new EmptyBorder(0, 5, 5, 5));
312       globalPicPanel.setBorder(new EmptyBorder(0, 5, 5, 5));
313       personPicPanel.setBorder(new EmptyBorder(0, 5, 5, 5));
314       candidatePanel.setBorder(new EmptyBorder(0, 5, 5, 5));
315
316       panel.setLayout(new GridLayout(2, 2));
317
318       panel.add(realplayPanel);
319       panel.add(globalPicPanel);
320       panel.add(personPicPanel);
321       panel.add(candidatePanel);
322
323       /*
324        * 预览面板
325        */
326       JLabel realplayLabel = new JLabel(Res.string().getRealplay());
327       realplayWindowPanel = new Panel();
328       realplayWindowPanel.setBackground(Color.GRAY);
329       realplayPanel.setLayout(new BorderLayout());
330       realplayPanel.add(realplayLabel, BorderLayout.NORTH);
331       realplayPanel.add(realplayWindowPanel, BorderLayout.CENTER);
332
333       /*
334        * 全景图面板
335        */
336       globalPicLabel =
337           new JLabel(
338               Res.string().getGlobalPicture() + " ------ [" + Res.string().getEventType() + "]");
339       globalPicShowPanel = new PaintPanel();
340       globalPicPanel.setLayout(new BorderLayout());
341       globalPicPanel.add(globalPicLabel, BorderLayout.NORTH);
342       globalPicPanel.add(globalPicShowPanel, BorderLayout.CENTER);
343
344       /*
345        * 人脸图面板
346        */
347       JLabel personPiclabel = new JLabel(Res.string().getPersonPicture());
348       personPicShowPanel = new PaintPanel();
349       JPanel faceDataPanel = new JPanel();
350
351       Dimension dimension = new Dimension();
352       dimension.width = 200;
353       faceDataPanel.setPreferredSize(dimension);
354
355       personPicPanel.setLayout(new BorderLayout());
356       personPicPanel.add(personPiclabel, BorderLayout.NORTH);
357       personPicPanel.add(personPicShowPanel, BorderLayout.CENTER);
358       personPicPanel.add(faceDataPanel, BorderLayout.EAST);
359
360       // 人脸信息
361       JLabel timeLabel = new JLabel(Res.string().getTime(), JLabel.CENTER);
362       JLabel sexLabel = new JLabel(Res.string().getSex(), JLabel.CENTER);
363       JLabel ageLabel = new JLabel(Res.string().getAge(), JLabel.CENTER);
364       JLabel colorLabel = new JLabel(Res.string().getColor(), JLabel.CENTER);
365       JLabel eyeLabel = new JLabel(Res.string().getEye(), JLabel.CENTER);
366       JLabel mouthLabel = new JLabel(Res.string().getMouth(), JLabel.CENTER);
367       JLabel maskLabel = new JLabel(Res.string().getMask(), JLabel.CENTER);
368       JLabel beardLabel = new JLabel(Res.string().getBeard(), JLabel.CENTER);
369
370       Dimension dimension1 = new Dimension();
371       dimension1.height = 18;
372       dimension1.width = 50;
373       timeLabel.setPreferredSize(dimension1);
374       sexLabel.setPreferredSize(dimension1);
375       ageLabel.setPreferredSize(dimension1);
376       colorLabel.setPreferredSize(dimension1);
377       eyeLabel.setPreferredSize(dimension1);
378       mouthLabel.setPreferredSize(dimension1);
379       maskLabel.setPreferredSize(dimension1);
380       beardLabel.setPreferredSize(dimension1);
381
382       timeTextField = new JTextField();
383       sexTextField = new JTextField();
384       ageTextField = new JTextField();
385       eyeTextField = new JTextField();
386       mouthTextField = new JTextField();
387       maskTextField = new JTextField();
388       beardTextField = new JTextField();
389
390       Dimension dimension2 = new Dimension();
391       dimension2.width = 125;
392       dimension2.height = 19;
393       timeTextField.setPreferredSize(dimension2);
394       sexTextField.setPreferredSize(dimension2);
395       ageTextField.setPreferredSize(dimension2);
396       eyeTextField.setPreferredSize(dimension2);
397       mouthTextField.setPreferredSize(dimension2);
398       maskTextField.setPreferredSize(dimension2);
399       beardTextField.setPreferredSize(dimension2);
400
401       timeTextField.setHorizontalAlignment(JTextField.CENTER);
402       sexTextField.setHorizontalAlignment(JTextField.CENTER);
403       ageTextField.setHorizontalAlignment(JTextField.CENTER);
404       eyeTextField.setHorizontalAlignment(JTextField.CENTER);
405       mouthTextField.setHorizontalAlignment(JTextField.CENTER);
406       maskTextField.setHorizontalAlignment(JTextField.CENTER);
407       beardTextField.setHorizontalAlignment(JTextField.CENTER);
408
409       timeTextField.setEnabled(false);
410       sexTextField.setEnabled(false);
411       ageTextField.setEnabled(false);
412       eyeTextField.setEnabled(false);
413       mouthTextField.setEnabled(false);
414       maskTextField.setEnabled(false);
415       beardTextField.setEnabled(false);
416
417       timeTextField.setFont(new Font("黑体", Font.PLAIN, 11));
418       sexTextField.setFont(new Font("黑体", Font.PLAIN, 11));
419       ageTextField.setFont(new Font("黑体", Font.PLAIN, 11));
420       eyeTextField.setFont(new Font("黑体", Font.PLAIN, 11));
421       mouthTextField.setFont(new Font("黑体", Font.PLAIN, 11));
422       maskTextField.setFont(new Font("黑体", Font.PLAIN, 11));
423       beardTextField.setFont(new Font("黑体", Font.PLAIN, 11));
424
425       faceDataPanel.setLayout(new FlowLayout());
426
427       faceDataPanel.add(timeLabel);
428       faceDataPanel.add(timeTextField);
429       faceDataPanel.add(sexLabel);
430       faceDataPanel.add(sexTextField);
431       faceDataPanel.add(ageLabel);
432       faceDataPanel.add(ageTextField);
433       faceDataPanel.add(eyeLabel);
434       faceDataPanel.add(eyeTextField);
435       faceDataPanel.add(mouthLabel);
436       faceDataPanel.add(mouthTextField);
437       faceDataPanel.add(maskLabel);
438       faceDataPanel.add(maskTextField);
439       faceDataPanel.add(beardLabel);
440       faceDataPanel.add(beardTextField);
441
442       /*
443        * 候选人图面板
444        */
445       JLabel candidateLabel = new JLabel(Res.string().getCandidatePicture());
446       candidatePicShowPanel = new PaintPanel();
447       JPanel candidateDataPanel = new JPanel();
448
449       Dimension dimension4 = new Dimension();
450       dimension4.width = 220;
451       candidateDataPanel.setPreferredSize(dimension4);
452
453       candidatePanel.setLayout(new BorderLayout());
454       candidatePanel.add(candidateLabel, BorderLayout.NORTH);
455       candidatePanel.add(candidatePicShowPanel, BorderLayout.CENTER);
456       candidatePanel.add(candidateDataPanel, BorderLayout.EAST);
457
458       // 候选人信息
459       JLabel nameLabel = new JLabel(Res.string().getName(), JLabel.CENTER);
460       JLabel sexLabel2 = new JLabel(Res.string().getSex(), JLabel.CENTER);
461       JLabel birthdayLabel = new JLabel(Res.string().getBirthday(), JLabel.CENTER);
462       JLabel idNoLabel = new JLabel(Res.string().getIdNo(), JLabel.CENTER);
463       JLabel groupIdLabel = new JLabel(Res.string().getFaceGroupId(), JLabel.CENTER);
464       JLabel groupNameLabel = new JLabel(Res.string().getFaceGroupName(), JLabel.CENTER);
465       JLabel similaryLabel = new JLabel(Res.string().getSimilarity(), JLabel.CENTER);
466
467       Dimension dimension3 = new Dimension();
468       dimension3.height = 19;
469       dimension3.width = 80;
470       nameLabel.setPreferredSize(dimension3);
471       sexLabel2.setPreferredSize(dimension3);
472       birthdayLabel.setPreferredSize(dimension3);
473       idNoLabel.setPreferredSize(dimension3);
474       groupIdLabel.setPreferredSize(dimension3);
475       groupNameLabel.setPreferredSize(dimension3);
476       similaryLabel.setPreferredSize(dimension3);
477
478       nameTextField = new JTextField();
479       sexTextField2 = new JTextField();
480       birthdayTextField = new JTextField();
481       idNoTextField = new JTextField();
482       groupIdTextField = new JTextField();
483       groupNameTextField = new JTextField();
484       similaryTextField = new JTextField();
485
486       nameTextField.setHorizontalAlignment(JTextField.CENTER);
487       sexTextField2.setHorizontalAlignment(JTextField.CENTER);
488       birthdayTextField.setHorizontalAlignment(JTextField.CENTER);
489       idNoTextField.setHorizontalAlignment(JTextField.CENTER);
490       groupIdTextField.setHorizontalAlignment(JTextField.CENTER);
491       groupNameTextField.setHorizontalAlignment(JTextField.CENTER);
492       similaryTextField.setHorizontalAlignment(JTextField.CENTER);
493
494       nameTextField.setPreferredSize(dimension2);
495       sexTextField2.setPreferredSize(dimension2);
496       birthdayTextField.setPreferredSize(dimension2);
497       idNoTextField.setPreferredSize(dimension2);
498       groupIdTextField.setPreferredSize(dimension2);
499       groupNameTextField.setPreferredSize(dimension2);
500       similaryTextField.setPreferredSize(dimension2);
501
502       nameTextField.setFont(new Font("黑体", Font.PLAIN, 11));
503       sexTextField2.setFont(new Font("黑体", Font.PLAIN, 11));
504       birthdayTextField.setFont(new Font("黑体", Font.PLAIN, 11));
505       idNoTextField.setFont(new Font("黑体", Font.PLAIN, 11));
506       groupIdTextField.setFont(new Font("黑体", Font.PLAIN, 11));
507       groupNameTextField.setFont(new Font("黑体", Font.PLAIN, 11));
508       similaryTextField.setFont(new Font("黑体", Font.PLAIN, 11));
509
510       nameTextField.setEnabled(false);
511       sexTextField2.setEnabled(false);
512       birthdayTextField.setEnabled(false);
513       idNoTextField.setEnabled(false);
514       groupIdTextField.setEnabled(false);
515       groupNameTextField.setEnabled(false);
516       similaryTextField.setEnabled(false);
517
518       candidateDataPanel.setLayout(new FlowLayout());
519
520       candidateDataPanel.add(nameLabel);
521       candidateDataPanel.add(nameTextField);
522       candidateDataPanel.add(sexLabel2);
523       candidateDataPanel.add(sexTextField2);
524       candidateDataPanel.add(birthdayLabel);
525       candidateDataPanel.add(birthdayTextField);
526       candidateDataPanel.add(idNoLabel);
527       candidateDataPanel.add(idNoTextField);
528       candidateDataPanel.add(groupIdLabel);
529       candidateDataPanel.add(groupIdTextField);
530       candidateDataPanel.add(groupNameLabel);
531       candidateDataPanel.add(groupNameTextField);
532       candidateDataPanel.add(similaryLabel);
533       candidateDataPanel.add(similaryTextField);
534
535       // 预览
536       realplayBtn.addActionListener(
537           new ActionListener() {
538             @Override
539             public void actionPerformed(ActionEvent arg0) {
540               realplay();
541             }
542           });
543
544       // 订阅
545       attachBtn.addActionListener(
546           new ActionListener() {
547             @Override
548             public void actionPerformed(ActionEvent arg0) {
549               realLoadPicture();
550             }
551           });
552
553       // 人脸库操作
554       faceDataBaseBtn.addActionListener(
555           new ActionListener() {
556             @Override
557             public void actionPerformed(ActionEvent arg0) {
558               groupOperateDialog = new GroupOperateDialog();
559               groupOperateDialog.setVisible(true);
560             }
561           });
562
563       // 查询目标识别事件记录
564       faceEventRecordBtn.addActionListener(
565           new ActionListener() {
566             @Override
567             public void actionPerformed(ActionEvent arg0) {
568               FindFaceEventRecordDialog faceEventRecordDialog = new FindFaceEventRecordDialog();
569               faceEventRecordDialog.setVisible(true);
570             }
571           });
572       // 以图搜图
573       searchByPicBtn.addActionListener(
574           new ActionListener() {
575             @Override
576             public void actionPerformed(ActionEvent e) {
577               SearchByPicDialog dialog = new SearchByPicDialog();
578               dialog.setVisible(true);
579             }
580           });
581     }
582   }
583
584   // 预览
585   public void realplay() {
586     if (!isRealplay) {
587       m_hPlayHandle =
588           RealPlayModule.startRealPlay(chnComboBox.getSelectedIndex(), 0, realplayWindowPanel);
589       if (m_hPlayHandle.longValue() != 0) {
590         realplayWindowPanel.repaint();
591         isRealplay = true;
592         chnComboBox.setEnabled(false);
593         realplayBtn.setText(Res.string().getStopRealPlay());
594
595         TargetRecognitionModule.renderPrivateData(m_hPlayHandle, 1);
596       } else {
597         JOptionPane.showMessageDialog(
598             null,
599             ToolKits.getErrorCodeShow(),
600             Res.string().getErrorMessage(),
601             JOptionPane.ERROR_MESSAGE);
602       }
603     } else {
604       TargetRecognitionModule.renderPrivateData(m_hPlayHandle, 0);
605
606       RealPlayModule.stopRealPlay(m_hPlayHandle);
607       realplayWindowPanel.repaint();
608       isRealplay = false;
609       chnComboBox.setEnabled(true);
610       realplayBtn.setText(Res.string().getStartRealPlay());
611     }
612   }
613
614   // 订阅
615   public void realLoadPicture() {
616     if (!isAttach) {
617       m_hAttachHandle =
618           TargetRecognitionModule.realLoadPicture(
619               chnComboBox.getSelectedIndex(), AnalyzerDataCB.getInstance());
620       if (m_hAttachHandle.longValue() != 0) {
621         isAttach = true;
622         attachBtn.setText(Res.string().getDetach());
623       } else {
624         JOptionPane.showMessageDialog(
625             null,
626             ToolKits.getErrorCodeShow(),
627             Res.string().getErrorMessage(),
628             JOptionPane.ERROR_MESSAGE);
629       }
630     } else {
631       TargetRecognitionModule.stopRealLoadPicture(m_hAttachHandle);
632       isAttach = false;
633       attachBtn.setText(Res.string().getAttach());
634
635       globalPicLabel.setText(
636           Res.string().getGlobalPicture() + " ------ [" + Res.string().getEventType() + "]");
637
638       globalPicShowPanel.setOpaque(true);
639       globalPicShowPanel.repaint();
640
641       personPicShowPanel.setOpaque(true);
642       personPicShowPanel.repaint();
643
644       candidatePicShowPanel.setOpaque(true);
645       candidatePicShowPanel.repaint();
646
647       timeTextField.setText("");
648       sexTextField.setText("");
649       ageTextField.setText("");
650       eyeTextField.setText("");
651       mouthTextField.setText("");
652       maskTextField.setText("");
653       beardTextField.setText("");
654
655       nameTextField.setText("");
656       sexTextField2.setText("");
657       birthdayTextField.setText("");
658       idNoTextField.setText("");
659       groupIdTextField.setText("");
660       groupNameTextField.setText("");
661       similaryTextField.setText("");
662
663       groupId = 0;
664       globalBufferedImage = null;
665       personBufferedImage = null;
666       candidateBufferedImage = null;
667     }
668   }
669
670   /** 写成静态主要是防止被回收 */
671   private static class AnalyzerDataCB implements fAnalyzerDataCallBack {
672     private AnalyzerDataCB() {}
673
674     private static class AnalyzerDataCBHolder {
675       private static final AnalyzerDataCB instance = new AnalyzerDataCB();
676     }
677
678     public static AnalyzerDataCB getInstance() {
679       return AnalyzerDataCBHolder.instance;
680     }
681
682     public int invoke(
683         LLong lAnalyzerHandle,
684         int dwAlarmType,
685         Pointer pAlarmInfo,
686         Pointer pBuffer,
687         int dwBufSize,
688         Pointer dwUser,
689         int nSequence,
690         Pointer reserved) {
691       if (lAnalyzerHandle.longValue() == 0 || pAlarmInfo == null) {
692         return -1;
693       }
694
695       switch (dwAlarmType) {
696         case NetSDKLib.EVENT_IVS_FACERECOGNITION: // /< 目标识别事件
697           {
698             // DEV_EVENT_FaceRecognition_INFO 结构体比较大,new对象会比较耗时, ToolKits.GetPointerData内容拷贝是不耗时的。
699             // 如果多台设备或者事件处理比较频繁,可以考虑将 static DEV_EVENT_FaceRecognition_INFO msg = new
700             // DEV_EVENT_FaceRecognition_INFO(); 改为全局。
701             // 写成全局,是因为每次new花费时间较多, 如果改为全局,此case下的处理需要加锁
702             // 加锁,是因为共用一个对象,防止数据出错
703
704             // 耗时800ms左右
705             DEV_EVENT_FACERECOGNITION_INFO msg = new DEV_EVENT_FACERECOGNITION_INFO();
706
707             // 耗时20ms左右
708             ToolKits.GetPointerData(pAlarmInfo, msg);
709
710             // 保存图片,获取图片缓存
711             // 耗时20ms左右
712             try {
713               saveTargetRecognitionPic(pBuffer, dwBufSize, msg);
714             } catch (FileNotFoundException e) {
715               e.printStackTrace();
716             }
717
718             // 列表、图片界面显示
719             // 回调属于子线程,以下是个UI线程,来刷新UI
720             EventQueue.invokeLater(
721                 new TargetRecognitionRunnable(
722                     globalBufferedImage, personBufferedImage, candidateBufferedImage, msg, index));
723
724             // 释放内存
725             msg = null;
726             System.gc();
727
728             break;
729           }
730         case NetSDKLib.EVENT_IVS_FACEDETECT: // /<目标检测
731           {
732             DEV_EVENT_FACEDETECT_INFO msg = new DEV_EVENT_FACEDETECT_INFO();
733
734             ToolKits.GetPointerData(pAlarmInfo, msg);
735
736             // 保存图片,获取图片缓存
737             try {
738               saveFaceDetectPic(pBuffer, dwBufSize, msg);
739             } catch (FileNotFoundException e) {
740               e.printStackTrace();
741             }
742
743             // 列表、图片界面显示
744             EventQueue.invokeLater(
745                 new FaceDetectRunnable(globalBufferedImage, personBufferedImage, msg));
746
747             // 释放内存
748             msg = null;
749             System.gc();
750
751             break;
752           }
753         default:
754           break;
755       }
756
757       return 0;
758     }
759
760     /**
761      * 保存目标识别事件图片
762      *
763      * @param pBuffer 抓拍图片信息
764      * @param dwBufSize 抓拍图片大小
765      * @param TargetRecognitionInfo 目标识别事件信息
766      */
767     public void saveTargetRecognitionPic(
768         Pointer pBuffer, int dwBufSize, DEV_EVENT_FACERECOGNITION_INFO TargetRecognitionInfo)
769         throws FileNotFoundException {
770       index = -1;
771       globalBufferedImage = null;
772       personBufferedImage = null;
773       candidateBufferedImage = null;
774
775       File path = new File("./TargetRecognition/");
776       if (!path.exists()) {
777         path.mkdir();
778       }
779
780       if (pBuffer == null || dwBufSize <= 0) {
781         return;
782       }
783
784       /////////////// 保存全景图 ///////////////////
785       if (TargetRecognitionInfo.bGlobalScenePic == 1) {
786
787         String strGlobalPicPathName =
788             path + "\\" + TargetRecognitionInfo.UTC.toStringTitle() + "_TargetRecognition_Global.jpg";
789         byte[] bufferGlobal =
790             pBuffer.getByteArray(
791                 TargetRecognitionInfo.stuGlobalScenePicInfo.dwOffSet,
792                 TargetRecognitionInfo.stuGlobalScenePicInfo.dwFileLenth);
793         ByteArrayInputStream byteArrInputGlobal = new ByteArrayInputStream(bufferGlobal);
794
795         try {
796           globalBufferedImage = ImageIO.read(byteArrInputGlobal);
797           if (globalBufferedImage != null) {
798             File globalFile = new File(strGlobalPicPathName);
799             if (globalFile != null) {
800               ImageIO.write(globalBufferedImage, "jpg", globalFile);
801             }
802           }
803         } catch (IOException e2) {
804           e2.printStackTrace();
805         }
806       }
807
808       /////////////// 保存人脸图 /////////////////////////
809       if (TargetRecognitionInfo.stuObject.stPicInfo != null) {
810         String strPersonPicPathName =
811             path + "\\" + TargetRecognitionInfo.UTC.toStringTitle() + "_TargetRecognition_Person.jpg";
812         byte[] bufferPerson =
813             pBuffer.getByteArray(
814                 TargetRecognitionInfo.stuObject.stPicInfo.dwOffSet,
815                 TargetRecognitionInfo.stuObject.stPicInfo.dwFileLenth);
816         ByteArrayInputStream byteArrInputPerson = new ByteArrayInputStream(bufferPerson);
817
818         try {
819           personBufferedImage = ImageIO.read(byteArrInputPerson);
820           if (personBufferedImage != null) {
821             File personFile = new File(strPersonPicPathName);
822             if (personFile != null) {
823               ImageIO.write(personBufferedImage, "jpg", personFile);
824             }
825           }
826         } catch (IOException e2) {
827           e2.printStackTrace();
828         }
829       }
830
831       ///////////// 保存对比图 //////////////////////
832       if (TargetRecognitionInfo.nRetCandidatesExNum > 0
833           && TargetRecognitionInfo.stuCandidatesEx != null) {
834         int maxValue = -1;
835
836         // 设备可能返回多张图片,这里只显示相似度最高的
837         int[] nSimilary = new int[TargetRecognitionInfo.nRetCandidatesExNum];
838         for (int i = 0; i < TargetRecognitionInfo.nRetCandidatesExNum; i++) {
839           nSimilary[i] = TargetRecognitionInfo.stuCandidatesEx[i].bySimilarity & 0xff;
840         }
841
842         for (int i = 0; i < nSimilary.length; i++) {
843           if (maxValue < nSimilary[i]) {
844             maxValue = nSimilary[i];
845             index = i;
846           }
847         }
848
849         String strCandidatePicPathName =
850             path
851                 + "\\"
852                 + TargetRecognitionInfo.UTC.toStringTitle()
853                 + "_TargetRecognition_Candidate.jpg";
854
855         // 每个候选人的图片个数:TargetRecognitionInfo.stuCandidatesEx[index].stPersonInfo.wFacePicNum,
856         // 正常情况下只有1张。如果有多张,此demo只显示第一张
857         byte[] bufferCandidate =
858             pBuffer.getByteArray(
859                 TargetRecognitionInfo.stuCandidatesEx[index].stPersonInfo.szFacePicInfo[0].dwOffSet,
860                 TargetRecognitionInfo
861                     .stuCandidatesEx[index]
862                     .stPersonInfo
863                     .szFacePicInfo[0]
864                     .dwFileLenth);
865         ByteArrayInputStream byteArrInputCandidate = new ByteArrayInputStream(bufferCandidate);
866
867         try {
868           candidateBufferedImage = ImageIO.read(byteArrInputCandidate);
869           if (candidateBufferedImage != null) {
870             File candidateFile = new File(strCandidatePicPathName);
871             if (candidateFile != null) {
872               ImageIO.write(candidateBufferedImage, "jpg", candidateFile);
873             }
874           }
875         } catch (IOException e2) {
876           e2.printStackTrace();
877         }
878       }
879     }
880
881     /**
882      * 保存目标检测事件图片
883      *
884      * @param pBuffer 抓拍图片信息
885      * @param dwBufSize 抓拍图片大小
886      * @param faceDetectInfo 目标检测事件信息
887      */
888     public void saveFaceDetectPic(
889         Pointer pBuffer, int dwBufSize, DEV_EVENT_FACEDETECT_INFO faceDetectInfo)
890         throws FileNotFoundException {
891       File path = new File("./FaceDetection/");
892       if (!path.exists()) {
893         path.mkdir();
894       }
895
896       if (pBuffer == null || dwBufSize <= 0) {
897         return;
898       }
899
900       // 小图的 stuObject.nRelativeID 来匹配大图的 stuObject.nObjectID,来判断是不是 一起的图片
901       if (groupId != faceDetectInfo.stuObject.nRelativeID) { // /->保存全景图
902         personBufferedImage = null;
903         groupId = faceDetectInfo.stuObject.nObjectID;
904
905         String strGlobalPicPathName =
906             path + "\\" + faceDetectInfo.UTC.toStringTitle() + "_FaceDetection_Global.jpg";
907         byte[] bufferGlobal = pBuffer.getByteArray(0, dwBufSize);
908         ByteArrayInputStream byteArrInputGlobal = new ByteArrayInputStream(bufferGlobal);
909
910         try {
911           globalBufferedImage = ImageIO.read(byteArrInputGlobal);
912           if (globalBufferedImage != null) {
913             File globalFile = new File(strGlobalPicPathName);
914             if (globalFile != null) {
915               ImageIO.write(globalBufferedImage, "jpg", globalFile);
916             }
917           }
918         } catch (IOException e2) {
919           e2.printStackTrace();
920         }
921       } else if (groupId == faceDetectInfo.stuObject.nRelativeID) { // /->保存人脸图
922         if (faceDetectInfo.stuObject.stPicInfo != null) {
923           String strPersonPicPathName =
924               path + "\\" + faceDetectInfo.UTC.toStringTitle() + "_FaceDetection_Person.jpg";
925           byte[] bufferPerson = pBuffer.getByteArray(0, dwBufSize);
926           ByteArrayInputStream byteArrInputPerson = new ByteArrayInputStream(bufferPerson);
927
928           try {
929             personBufferedImage = ImageIO.read(byteArrInputPerson);
930             if (personBufferedImage != null) {
931               File personFile = new File(strPersonPicPathName);
932               if (personFile != null) {
933                 ImageIO.write(personBufferedImage, "jpg", personFile);
934               }
935             }
936           } catch (IOException e2) {
937             e2.printStackTrace();
938           }
939         }
940       }
941     }
942   }
943
944   private static class TargetRecognitionRunnable implements Runnable {
945     private BufferedImage globalBufferedImage;
946     private BufferedImage personBufferedImage;
947     private BufferedImage candidateBufferedImage;
948     private DEV_EVENT_FACERECOGNITION_INFO TargetRecognitionInfo;
949     private int index = -1;
950
951     public TargetRecognitionRunnable(
952         BufferedImage globalBufferedImage,
953         BufferedImage personBufferedImage,
954         BufferedImage candidateBufferedImage,
955         DEV_EVENT_FACERECOGNITION_INFO TargetRecognitionInfo,
956         int index) {
957
958       this.globalBufferedImage = globalBufferedImage;
959       this.personBufferedImage = personBufferedImage;
960       this.candidateBufferedImage = candidateBufferedImage;
961       this.TargetRecognitionInfo = TargetRecognitionInfo;
962       this.index = index;
963     }
964
965     @Override
966     public void run() {
967       if (!isAttach) {
968         return;
969       }
970
971       // 列表显示事件信息
972       showTargetRecognitionEventInfo(
973           globalBufferedImage,
974           personBufferedImage,
975           candidateBufferedImage,
976           TargetRecognitionInfo,
977           index);
978     }
979   }
980
981   private static class FaceDetectRunnable implements Runnable {
982     private BufferedImage globalBufferedImage = null;
983     private BufferedImage personBufferedImage = null;
984     private DEV_EVENT_FACEDETECT_INFO facedetectInfo = null;
985
986     public FaceDetectRunnable(
987         BufferedImage globalBufferedImage,
988         BufferedImage personBufferedImage,
989         DEV_EVENT_FACEDETECT_INFO facedetectInfo) {
990
991       this.globalBufferedImage = globalBufferedImage;
992       this.personBufferedImage = personBufferedImage;
993       this.facedetectInfo = facedetectInfo;
994     }
995
996     @Override
997     public void run() {
998       if (!isAttach) {
999         return;
1000       }
1001
1002       showFaceDetectEventInfo(globalBufferedImage, personBufferedImage, facedetectInfo);
1003     }
1004   }
1005
1006   private static void showTargetRecognitionEventInfo(
1007       BufferedImage globalBufferedImage,
1008       BufferedImage personBufferedImage,
1009       BufferedImage candidateBufferedImage,
1010       DEV_EVENT_FACERECOGNITION_INFO targetRecognitionInfo,
1011       int index) {
1012     globalPicLabel.setText(
1013         Res.string().getGlobalPicture()
1014             + " ------ ["
1015             + Res.string().getTargetRecognitionEvent()
1016             + "]");
1017
1018     // 全景图
1019     if (globalBufferedImage != null) {
1020       globalPicShowPanel.setImage(globalBufferedImage);
1021       globalPicShowPanel.setOpaque(false);
1022       globalPicShowPanel.repaint();
1023     } else {
1024       globalPicShowPanel.setOpaque(true);
1025       globalPicShowPanel.repaint();
1026     }
1027
1028     // 人脸图
1029     if (personBufferedImage != null) {
1030       personPicShowPanel.setImage(personBufferedImage);
1031       personPicShowPanel.setOpaque(false);
1032       personPicShowPanel.repaint();
1033     } else {
1034       personPicShowPanel.setOpaque(true);
1035       personPicShowPanel.repaint();
1036     }
1037
1038     // 候选人图
1039     if (candidateBufferedImage != null) {
1040       candidatePicShowPanel.setImage(candidateBufferedImage);
1041       candidatePicShowPanel.setOpaque(false);
1042       candidatePicShowPanel.repaint();
1043     } else {
1044       candidatePicShowPanel.setOpaque(true);
1045       candidatePicShowPanel.repaint();
1046     }
1047
1048     // 时间
1049     if (targetRecognitionInfo.UTC == null || targetRecognitionInfo.UTC.toString().equals("")) {
1050       timeTextField.setText("");
1051     } else {
1052       timeTextField.setText(targetRecognitionInfo.UTC.toString());
1053     }
1054
1055     // 人脸信息
1056     if (targetRecognitionInfo.stuFaceData == null) {
1057       sexTextField.setText("");
1058       ageTextField.setText("");
1059       eyeTextField.setText("");
1060       mouthTextField.setText("");
1061       maskTextField.setText("");
1062       beardTextField.setText("");
1063     } else {
1064       sexTextField.setText(Res.string().getSex(targetRecognitionInfo.stuFaceData.emSex));
1065       if (targetRecognitionInfo.stuFaceData.nAge == -1) {
1066         ageTextField.setText(Res.string().getUnKnow());
1067       } else {
1068         ageTextField.setText(String.valueOf(targetRecognitionInfo.stuFaceData.nAge));
1069       }
1070       eyeTextField.setText(Res.string().getEyeState(targetRecognitionInfo.stuFaceData.emEye));
1071       mouthTextField.setText(Res.string().getMouthState(targetRecognitionInfo.stuFaceData.emMouth));
1072       maskTextField.setText(Res.string().getMaskState(targetRecognitionInfo.stuFaceData.emMask));
1073       beardTextField.setText(Res.string().getBeardState(targetRecognitionInfo.stuFaceData.emBeard));
1074     }
1075
1076     // 候选人信息
1077     if (targetRecognitionInfo.nRetCandidatesExNum == 0 || index == -1) {
1078       nameTextField.setText("");
1079       sexTextField2.setText("");
1080       birthdayTextField.setText("");
1081       idNoTextField.setText("");
1082       groupIdTextField.setText("");
1083       groupNameTextField.setText("");
1084       similaryTextField.setText(Res.string().getStranger());
1085     } else {
1086       sexTextField2.setText(
1087           Res.string()
1088               .getSex(targetRecognitionInfo.stuCandidatesEx[index].stPersonInfo.bySex & 0xff));
1089       birthdayTextField.setText(
1090           String.valueOf((int) targetRecognitionInfo.stuCandidatesEx[index].stPersonInfo.wYear)
1091               + "-"
1092               + String.valueOf(
1093                   targetRecognitionInfo.stuCandidatesEx[index].stPersonInfo.byMonth & 0xff)
1094               + "-"
1095               + String.valueOf(
1096                   targetRecognitionInfo.stuCandidatesEx[index].stPersonInfo.byDay & 0xff));
1097
1098       try {
1099         nameTextField.setText(
1100             new String(targetRecognitionInfo.stuCandidatesEx[index].stPersonInfo.szPersonName, "GBK")
1101                 .trim());
1102         idNoTextField.setText(
1103             new String(targetRecognitionInfo.stuCandidatesEx[index].stPersonInfo.szID, "GBK").trim());
1104         groupIdTextField.setText(
1105             new String(targetRecognitionInfo.stuCandidatesEx[index].stPersonInfo.szGroupID, "GBK")
1106                 .trim());
1107         groupNameTextField.setText(
1108             new String(targetRecognitionInfo.stuCandidatesEx[index].stPersonInfo.szGroupName, "GBK")
1109                 .trim());
1110       } catch (UnsupportedEncodingException e) {
1111         e.printStackTrace();
1112       }
1113
1114       similaryTextField.setText(
1115           String.valueOf(targetRecognitionInfo.stuCandidatesEx[index].bySimilarity & 0xff));
1116     }
1117   }
1118
1119   private static void showFaceDetectEventInfo(
1120       BufferedImage globalBufferedImage,
1121       BufferedImage personBufferedImage,
1122       DEV_EVENT_FACEDETECT_INFO facedetectInfo) {
1123
1124     globalPicLabel.setText(
1125         Res.string().getGlobalPicture() + " ------ [" + Res.string().getTargetDetectEvent() + "]");
1126
1127     // 全景图
1128     if (globalBufferedImage != null) {
1129       globalPicShowPanel.setImage(globalBufferedImage);
1130       globalPicShowPanel.setOpaque(false);
1131       globalPicShowPanel.repaint();
1132     } else {
1133       globalPicShowPanel.setOpaque(true);
1134       globalPicShowPanel.repaint();
1135     }
1136
1137     // 人脸图
1138     if (personBufferedImage != null) {
1139       personPicShowPanel.setImage(personBufferedImage);
1140       personPicShowPanel.setOpaque(false);
1141       personPicShowPanel.repaint();
1142     } else {
1143       personPicShowPanel.setOpaque(true);
1144       personPicShowPanel.repaint();
1145     }
1146
1147     // 时间
1148     if (facedetectInfo.UTC == null || facedetectInfo.UTC.toString().equals("")) {
1149       timeTextField.setText("");
1150     } else {
1151       timeTextField.setText(facedetectInfo.UTC.toString());
1152     }
1153
1154     // 人脸信息
1155     sexTextField.setText(Res.string().getSex(facedetectInfo.emSex));
1156     if (facedetectInfo.nAge == -1) {
1157       ageTextField.setText(Res.string().getUnKnow());
1158     } else {
1159       ageTextField.setText(String.valueOf(facedetectInfo.nAge));
1160     }
1161     eyeTextField.setText(Res.string().getEyeState(facedetectInfo.emEye));
1162     mouthTextField.setText(Res.string().getMouthState(facedetectInfo.emMouth));
1163     maskTextField.setText(Res.string().getMaskState(facedetectInfo.emMask));
1164     beardTextField.setText(Res.string().getBeardState(facedetectInfo.emBeard));
1165
1166     // 候选人图和信息, 重绘清空
1167     candidatePicShowPanel.setOpaque(true);
1168     candidatePicShowPanel.repaint();
1169
1170     nameTextField.setText("");
1171     sexTextField2.setText("");
1172     birthdayTextField.setText("");
1173     idNoTextField.setText("");
1174     groupIdTextField.setText("");
1175     groupNameTextField.setText("");
1176     similaryTextField.setText("");
1177   }
1178
1179   private void setEnable(boolean bln) {
1180
1181     chnComboBox.setEnabled(bln);
1182     realplayBtn.setEnabled(bln);
1183     attachBtn.setEnabled(bln);
1184     faceDataBaseBtn.setEnabled(bln);
1185     faceEventRecordBtn.setEnabled(bln);
1186     searchByPicBtn.setEnabled(bln);
1187   }
1188
1189   /*
1190    * 登录
1191    */
1192   private LoginPanel loginPanel;
1193
1194   /*
1195    * 预览
1196    */
1197   private JLabel chnlabel;
1198   private JComboBox chnComboBox;
1199   private JButton realplayBtn;
1200   private JButton attachBtn;
1201   private JButton faceDataBaseBtn;
1202   private JButton faceEventRecordBtn;
1203   /** 以图搜图按钮 */
1204   private JButton searchByPicBtn;
1205
1206   private Panel realplayWindowPanel;
1207   private static PaintPanel globalPicShowPanel;
1208   private static PaintPanel personPicShowPanel;
1209   private static PaintPanel candidatePicShowPanel;
1210
1211   private static JLabel globalPicLabel;
1212
1213   /*
1214    * 人脸信息
1215    */
1216   private static JTextField timeTextField;
1217   private static JTextField sexTextField;
1218   private static JTextField ageTextField;
1219   private static JTextField eyeTextField;
1220   private static JTextField mouthTextField;
1221   private static JTextField maskTextField;
1222   private static JTextField beardTextField;
1223
1224   /*
1225    * 候选人信息
1226    */
1227   private static JTextField nameTextField;
1228   private static JTextField sexTextField2;
1229   private static JTextField birthdayTextField;
1230   private static JTextField idNoTextField;
1231   private static JTextField groupIdTextField;
1232   private static JTextField groupNameTextField;
1233   private static JTextField similaryTextField;
1234 }
1235
1236 public class TargetRecognition {
1237   public static void main(String[] args) {
1238     SwingUtilities.invokeLater(
1239         new Runnable() {
1240           public void run() {
1241             TargetRecognitionFrame demo = new TargetRecognitionFrame();
1242             demo.setVisible(true);
1243           }
1244         });
1245   }
1246 }