-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaptureThread.java
executable file
·190 lines (165 loc) · 4.64 KB
/
CaptureThread.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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.digitalpersona.uareu.*;
public class CaptureThread extends Thread
{
public static final String ACT_CAPTURE = "capture_thread_captured";
public class CaptureEvent extends ActionEvent{
private static final long serialVersionUID = 101;
public Reader.CaptureResult capture_result;
public Reader.Status reader_status;
public UareUException exception;
public CaptureEvent(Object source, String action, Reader.CaptureResult cr, Reader.Status st, UareUException ex){
super(source, ActionEvent.ACTION_PERFORMED, action);
capture_result = cr;
reader_status = st;
exception = ex;
}
}
private ActionListener m_listener;
private boolean m_bCancel;
private Reader m_reader;
private boolean m_bStream;
private Fid.Format m_format;
private Reader.ImageProcessing m_proc;
private CaptureEvent m_last_capture;
public CaptureThread(Reader reader, boolean bStream, Fid.Format img_format, Reader.ImageProcessing img_proc){
m_bCancel = false;
m_reader = reader;
m_bStream = bStream;
m_format = img_format;
m_proc = img_proc;
}
public void start(ActionListener listener){
m_listener = listener;
super.start();
}
public void join(int milliseconds){
try{
super.join(milliseconds);
}
catch(InterruptedException e){ e.printStackTrace(); }
}
public CaptureEvent getLastCaptureEvent(){
return m_last_capture;
}
private void Capture(){
synchronized(this){
try{
//wait for reader to become ready
boolean bReady = false;
while(!bReady && !m_bCancel){
Reader.Status rs = m_reader.GetStatus();
if(Reader.ReaderStatus.BUSY == rs.status){
//if busy, wait a bit
try{
Thread.sleep(100);
}
catch(InterruptedException e) {
e.printStackTrace();
break;
}
}
else if(Reader.ReaderStatus.READY == rs.status || Reader.ReaderStatus.NEED_CALIBRATION == rs.status){
//ready for capture
bReady = true;
break;
}
else{
//reader failure
NotifyListener(ACT_CAPTURE, null, rs, null);
break;
}
}
if(m_bCancel){
Reader.CaptureResult cr = new Reader.CaptureResult();
cr.quality = Reader.CaptureQuality.CANCELED;
NotifyListener(ACT_CAPTURE, cr, null, null);
}
if(bReady){
//capture
Reader.CaptureResult cr = m_reader.Capture(m_format, m_proc, 500, -1);
NotifyListener(ACT_CAPTURE, cr, null, null);
}
}
catch(UareUException e){
NotifyListener(ACT_CAPTURE, null, null, e);
}
}
}
private void Stream(){
try{
//wait for reader to become ready
boolean bReady = false;
while(!bReady && !m_bCancel){
Reader.Status rs = m_reader.GetStatus();
if(Reader.ReaderStatus.BUSY == rs.status){
//if busy, wait a bit
try{
Thread.sleep(100);
}
catch(InterruptedException e) {
e.printStackTrace();
break;
}
}
else if(Reader.ReaderStatus.READY == rs.status || Reader.ReaderStatus.NEED_CALIBRATION == rs.status){
//ready for capture
bReady = true;
break;
}
else{
//reader failure
NotifyListener(ACT_CAPTURE, null, rs, null);
break;
}
}
if(bReady){
//start streaming
m_reader.StartStreaming();
//get images
while(!m_bCancel){
Reader.CaptureResult cr = m_reader.GetStreamImage(m_format, m_proc, 500);
NotifyListener(ACT_CAPTURE, cr, null, null);
}
//stop streaming
m_reader.StopStreaming();
}
}
catch(UareUException e){
NotifyListener(ACT_CAPTURE, null, null, e);
}
if(m_bCancel){
Reader.CaptureResult cr = new Reader.CaptureResult();
cr.quality = Reader.CaptureQuality.CANCELED;
NotifyListener(ACT_CAPTURE, cr, null, null);
}
}
private void NotifyListener(String action, Reader.CaptureResult cr, Reader.Status st, UareUException ex){
final CaptureEvent evt = new CaptureEvent(this, action, cr, st, ex);
//store last capture event
m_last_capture = evt;
if(null == m_listener || null == action || action.equals("")) return;
//invoke listener on EDT thread
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
m_listener.actionPerformed(evt);
}
});
}
public void cancel(){
m_bCancel = true;
try{
if(!m_bStream) m_reader.CancelCapture();
}
catch(UareUException e){}
}
public void run(){
if(m_bStream){
Stream();
}
else{
Capture();
}
}
}