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
package com.iailab.netsdk.demo.frame.Gate;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
 
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
 
import com.sun.jna.Memory;
 
import com.iailab.netsdk.common.BorderEx;
import com.iailab.netsdk.common.DateChooserJButton;
import com.iailab.netsdk.common.PaintPanel;
import com.iailab.netsdk.common.Res;
import com.iailab.netsdk.demo.module.GateModule;
import com.iailab.netsdk.lib.ToolKits;
 
public class AddCardDialog extends JDialog{
 
    private static final long serialVersionUID = 1L;
    
    private Memory memory = null;
    
    private String picPath = "";
 
    public AddCardDialog(){
        setTitle(Res.string().getAdd() + Res.string().getCardInfo());
        setLayout(new BorderLayout());
        setModal(true);
        pack();
        setSize(520, 390);
        setResizable(false);
        setLocationRelativeTo(null);    
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);   // 释放窗体
        
        CardInfoPanel cardInfoPanel = new CardInfoPanel();
        ImagePanel imagePanel = new ImagePanel();
        
        add(cardInfoPanel, BorderLayout.CENTER);
        add(imagePanel, BorderLayout.EAST);
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                clear();
                dispose();
            }
        });
    }
    
    /**
     * 卡信息
     */
    private class CardInfoPanel extends JPanel {
 
        private static final long serialVersionUID = 1L;
        
        public CardInfoPanel() {
            BorderEx.set(this, Res.string().getCardInfo(), 4);
            setLayout(new FlowLayout());
            
            JLabel cardNoLabel = new JLabel(Res.string().getCardNo() + ":", JLabel.CENTER);
            JLabel userIdLabel = new JLabel(Res.string().getUserId() + ":", JLabel.CENTER);
            JLabel cardNameLabel = new JLabel(Res.string().getCardName() + ":", JLabel.CENTER);
            JLabel cardPasswdLabel = new JLabel(Res.string().getCardPassword() + ":", JLabel.CENTER);
            JLabel cardStatusLabel = new JLabel(Res.string().getCardStatus() + ":", JLabel.CENTER);
            JLabel cardTypeLabel = new JLabel(Res.string().getCardType() + ":", JLabel.CENTER);
            JLabel useTimesLabel = new JLabel(Res.string().getUseTimes() + ":", JLabel.CENTER);
            JLabel validPeriodLabel = new JLabel(Res.string().getValidPeriod() + ":", JLabel.CENTER);
            
            Dimension dimension = new Dimension();
            dimension.width = 85;
            dimension.height = 20;
            cardNoLabel.setPreferredSize(dimension);
            userIdLabel.setPreferredSize(dimension);
            cardNameLabel.setPreferredSize(dimension);
            cardPasswdLabel.setPreferredSize(dimension);
            cardStatusLabel.setPreferredSize(dimension);
            cardTypeLabel.setPreferredSize(dimension);
            useTimesLabel.setPreferredSize(dimension);
            validPeriodLabel.setPreferredSize(dimension);
            
            cardNoTextField = new JTextField();
            userIdTextField = new JTextField();
            cardNameTextField = new JTextField();
            cardPasswdField = new JPasswordField();        
            cardStatusComboBox = new JComboBox(Res.string().getCardStatusList());
            cardTypeComboBox = new JComboBox(Res.string().getCardTypeList());    
            useTimesTextField = new JTextField("0");
            firstEnterCheckBox = new JCheckBox(Res.string().getIsFirstEnter());
            enableCheckBox = new JCheckBox(Res.string().getEnable());
            enableCheckBox.setSelected(true);
            enableCheckBox.setVisible(false);
            startTimeBtn = new DateChooserJButton();
            endTimeBtn = new DateChooserJButton();
            
            cardNoTextField.setPreferredSize(new Dimension(145, 20));
            userIdTextField.setPreferredSize(new Dimension(145, 20));
            cardNameTextField.setPreferredSize(new Dimension(145, 20));
            cardPasswdField.setPreferredSize(new Dimension(145, 20));
            useTimesTextField.setPreferredSize(new Dimension(145, 20));    
            cardStatusComboBox.setPreferredSize(new Dimension(145, 20));
            cardTypeComboBox.setPreferredSize(new Dimension(145, 20));
            startTimeBtn.setPreferredSize(new Dimension(145, 20));
            endTimeBtn.setPreferredSize(new Dimension(145, 20));
            firstEnterCheckBox.setPreferredSize(new Dimension(170, 20));
            enableCheckBox.setPreferredSize(new Dimension(70, 20));
            
            JLabel nullLabel1 = new JLabel();
            JLabel nullLabel2 = new JLabel();
            JLabel nullLabel3 = new JLabel();
            nullLabel1.setPreferredSize(new Dimension(5, 20));
            nullLabel2.setPreferredSize(new Dimension(30, 20));
            nullLabel3.setPreferredSize(new Dimension(85, 20));
 
            addBtn = new JButton(Res.string().getAdd());
            cancelBtn = new JButton(Res.string().getCancel());
            JLabel nullLabel4 = new JLabel();
            nullLabel4.setPreferredSize(new Dimension(250, 20));
            addBtn.setPreferredSize(new Dimension(110, 20));
            cancelBtn.setPreferredSize(new Dimension(110, 20));
            
            add(cardNoLabel);
            add(cardNoTextField);
            add(userIdLabel);
            add(userIdTextField);
            
            add(cardNameLabel);
            add(cardNameTextField);
            add(cardPasswdLabel);
            add(cardPasswdField);
            
            add(cardStatusLabel);
            add(cardStatusComboBox);
            add(cardTypeLabel);
            add(cardTypeComboBox);
            
            add(useTimesLabel);
            add(useTimesTextField);
            add(nullLabel1);
            add(firstEnterCheckBox);
            add(nullLabel2);
            add(enableCheckBox);
            
            add(validPeriodLabel);
            add(startTimeBtn);
            add(nullLabel3);
            add(endTimeBtn);
            
            add(nullLabel4);
            add(addBtn);
            add(cancelBtn);
            
            // 添加
            addBtn.addActionListener(new ActionListener() {        
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if(cardNoTextField.getText().isEmpty()) {
                        JOptionPane.showMessageDialog(null, Res.string().getInputCardNo(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    
                    if(userIdTextField.getText().isEmpty()) {
                        JOptionPane.showMessageDialog(null, Res.string().getInputUserId(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    
 
                    
                    if(memory == null) {
                        JOptionPane.showMessageDialog(null, Res.string().getSelectPicture(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    
                    try {
                        if (cardNoTextField.getText().getBytes("UTF-8").length > 31) {
                            JOptionPane.showMessageDialog(null, Res.string().getCardNoExceedLength() + "(31)", Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        
                        if (userIdTextField.getText().getBytes("UTF-8").length > 31) {
                            JOptionPane.showMessageDialog(null, Res.string().getUserIdExceedLength() + "(31)", Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        
                        if (cardNameTextField.getText().getBytes("UTF-8").length > 63) {
                            JOptionPane.showMessageDialog(null, Res.string().getCardNameExceedLength() + "(63)", Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        
                        if (new String(cardPasswdField.getPassword()).getBytes("UTF-8").length > 63) {
                            JOptionPane.showMessageDialog(null, Res.string().getCardPasswdExceedLength() + "(63)", Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }                                 
                    
                    // 先添加卡,卡添加成功后,再添加图片
                    int useTimes = 0;
                    if(useTimesTextField.getText().isEmpty()) {
                        useTimes = 0;
                    } else {
                        useTimes = Integer.parseInt(useTimesTextField.getText());
                    }
                    
                    boolean bCardFlags = GateModule.insertCard(cardNoTextField.getText(), userIdTextField.getText(), cardNameTextField.getText(), 
                            new String(cardPasswdField.getPassword()), Res.string().getCardStatusInt(cardStatusComboBox.getSelectedIndex()), 
                            Res.string().getCardTypeInt(cardTypeComboBox.getSelectedIndex()), useTimes, 
                            firstEnterCheckBox.isSelected() ? 1:0, enableCheckBox.isSelected() ? 1:0, startTimeBtn.getText(), endTimeBtn.getText());
                    String cardError = "";
                    if(!bCardFlags) {
                        cardError = ToolKits.getErrorCodeShow();
                    }
                    
                    
                    boolean bFaceFalgs = GateModule.addFaceInfo(userIdTextField.getText(), memory);
                    String faceError = "";
                    if(!bFaceFalgs) {
                        faceError = ToolKits.getErrorCodeShow();
                    }
 
                    // 添加卡信息和人脸成功
                    if(bCardFlags && bFaceFalgs) {
                        JOptionPane.showMessageDialog(null, Res.string().getSucceedAddCardAndPerson(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
                        dispose();
                    }
                    
                    // 添加卡信息和人脸失败
                    if(!bCardFlags && !bFaceFalgs) {
                        JOptionPane.showMessageDialog(null, Res.string().getFailedAddCard() + " : " + cardError, 
                                                            Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                    }
                    
                    // 添加卡信息成功,添加人脸失败
                    if(bCardFlags && !bFaceFalgs) {
                        JOptionPane.showMessageDialog(null, Res.string().getSucceedAddCardButFailedAddPerson() + " : " + faceError, Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                    }
                    
                    // 卡信息已存在,添加人脸成功
                    if(!bCardFlags && bFaceFalgs) {
                        JOptionPane.showMessageDialog(null, Res.string().getCardExistedSucceedAddPerson(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            });
            
            // 取消
            cancelBtn.addActionListener(new ActionListener() {        
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    clear();
                    dispose();                
                }
            });
        }
    }
    
    /**
     * 选择图片
     */
    private class ImagePanel extends JPanel {
 
        private static final long serialVersionUID = 1L;
        
        public ImagePanel() {
            BorderEx.set(this, Res.string().getPersonPicture(), 4);
            Dimension dimension = new Dimension();
            dimension.width = 250;
            setPreferredSize(dimension);
            setLayout(new BorderLayout());
            
            addImagePanel = new PaintPanel();   // 添加的人员信息图片显示
            selectImageBtn = new JButton(Res.string().getSelectPicture());
            add(addImagePanel, BorderLayout.CENTER);
            add(selectImageBtn, BorderLayout.SOUTH);
            
            // 选择图片,获取图片的信息
            selectImageBtn.addActionListener(new ActionListener() {                
                @Override
                public void actionPerformed(ActionEvent arg0) {        
                    // 选择图片,获取图片路径,并在界面显示
                    picPath = ToolKits.openPictureFile(addImagePanel);
                            
                    if(!picPath.isEmpty()) {
                        try {
                            memory = ToolKits.readPictureFile(picPath);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
            
                }
            });
        }
    }
    
    private void clear() {
        memory = null;
        picPath = "";
    }
    
    private PaintPanel addImagePanel;
    private JButton selectImageBtn;
    
    private JTextField cardNoTextField;
    private JTextField userIdTextField;
    private JTextField cardNameTextField;
    private JPasswordField cardPasswdField;        
    private JComboBox cardStatusComboBox;
    private JComboBox cardTypeComboBox;    
    private JTextField useTimesTextField;
    private JCheckBox firstEnterCheckBox;
    private JCheckBox enableCheckBox;
    private DateChooserJButton startTimeBtn;
    private DateChooserJButton endTimeBtn;
    
    private JButton addBtn;
    private JButton cancelBtn;
}