-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnrollment.java
executable file
·299 lines (255 loc) · 8.44 KB
/
Enrollment.java
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
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.digitalpersona.uareu.*;
public class Enrollment
extends JPanel
implements ActionListener
{
public class EnrollmentThread
extends Thread
implements Engine.EnrollmentCallback
{
public static final String ACT_PROMPT = "enrollment_prompt";
public static final String ACT_CAPTURE = "enrollment_capture";
public static final String ACT_FEATURES = "enrollment_features";
public static final String ACT_DONE = "enrollment_done";
public static final String ACT_CANCELED = "enrollment_canceled";
public class EnrollmentEvent extends ActionEvent{
private static final long serialVersionUID = 102;
public Reader.CaptureResult capture_result;
public Reader.Status reader_status;
public UareUException exception;
public Fmd enrollment_fmd;
public EnrollmentEvent(Object source, String action, Fmd fmd, Reader.CaptureResult cr, Reader.Status st, UareUException ex){
super(source, ActionEvent.ACTION_PERFORMED, action);
capture_result = cr;
reader_status = st;
exception = ex;
enrollment_fmd = fmd;
}
}
private final Reader m_reader;
private CaptureThread m_capture;
private ActionListener m_listener;
private boolean m_bCancel;
protected EnrollmentThread(Reader reader, ActionListener listener){
m_reader = reader;
m_listener = listener;
}
public Engine.PreEnrollmentFmd GetFmd(Fmd.Format format){
Engine.PreEnrollmentFmd prefmd = null;
while(null == prefmd && !m_bCancel){
//start capture thread
m_capture = new CaptureThread(m_reader, false, Fid.Format.ANSI_381_2004, Reader.ImageProcessing.IMG_PROC_DEFAULT);
m_capture.start(null);
//prompt for finger
SendToListener(ACT_PROMPT, null, null, null, null);
//wait till done
m_capture.join(0);
//check result
CaptureThread.CaptureEvent evt = m_capture.getLastCaptureEvent();
if(null != evt.capture_result){
if(Reader.CaptureQuality.CANCELED == evt.capture_result.quality){
//capture canceled, return null
break;
}
else if(null != evt.capture_result.image && Reader.CaptureQuality.GOOD == evt.capture_result.quality){
//acquire engine
Engine engine = UareUGlobal.GetEngine();
try{
//extract features
Fmd fmd = engine.CreateFmd(evt.capture_result.image, Fmd.Format.ANSI_378_2004);
//return prefmd
prefmd = new Engine.PreEnrollmentFmd();
prefmd.fmd = fmd;
prefmd.view_index = 0;
//send success
SendToListener(ACT_FEATURES, null, null, null, null);
}
catch(UareUException e){
//send extraction error
SendToListener(ACT_FEATURES, null, null, null, e);
}
}
else{
//send quality result
SendToListener(ACT_CAPTURE, null, evt.capture_result, evt.reader_status, evt.exception);
}
}
else{
//send capture error
SendToListener(ACT_CAPTURE, null, evt.capture_result, evt.reader_status, evt.exception);
}
}
return prefmd;
}
public void cancel(){
m_bCancel = true;
if(null != m_capture) m_capture.cancel();
}
private void SendToListener(String action, Fmd fmd, Reader.CaptureResult cr, Reader.Status st, UareUException ex){
if(null == m_listener || null == action || action.equals("")) return;
final EnrollmentEvent evt = new EnrollmentEvent(this, action, fmd, cr, st, ex);
//invoke listener on EDT thread
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
m_listener.actionPerformed(evt);
}
});
}
catch (InvocationTargetException e) { e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
}
public void run(){
//acquire engine
Engine engine = UareUGlobal.GetEngine();
try{
m_bCancel = false;
while(!m_bCancel){
//run enrollment
Fmd fmd = engine.CreateEnrollmentFmd(Fmd.Format.ANSI_378_2004, this);
//send result
if(null != fmd){
SendToListener(ACT_DONE, fmd, null, null, null);
}
else{
SendToListener(ACT_CANCELED, null, null, null, null);
break;
}
}
}
catch(UareUException e){
SendToListener(ACT_DONE, null, null, null, e);
}
}
}
private static final long serialVersionUID = 6;
private static final String ACT_BACK = "back";
private EnrollmentThread m_enrollment;
private Reader m_reader;
private JDialog m_dlgParent;
private JTextArea m_text;
private boolean m_bJustStarted;
private Enrollment(Reader reader){
m_reader = reader;
m_bJustStarted = true;
m_enrollment = new EnrollmentThread(m_reader, this);
final int vgap = 5;
final int width = 380;
BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
setLayout(layout);
m_text = new JTextArea(22, 1);
m_text.setEditable(false);
JScrollPane paneReader = new JScrollPane(m_text);
add(paneReader);
Dimension dm = paneReader.getPreferredSize();
dm.width = width;
paneReader.setPreferredSize(dm);
add(Box.createVerticalStrut(vgap));
JButton btnBack = new JButton("Back");
btnBack.setActionCommand(ACT_BACK);
btnBack.addActionListener(this);
add(btnBack);
add(Box.createVerticalStrut(vgap));
setOpaque(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals(ACT_BACK)){
//destroy dialog to cancel enrollment
m_dlgParent.setVisible(false);
}
else{
EnrollmentThread.EnrollmentEvent evt = (EnrollmentThread.EnrollmentEvent)e;
if(e.getActionCommand().equals(EnrollmentThread.ACT_PROMPT)){
if(m_bJustStarted){
m_text.append("Enrollment started\n");
m_text.append(" put any finger on the reader\n");
}
else{
m_text.append(" put the same finger on the reader\n");
}
m_bJustStarted = false;
}
else if(e.getActionCommand().equals(EnrollmentThread.ACT_CAPTURE)){
if(null != evt.capture_result){
MessageBox.BadQuality(evt.capture_result.quality);
}
else if(null != evt.exception){
MessageBox.DpError("Capture", evt.exception);
}
else if(null != evt.reader_status){
MessageBox.BadStatus(evt.reader_status);
}
m_bJustStarted = false;
}
else if(e.getActionCommand().equals(EnrollmentThread.ACT_FEATURES)){
if(null == evt.exception){
m_text.append(" fingerprint captured, features extracted\n\n");
}
else{
MessageBox.DpError("Feature extraction", evt.exception);
}
m_bJustStarted = false;
}
else if(e.getActionCommand().equals(EnrollmentThread.ACT_DONE)){
if(null == evt.exception){
String str = String.format(" enrollment template created, size: %d\n\n\n", evt.enrollment_fmd.getData().length);
m_text.append(str);
}
else{
MessageBox.DpError("Enrollment template creation", evt.exception);
}
m_bJustStarted = true;
}
else if(e.getActionCommand().equals(EnrollmentThread.ACT_CANCELED)){
//canceled, destroy dialog
m_dlgParent.setVisible(false);
}
//cancel enrollment if any exception or bad reader status
if(null != evt.exception){
m_dlgParent.setVisible(false);
}
else if(null != evt.reader_status && Reader.ReaderStatus.READY != evt.reader_status.status && Reader.ReaderStatus.NEED_CALIBRATION != evt.reader_status.status){
m_dlgParent.setVisible(false);
}
}
}
private void doModal(JDialog dlgParent){
//open reader
try{
m_reader.Open(Reader.Priority.COOPERATIVE);
}
catch(UareUException e){ MessageBox.DpError("Reader.Open()", e); }
//start enrollment thread
m_enrollment.start();
//bring up modal dialog
m_dlgParent = dlgParent;
m_dlgParent.setContentPane(this);
m_dlgParent.pack();
m_dlgParent.setLocationRelativeTo(null);
m_dlgParent.setVisible(true);
m_dlgParent.dispose();
//stop enrollment thread
m_enrollment.cancel();
//close reader
try{
m_reader.Close();
}
catch(UareUException e){ MessageBox.DpError("Reader.Close()", e); }
}
public static void Run(Reader reader){
JDialog dlg = new JDialog((JDialog)null, "Enrollment", true);
Enrollment enrollment = new Enrollment(reader);
enrollment.doModal(dlg);
}
}