潘志宝
2024-12-10 29b971556c85da59d9f820dce13f8bce4d52056a
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.demo.frame.TargetRecognition;
H 2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.GridLayout;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.awt.event.WindowAdapter;
10 import java.awt.event.WindowEvent;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.Vector;
14
15 import javax.swing.JButton;
16 import javax.swing.JComboBox;
17 import javax.swing.JDialog;
18 import javax.swing.JLabel;
19 import javax.swing.JOptionPane;
20 import javax.swing.JPanel;
21 import javax.swing.JScrollPane;
22 import javax.swing.JTable;
23 import javax.swing.JTextField;
24 import javax.swing.table.DefaultTableCellRenderer;
25 import javax.swing.table.DefaultTableModel;
26
27 import com.iailab.netsdk.common.*;
28 import com.iailab.netsdk.demo.module.LoginModule;
29 import com.iailab.netsdk.demo.module.TargetRecognitionModule;
30 import com.iailab.netsdk.lib.ToolKits;
31 import com.iailab.netsdk.lib.NetSDKLib.NET_FACERECONGNITION_GROUP_INFO;
32
33 public class DispositionOperateDialog extends JDialog {
34     /**
35      * 
36      */
37     private static final long serialVersionUID = 1L;
38     
39     // 通道列表
40     private Vector<String> chnlist = new Vector<String>(); 
41     
42     // 通道个数
43     private int nChn = LoginModule.m_stDeviceInfo.byChanNum;
44     
45     // 通道列表, 用于布控
46     private ArrayList< Integer> arrayList = new ArrayList<Integer>();
47     
48     // key:通道     value:相似度, 用于撤控
49     private HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
50     
51     String groupId = "";
52     String groupName = "";
53
54     public DispositionOperateDialog(String groupId, String groupName) {
55         setTitle(Res.string().getDisposition() + "/" + Res.string().getDelDisposition());
56         setLayout(new BorderLayout());
57         setModal(true);   
58         pack();
59         setSize(450, 400);
60         setResizable(false);
61         setLocationRelativeTo(null); 
62         setDefaultCloseOperation(DISPOSE_ON_CLOSE);   // 释放窗体
63         
64         for(int i = 1; i < nChn + 1; i++) {
65             chnlist.add(Res.string().getChannel() + " " + String.valueOf(i));
66         }
67         
68         this.groupId = groupId;
69         this.groupName = groupName;
70         
71         DispositionListPanel dispositionListPanel = new DispositionListPanel();
72         DispositionInfoPanel dispositionInfoPanel = new DispositionInfoPanel();
73         
74         add(dispositionListPanel, BorderLayout.CENTER);
75         add(dispositionInfoPanel, BorderLayout.NORTH);
76         
77         findChnAndSimilary();
78         
79         addWindowListener(new WindowAdapter() {
80             public void windowClosing(WindowEvent e) {
81                 dispose();
82             }
83         });
84     }
85     
86     /*
87      * 布控显示列表
88      */
89     private class DispositionListPanel extends JPanel {
90         private static final long serialVersionUID = 1L;
91         public DispositionListPanel() {
92             BorderEx.set(this, "", 2);
93             setLayout(new BorderLayout());
94             
95             JPanel panel = new JPanel();
96             JPanel panel2 = new JPanel();
97             JPanel panel3 = new JPanel();
98             
99             Dimension dimension = new Dimension();
100             dimension.width = 145;
101             panel.setPreferredSize(dimension);
102             panel.setLayout(new BorderLayout());
103             panel.add(panel2, BorderLayout.NORTH);
104             panel.add(panel3, BorderLayout.SOUTH);
105             
106             addBtn = new JButton(Res.string().getAdd());
107             refreshBtn = new JButton(Res.string().getFresh());
108             panel2.setLayout(new GridLayout(2, 1));
109             panel2.add(addBtn);
110             panel2.add(refreshBtn);
111             
112             dispositionBtn = new JButton(Res.string().getDisposition());
113             delDispositionBtn = new JButton(Res.string().getDelDisposition());
114             panel3.setLayout(new GridLayout(2, 1));
115             panel3.add(dispositionBtn);
116             panel3.add(delDispositionBtn);
117         
118             data = new Object[512][2];
119             defaultTableModel = new DefaultTableModel(data, Res.string().getDispositionTable());
120             table = new JTable(defaultTableModel){   // 列表不可编辑
121                 private static final long serialVersionUID = 1L;
122                 @Override
123                 public boolean isCellEditable(int row, int column) {
124                     return false;
125                 }
126             };
127             
128             DefaultTableCellRenderer dCellRenderer = new DefaultTableCellRenderer();
129             dCellRenderer.setHorizontalAlignment(JLabel.CENTER);
130             table.setDefaultRenderer(Object.class, dCellRenderer);
131             
132             add(new JScrollPane(table), BorderLayout.CENTER);
133             add(panel, BorderLayout.EAST);
134
135             // 添加通道和相似度到列表
136             addBtn.addActionListener(new ActionListener() {            
137                 @Override
138                 public void actionPerformed(ActionEvent arg0) {
139                     boolean isExit = false;
140                     
141                     String chn = String.valueOf(chnComboBox.getSelectedIndex() + 1);
142                     String similary = similaryTextField.getText();
143                     
144                     if(similaryTextField.getText().equals("") 
145                             || Integer.parseInt(similaryTextField.getText()) > 100) {                        
146                         JOptionPane.showMessageDialog(null, Res.string().getSimilarityRange(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);                    
147                         return;
148                     }
149                     
150                     // 如果存在了通道号,列表修改相应的相似度
151                     for(int i = 0; i < nChn; i++) {
152                         if(chn.equals(String.valueOf(defaultTableModel.getValueAt(i, 0)).trim())) {
153                             defaultTableModel.setValueAt(similary, i, 1);
154                             isExit = true;
155                             break;
156                         }
157                     }
158                     
159                     if(isExit) {
160                         return;
161                     }
162                     
163                     // 如果不存在通道号,按顺序添加人列表
164                     for(int i = 0; i < nChn; i++) {
165                         if(String.valueOf(defaultTableModel.getValueAt(i, 0)).trim().equals("") 
166                                 || defaultTableModel.getValueAt(i, 0) == null) {
167                             defaultTableModel.setValueAt(chn, i, 0);
168                             defaultTableModel.setValueAt(similary, i, 1);
169                             break;
170                         }
171                     }                        
172                 }
173             });
174             
175             // 刷新已布控的通道和相似度信息
176             refreshBtn.addActionListener(new ActionListener() {            
177                 @Override
178                 public void actionPerformed(ActionEvent arg0) {
179                     findChnAndSimilary();            
180                 }
181             });
182             
183             // 布控
184             dispositionBtn.addActionListener(new ActionListener() {            
185                 @Override
186                 public void actionPerformed(ActionEvent arg0) {
187                     hashMap.clear();
188                     
189                     // 获取列表里的数据
190                     for(int i = 0; i < nChn; i++) {
191                         // 判断通道号是否为空
192                         if(!String.valueOf(defaultTableModel.getValueAt(i, 0)).trim().equals("") 
193                                 && defaultTableModel.getValueAt(i, 0) != null) {        
194                             
195                             // 判断相似度是否为空
196                             if(!String.valueOf(defaultTableModel.getValueAt(i, 1)).trim().equals("") 
197                                 && defaultTableModel.getValueAt(i, 1) != null) {
198                                 hashMap.put(Integer.parseInt(String.valueOf(defaultTableModel.getValueAt(i, 0)).trim()), 
199                                         Integer.parseInt(String.valueOf(defaultTableModel.getValueAt(i, 1)).trim()));
200                             } else {
201                                 hashMap.put(Integer.parseInt(String.valueOf(defaultTableModel.getValueAt(i, 0)).trim()), 0);
202                             }
203                         } 
204                     }
205                     System.out.println("size:" + hashMap.size());
206                     if(hashMap.size() == 0) {
207                         JOptionPane.showMessageDialog(null, Res.string().getAddDispositionInfo(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);    
208                         return;
209                     }
210                     
211                     if(!TargetRecognitionModule.putDisposition(goroupIdTextField.getText(), hashMap)) {
212                         JOptionPane.showMessageDialog(null, Res.string().getFailed() + "," + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);    
213                     } else {
214                         JOptionPane.showMessageDialog(null, Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
215                     }
216                     
217                     // 刷新列表
218                     findChnAndSimilary();        
219                 }
220             });
221             
222             delDispositionBtn.addActionListener(new ActionListener() {            
223                 @Override
224                 public void actionPerformed(ActionEvent arg0) {
225                     arrayList.clear();
226                     
227                     // 获取所有选中行数
228                     int[] rows = null;
229                     rows = table.getSelectedRows();
230
231                     if(rows == null) {
232                         JOptionPane.showMessageDialog(null, Res.string().getSelectDelDispositionInfo(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
233                         return;
234                     }
235                     
236                     // 获取所有选中,非空的通道号
237                     for(int i = 0; i < rows.length; i++) {
238                         if(!String.valueOf(defaultTableModel.getValueAt(rows[i], 0)).trim().equals("") 
239                                 && defaultTableModel.getValueAt(rows[i], 0) != null) {
240                             arrayList.add(Integer.parseInt(String.valueOf(defaultTableModel.getValueAt(rows[i], 0)).trim()));
241                         }    
242                     }
243                     
244                     if(arrayList.size() == 0) {
245                         JOptionPane.showMessageDialog(null, Res.string().getSelectDelDispositionInfo(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
246                         return;
247                     }
248                     
249                     if(!TargetRecognitionModule.delDisposition(goroupIdTextField.getText(), arrayList)) {
250                         JOptionPane.showMessageDialog(null, Res.string().getFailed() + "," + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);    
251                     } else {
252                         JOptionPane.showMessageDialog(null, Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
253                     }
254                     
255                     // 刷新列表
256                     findChnAndSimilary();                
257                 }
258             });
259         }
260     }
261     
262     /*
263      * 布控信息    
264      */
265     private class DispositionInfoPanel extends JPanel {
266         /**
267          * 
268          */
269         private static final long serialVersionUID = 1L;
270         public DispositionInfoPanel() {    
271             BorderEx.set(this, "", 2);
272             setLayout(new FlowLayout());
273             
274             Dimension dimension = new Dimension();
275             dimension.height = 80;
276             setPreferredSize(dimension);
277             
278             JLabel goroupIdLabel = new JLabel(Res.string().getFaceGroupId(), JLabel.CENTER);
279             JLabel goroupNameLabel = new JLabel(Res.string().getFaceGroupName(), JLabel.CENTER);
280             JLabel chnLabel = new JLabel(Res.string().getChannel(), JLabel.CENTER);
281             JLabel similaryLabel = new JLabel(Res.string().getSimilarity(), JLabel.CENTER);
282             
283             goroupIdTextField = new JTextField();
284             goroupNameTextField = new JTextField();
285             chnComboBox = new JComboBox(chnlist);    
286             similaryTextField = new JTextField();
287             
288             Dimension dimension1 = new Dimension();
289             dimension1.width = 80;
290             dimension1.height = 20;
291             goroupIdLabel.setPreferredSize(dimension1);
292             goroupNameLabel.setPreferredSize(dimension1);
293             chnLabel.setPreferredSize(dimension1);
294             similaryLabel.setPreferredSize(dimension1);
295             goroupIdTextField.setPreferredSize(new Dimension(120, 20));
296             goroupNameTextField.setPreferredSize(new Dimension(120, 20));
297             chnComboBox.setPreferredSize(new Dimension(120, 20));
298             similaryTextField.setPreferredSize(new Dimension(120, 20));
299     
300             add(goroupIdLabel);
301             add(goroupIdTextField);
302             add(goroupNameLabel);
303             add(goroupNameTextField);
304             add(chnLabel);
305             add(chnComboBox);
306             add(similaryLabel);
307             add(similaryTextField);
308             
309             ToolKits.limitTextFieldLength(similaryTextField, 3);
310             
311             goroupIdTextField.setEditable(false);
312             goroupNameTextField.setEditable(false);
313             
314             goroupIdTextField.setText(groupId);
315             goroupNameTextField.setText(groupName);
316         }
317     }
318     
319     // 查找人脸库的布控通道以及对应的相似度
320     private void findChnAndSimilary() {
321         // 清空列表
322         defaultTableModel.setRowCount(0);
323         defaultTableModel.setRowCount(512);
324         
325         // 查询布控信息
326         NET_FACERECONGNITION_GROUP_INFO[] groupInfos = TargetRecognitionModule.findGroupInfo(goroupIdTextField.getText());
327         
328         if(groupInfos == null) {
329             return;
330         }
331         
332         for(int i = 0; i < groupInfos[0].nRetChnCount; i++) {
333             defaultTableModel.setValueAt(String.valueOf(groupInfos[0].nChannel[i] + 1), i, 0);
334             defaultTableModel.setValueAt(String.valueOf(groupInfos[0].nSimilarity[i]), i, 1);
335         }            
336     }
337     
338     private Object[][] data;
339     private DefaultTableModel defaultTableModel;
340     private JTable table;
341     
342     private JButton addBtn;
343     private JButton refreshBtn;
344     private JButton dispositionBtn;
345     private JButton delDispositionBtn;
346     private JTextField goroupIdTextField;
347     private JTextField goroupNameTextField;
348     private JComboBox chnComboBox;
349     private JTextField similaryTextField;
350 }