-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
WebcamDefaultDevice.java
298 lines (238 loc) · 7.61 KB
/
WebcamDefaultDevice.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
package com.github.sarxos.webcam.ds.buildin;
import java.awt.Dimension;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.ComponentSampleModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamException;
import com.github.sarxos.webcam.ds.buildin.cgt.CloseSessionTask;
import com.github.sarxos.webcam.ds.buildin.cgt.GetImageTask;
import com.github.sarxos.webcam.ds.buildin.cgt.GetSizeTask;
import com.github.sarxos.webcam.ds.buildin.cgt.NextFrameTask;
import com.github.sarxos.webcam.ds.buildin.cgt.StartSessionTask;
import com.github.sarxos.webcam.ds.buildin.natives.Device;
public class WebcamDefaultDevice implements WebcamDevice {
static {
if (!"true".equals(System.getProperty("webcam.debug"))) {
System.setProperty("bridj.quiet", "true");
}
}
public static final Dimension SIZE_QQVGA = new Dimension(176, 144);
public static final Dimension SIZE_QVGA = new Dimension(320, 240);
public static final Dimension SIZE_CIF = new Dimension(352, 288);
public static final Dimension SIZE_HVGA = new Dimension(480, 400);
public static final Dimension SIZE_VGA = new Dimension(640, 480);
public static final Dimension SIZE_PAL = new Dimension(768, 576);
public static final Dimension SIZE_SVGA = new Dimension(800, 600);
public static final Dimension SIZE_XGA = new Dimension(1024, 768);
public static final Dimension SIZE_HD720 = new Dimension(1280, 720);
public static final Dimension SIZE_WXGA = new Dimension(1280, 768);
public static final Dimension SIZE_SXGA = new Dimension(1280, 1024);
public static final Dimension SIZE_UXGA = new Dimension(1600, 1200);
public static final Dimension SIZE_QXGA = new Dimension(2048, 1536);
/**
* Logger.
*/
private static final Logger LOG = LoggerFactory.getLogger(WebcamDefaultDevice.class);
/**
* Artificial view sizes. I'm really not sure if will fit into other webcams
* but hope that OpenIMAJ can handle this.
*/
// @formatter:off
private final static Dimension[] DIMENSIONS = new Dimension[] {
SIZE_QQVGA,
SIZE_QVGA,
SIZE_CIF,
SIZE_HVGA,
SIZE_VGA,
SIZE_XGA,
};
// @formatter:on
/**
* RGB offsets.
*/
private static final int[] BAND_OFFSETS = new int[] { 0, 1, 2 };
/**
* Number of bytes in each pixel.
*/
private static final int[] BITS = { 8, 8, 8 };
/**
* Image offset.
*/
private static final int[] OFFSET = new int[] { 0 };
/**
* Data type used in image.
*/
private static final int DATA_TYPE = DataBuffer.TYPE_BYTE;
/**
* Image color space.
*/
private static final ColorSpace COLOR_SPACE = ColorSpace.getInstance(ColorSpace.CS_sRGB);
/**
* Synchronous webcam video grabber processor.
*/
private final WebcamGrabberProcessor processor = new WebcamGrabberProcessor();
// synchronous grabber tasks
private final NextFrameTask frameTask = new NextFrameTask(processor);
private final GetImageTask imageTask = new GetImageTask(processor);
private final StartSessionTask sessionTask = new StartSessionTask(processor);
private final GetSizeTask sizeTask = new GetSizeTask(processor);
private final CloseSessionTask closeTask = new CloseSessionTask(processor);
private Device device = null;
private Dimension size = null;
private ComponentSampleModel sampleModel = null;
private ColorModel colorModel = null;
private boolean failOnSizeMismatch = false;
private volatile boolean open = false;
private volatile boolean opening = false;
private volatile boolean disposed = false;
private String name = null;
private String id = null;
private String fullname = null;
protected WebcamDefaultDevice(Device device) {
this.device = device;
this.name = device.getNameStr();
this.id = device.getIdentifierStr();
this.fullname = String.format("%s %s", this.name, this.id);
}
@Override
public String getName() {
return fullname;
}
@Override
public Dimension[] getSizes() {
return DIMENSIONS;
}
@Override
public Dimension getSize() {
return size;
}
@Override
public void setSize(Dimension size) {
this.size = size;
}
@Override
public BufferedImage getImage() {
if (disposed) {
throw new WebcamException("Cannot get image since device is already disposed");
}
if (!open) {
LOG.error("Cannot get image when device is closed");
return null;
}
frameTask.nextFrame();
byte[] bytes = imageTask.getImage(size);
byte[][] data = new byte[][] { bytes };
if (bytes == null) {
LOG.error("Images byte array is null!");
return null;
}
DataBufferByte buffer = new DataBufferByte(data, bytes.length, OFFSET);
WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null);
BufferedImage bi = new BufferedImage(colorModel, raster, false, null);
bi.flush();
return bi;
}
@Override
public void open() {
if (disposed) {
throw new WebcamException("Cannot open webcam when device it's already disposed");
}
synchronized (device) {
if (opening) {
try {
device.wait();
} catch (InterruptedException e) {
throw new WebcamException("Opening wait interrupted");
} finally {
opening = false;
}
}
if (open) {
return;
} else {
opening = true;
}
}
if (size == null) {
size = getSizes()[0];
}
try {
boolean started = sessionTask.startSession(size, device);
if (!started) {
throw new WebcamException("Cannot start video data grabber!");
}
Dimension size2 = sizeTask.getSize();
int w1 = size.width;
int w2 = size2.width;
int h1 = size.height;
int h2 = size2.height;
if (w1 != w2 || h1 != h2) {
if (failOnSizeMismatch) {
throw new WebcamException(String.format("Different size obtained vs requested - [%dx%d] vs [%dx%d]", w1, h1, w2, h2));
}
LOG.warn("Different size obtained vs requested - [{}x{}] vs [{}x{}]. Setting correct one. New size is [{}x{}]", new Object[] { w1, h1, w2, h2, w2, h2 });
size = new Dimension(w2, h2);
}
sampleModel = new ComponentSampleModel(DATA_TYPE, size.width, size.height, 3, size.width * 3, BAND_OFFSETS);
colorModel = new ComponentColorModel(COLOR_SPACE, BITS, false, false, Transparency.OPAQUE, DATA_TYPE);
int i = 0;
do {
frameTask.nextFrame();
imageTask.getImage(size);
if (disposed) {
opening = false;
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOG.error("Nasty interrupted exception", e);
}
} while (i++ < 3);
open = true;
opening = false;
} finally {
// notify all threads which are also waiting for device to be open
synchronized (device) {
device.notifyAll();
}
}
}
@Override
public void close() {
if (!open) {
return;
}
synchronized (device) {
closeTask.closeSession();
open = false;
}
}
@Override
public void dispose() {
if (disposed) {
return;
}
close();
disposed = true;
}
/**
* Determines if device should fail when requested image size is different
* than actually received.
*
* @param fail the fail on size mismatch flag, true or false
*/
public void setFailOnSizeMismatch(boolean fail) {
this.failOnSizeMismatch = fail;
}
}