-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Webcam.java
746 lines (633 loc) · 20.7 KB
/
Webcam.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
package com.github.sarxos.webcam;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDevice;
import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver;
import com.github.sarxos.webcam.ds.cgt.WebcamCloseTask;
import com.github.sarxos.webcam.ds.cgt.WebcamDisposeTask;
import com.github.sarxos.webcam.ds.cgt.WebcamOpenTask;
import com.github.sarxos.webcam.ds.cgt.WebcamReadBufferTask;
/**
* Webcam class. It wraps webcam device obtained from webcam driver.
*
* @author Bartosz Firyn (bfiryn)
*/
public class Webcam {
/**
* Logger instance.
*/
private static final Logger LOG = LoggerFactory.getLogger(Webcam.class);
/**
* List of default drivers to search in classpath.
*/
// @formatter:off
private static final String[] DRIVERS_DEFAULT = new String[] {
"com.github.sarxos.webcam.ds.openimaj.OpenImajDriver",
"com.github.sarxos.webcam.ds.civil.LtiCivilDriver",
"com.github.sarxos.webcam.ds.jmf.JmfDriver",
};
// @formatter:on
private static final List<String> DRIVERS_LIST = new ArrayList<String>(Arrays.asList(DRIVERS_DEFAULT));
private static final List<Class<?>> DRIVERS_CLASS_LIST = new ArrayList<Class<?>>();
private static final List<WebcamDiscoveryListener> DISCOVERY_LISTENERS = Collections.synchronizedList(new ArrayList<WebcamDiscoveryListener>());
/**
* Shutdown hook to be executed when JVM exits gracefully.
*
* @author Bartosz Firyn (sarxos)
*/
private static final class ShutdownHook extends Thread {
private static int number = 0;
/**
* Webcam instance to be disposed / closed.
*/
private Webcam webcam = null;
public ShutdownHook(Webcam webcam) {
super("shutdown-hook-" + (++number));
this.webcam = webcam;
}
@Override
public void run() {
LOG.info("Automatic {} deallocation", webcam.getName());
webcam.dispose();
}
}
/**
* Webcam driver (LtiCivil, JMF, FMJ, JQT, OpenCV, VLCj, etc).
*/
private static WebcamDriver driver = null;
/**
* Webcam tasks processor synchronize non-thread-safe operations.
*/
private static WebcamProcessor processor = new WebcamProcessor();
/**
* Webcam discovery service.
*/
private static volatile WebcamDiscoveryService discovery = null;
/**
* Is automated deallocation on TERM signal enabled.
*/
private static boolean deallocOnTermSignal = false;
private static boolean autoOpen = false;
/**
* Webcam listeners.
*/
private List<WebcamListener> listeners = Collections.synchronizedList(new ArrayList<WebcamListener>());
/**
* List of custom resolution sizes supported by webcam instance.
*/
private List<Dimension> customSizes = new ArrayList<Dimension>();
/**
* Shutdown hook.
*/
private ShutdownHook hook = null;
/**
* Underlying webcam device.
*/
private WebcamDevice device = null;
/**
* Is webcam open?
*/
private AtomicBoolean open = new AtomicBoolean(false);
/**
* Is webcam already disposed?
*/
private AtomicBoolean disposed = new AtomicBoolean(false);
/**
* Webcam class.
*
* @param device - device to be used as webcam
* @throws IllegalArgumentException when argument is null
*/
protected Webcam(WebcamDevice device) {
if (device == null) {
throw new IllegalArgumentException("Webcam device cannot be null");
}
this.device = device;
}
/**
* Open the webcam.
*/
public void open() {
if (!open.compareAndSet(false, true)) {
LOG.debug("Webcam is already open {}", getName());
return;
}
WebcamOpenTask task = new WebcamOpenTask(this);
task.open(this);
Runtime.getRuntime().addShutdownHook(hook = new ShutdownHook(this));
}
/**
* Close the webcam.
*/
public void close() {
if (!open.compareAndSet(true, false)) {
LOG.debug("Webcam is already closed {}", getName());
return;
}
WebcamCloseTask task = new WebcamCloseTask(this);
task.close(this);
Runtime.getRuntime().removeShutdownHook(hook);
}
/**
* Is webcam open?
*
* @return true if open, false otherwise
*/
public boolean isOpen() {
return open.get();
}
/**
* Get current webcam resolution in pixels.
*
* @return Webcam resolution (picture size) in pixels.
*/
public Dimension getViewSize() {
return device.getResolution();
}
/**
* Return list of supported view sizes. It can differ between vary webcam
* data sources.
*
* @return
*/
public Dimension[] getViewSizes() {
return device.getResolutions();
}
/**
* Set custom resolution. If you are using this method you have to make sure
* that your webcam device can support this specific resolution.
*
* @param sizes the array of custom resolutions to be supported by webcam
*/
public void setCustomViewSizes(Dimension[] sizes) {
if (sizes == null) {
customSizes.clear();
return;
}
customSizes = Arrays.asList(sizes);
}
public Dimension[] getCustomViewSizes() {
return customSizes.toArray(new Dimension[customSizes.size()]);
}
/**
* Set new view size. New size has to exactly the same as one of the default
* sized or exactly the same as one of the custom ones.
*
* @param size the new view size to be set
* @see Webcam#setCustomViewSizes(Dimension[])
* @see Webcam#getViewSizes()
*/
public void setViewSize(Dimension size) {
if (size == null) {
throw new IllegalArgumentException("Resolution cannot be null!");
}
if (open.get()) {
throw new IllegalStateException("Cannot change resolution when webcam is open, please close it first");
}
// check if new resolution is the same as current one
Dimension current = getViewSize();
if (current != null && current.width == size.width && current.height == size.height) {
return;
}
// check if new resolution is valid
Dimension[] predefined = getViewSizes();
Dimension[] custom = getCustomViewSizes();
boolean ok = false;
for (Dimension d : predefined) {
if (d.width == size.width && d.height == size.height) {
ok = true;
break;
}
}
if (!ok) {
for (Dimension d : custom) {
if (d.width == size.width && d.height == size.height) {
ok = true;
break;
}
}
}
if (!ok) {
StringBuilder sb = new StringBuilder("Incorrect dimension [");
sb.append(size.width).append("x").append(size.height).append("] ");
sb.append("possible ones are ");
for (Dimension d : predefined) {
sb.append("[").append(d.width).append("x").append(d.height).append("] ");
}
for (Dimension d : custom) {
sb.append("[").append(d.width).append("x").append(d.height).append("] ");
}
throw new IllegalArgumentException(sb.toString());
}
LOG.debug("Setting new resolution {}x{}", size.width, size.height);
device.setResolution(size);
}
/**
* Capture image from webcam and return it. Will return image object or null
* if webcam is closed or has been already disposed by JVM.<br>
* <br>
* <b>IMPORTANT NOTE!!!</b><br>
* <br>
* There are two possible behaviors of what webcam should do when you try to
* get image and webcam is actually closed. Normally it will return null,
* but there is a special flag which can be statically set to switch all
* webcams to auto open mode. In this mode, webcam will be automatically
* open, when you try to get image from closed webcam. Please be aware of
* some side effects! In case of multi-threaded applications, there is no
* guarantee that one thread will not try to open webcam even if it was
* manually closed in different thread.
*
* @return Captured image or null if webcam is closed or disposed by JVM
*/
public BufferedImage getImage() {
if (disposed.get()) {
LOG.warn("Cannot get image, webcam has been already disposed");
return null;
}
if (!open.get()) {
if (autoOpen) {
open();
} else {
return null;
}
}
WebcamReadBufferTask task = new WebcamReadBufferTask(this);
return task.getImage();
}
/**
* Get list of webcams to use. This method will wait predefined time
* interval for webcam devices to be discovered. By default this time is set
* to 1 minute.
*
* @return List of webcams existing in the ssytem
* @throws WebcamException when something is wrong
* @see Webcam#getWebcams(long, TimeUnit)
*/
public static List<Webcam> getWebcams() throws WebcamException {
// timeout exception below will never be caught since user would have to
// wait around three hundreds billion years for it to occur
try {
return getWebcams(Long.MAX_VALUE);
} catch (TimeoutException e) {
throw new RuntimeException(e);
}
}
/**
* Get list of webcams to use. This method will wait given time interval for
* webcam devices to be discovered. Time argument is given in milliseconds.
*
* @param timeout the time to wait for webcam devices to be discovered
* @return List of webcams existing in the ssytem
* @throws WebcamException when something is wrong
* @see Webcam#getWebcams(long, TimeUnit)
*/
public static List<Webcam> getWebcams(long timeout) throws TimeoutException, WebcamException {
return getWebcams(timeout, TimeUnit.MILLISECONDS);
}
/**
* Get list of webcams to use. This method will wait given time interval for
* webcam devices to be discovered.
*
* @param timeout the devices discovery timeout
* @param tunit the time unit
* @return List of webcams
* @throws TimeoutException when timeout has been exceeded
* @throws WebcamException when something is wrong
*/
public static synchronized List<Webcam> getWebcams(long timeout, TimeUnit tunit) throws TimeoutException, WebcamException {
WebcamDiscoveryService discovery = getDiscoveryService();
List<Webcam> webcams = discovery.getWebcams(timeout, tunit);
if (!discovery.isRunning()) {
discovery.start();
}
return webcams;
}
/**
* Will discover and return first webcam available in the system.
*
* @return Default webcam (first from the list)
* @throws WebcamException if something is really wrong
* @see Webcam#getWebcams()
*/
public static Webcam getDefault() throws WebcamException {
try {
return getDefault(Long.MAX_VALUE);
} catch (TimeoutException e) {
// this should never happen since user would have to wait 300000000
// years for it to occur
throw new RuntimeException(e);
}
}
/**
* Will discover and return first webcam available in the system.
*
* @param timeout the webcam discovery timeout (1 minute by default)
* @return Default webcam (first from the list)
* @throws TimeoutException when discovery timeout has been exceeded
* @throws WebcamException if something is really wrong
* @see Webcam#getWebcams(long)
*/
public static Webcam getDefault(long timeout) throws TimeoutException, WebcamException {
return getDefault(timeout, TimeUnit.MILLISECONDS);
}
/**
* Will discover and return first webcam available in the system.
*
* @param timeout the webcam discovery timeout (1 minute by default)
* @param tunit the time unit
* @return Default webcam (first from the list)
* @throws TimeoutException when discovery timeout has been exceeded
* @throws WebcamException if something is really wrong
* @see Webcam#getWebcams(long, TimeUnit)
*/
public static Webcam getDefault(long timeout, TimeUnit tunit) throws TimeoutException, WebcamException {
if (timeout < 0) {
throw new IllegalArgumentException("Timeout cannot be negative");
}
if (tunit == null) {
throw new IllegalArgumentException("Time unit cannot be null!");
}
List<Webcam> webcams = getWebcams(timeout, tunit);
if (!webcams.isEmpty()) {
return webcams.get(0);
}
LOG.warn("No webcam has been detected!");
return null;
}
/**
* Get webcam name (device name). The name of device depends on the value
* returned by the underlying data source, so in some cases it can be
* human-readable value and sometimes it can be some strange number.
*
* @return Name
*/
public String getName() {
return device.getName();
}
@Override
public String toString() {
return String.format("Webcam %s", getName());
}
/**
* Add webcam listener.
*
* @param l the listener to be added
* @throws IllegalArgumentException when argument is null
*/
public boolean addWebcamListener(WebcamListener l) {
if (l == null) {
throw new IllegalArgumentException("Webcam listener cannot be null!");
}
return listeners.add(l);
}
/**
* @return All webcam listeners
*/
public WebcamListener[] getWebcamListeners() {
return listeners.toArray(new WebcamListener[listeners.size()]);
}
/**
* Removes webcam listener.
*
* @param l the listener to be removed
* @return True if listener has been removed, false otherwise
*/
public boolean removeWebcamListener(WebcamListener l) {
return listeners.remove(l);
}
/**
* Return webcam driver. Perform search if necessary.<br>
* <br>
* <b>This method is not thread-safe!</b>
*
* @return Webcam driver
*/
public static synchronized WebcamDriver getDriver() {
if (driver == null) {
driver = WebcamDriverUtils.findDriver(DRIVERS_LIST, DRIVERS_CLASS_LIST);
}
if (driver == null) {
LOG.info("Webcam driver has not been found, default one will be used!");
driver = new WebcamDefaultDriver();
}
return driver;
}
/**
* Set new video driver to be used by webcam.<br>
* <br>
* <b>This method is not thread-safe!</b>
*
* @param driver new webcam driver to be used (e.g. LtiCivil, JFM, FMJ, QTJ)
* @throws IllegalArgumentException when argument is null
*/
public static synchronized void setDriver(WebcamDriver driver) {
if (driver == null) {
throw new IllegalArgumentException("Webcam driver cannot be null!");
}
resetDriver();
Webcam.driver = driver;
}
/**
* Set new video driver class to be used by webcam. Class given in the
* argument shall extend {@link WebcamDriver} interface and should have
* public default constructor, so instance can be created by reflection.<br>
* <br>
* <b>This method is not thread-safe!</b>
*
* @param driver new video driver class to use
* @throws IllegalArgumentException when argument is null
*/
public static synchronized void setDriver(Class<? extends WebcamDriver> driverClass) {
resetDriver();
if (driverClass == null) {
throw new IllegalArgumentException("Webcam driver class cannot be null!");
}
try {
driver = driverClass.newInstance();
} catch (InstantiationException e) {
throw new WebcamException(e);
} catch (IllegalAccessException e) {
throw new WebcamException(e);
}
}
/**
* Reset webcam driver.<br>
* <br>
* <b>This method is not thread-safe!</b>
*/
public static synchronized void resetDriver() {
DRIVERS_LIST.clear();
DRIVERS_LIST.addAll(Arrays.asList(DRIVERS_DEFAULT));
if (discovery != null) {
discovery.shutdown();
discovery = null;
}
driver = null;
}
/**
* Register new webcam video driver.
*
* @param clazz webcam video driver class
* @throws IllegalArgumentException when argument is null
*/
public static void registerDriver(Class<? extends WebcamDriver> clazz) {
if (clazz == null) {
throw new IllegalArgumentException("Webcam driver class to register cannot be null!");
}
DRIVERS_CLASS_LIST.add(clazz);
registerDriver(clazz.getCanonicalName());
}
/**
* Register new webcam video driver.
*
* @param clazzName webcam video driver class name
* @throws IllegalArgumentException when argument is null
*/
public static void registerDriver(String clazzName) {
if (clazzName == null) {
throw new IllegalArgumentException("Webcam driver class name to register cannot be null!");
}
DRIVERS_LIST.add(clazzName);
}
/**
* Return underlying webcam device. Depending on the driver used to discover
* devices, this method can return instances of different class. By default
* {@link WebcamDefaultDevice} is returned when no external driver is used.
*
* @return Underlying webcam device instance
*/
public WebcamDevice getDevice() {
return device;
}
protected WebcamProcessor getProcessor() {
return processor;
}
/**
* Completely dispose capture device. After this operation webcam cannot be
* used any more and full reinstantiation is required.
*/
protected void dispose() {
if (!disposed.compareAndSet(false, true)) {
return;
}
open.set(false);
LOG.info("Disposing webcam {}", getName());
WebcamDisposeTask task = new WebcamDisposeTask(this);
task.dispose();
WebcamEvent we = new WebcamEvent(this);
for (WebcamListener l : listeners) {
try {
l.webcamClosed(we);
l.webcamDisposed(we);
} catch (Exception e) {
LOG.error(String.format("Notify webcam disposed, exception when calling %s listener", l.getClass()), e);
}
}
// hook can be null because there is a possibility that webcam has never
// been open and therefore hook was not created
if (hook != null) {
try {
Runtime.getRuntime().removeShutdownHook(hook);
} catch (IllegalStateException e) {
// ignore, it means that shutdown is in progress
}
}
LOG.debug("Webcam disposed {}", getName());
}
/**
* <b>CAUTION!!!</b><br>
* <br>
* This is experimental feature to be used mostly in in development phase.
* After you set handle term signal to true, and fetch capture devices,
* Webcam Capture API will listen for TERM signal and try to close all
* devices after it has been received. <b>This feature can be unstable on
* some systems!</b>
*
* @param on signal handling will be enabled if true, disabled otherwise
*/
public static void setHandleTermSignal(boolean on) {
if (on) {
LOG.warn("Automated deallocation on TERM signal is now enabled! Make sure to not use it in production!");
}
deallocOnTermSignal = on;
}
/**
* Is TERM signal handler enabled.
*
* @return True if enabled, false otherwise
*/
public static boolean isHandleTermSignal() {
return deallocOnTermSignal;
}
/**
* Switch all webcams to auto open mode. In this mode, each webcam will be
* automatically open whenever user will try to get image from instance
* which has not yet been open. Please be aware of some side effects! In
* case of multi-threaded applications, there is no guarantee that one
* thread will not try to open webcam even if it was manually closed in
* different thread.
*
* @param on true to enable, false to disable
*/
public static void setAutoOpenMode(boolean on) {
autoOpen = on;
}
/**
* Is auto open mode enabled. Auto open mode will will automatically open
* webcam whenever user will try to get image from instance which has not
* yet been open. Please be aware of some side effects! In case of
* multi-threaded applications, there is no guarantee that one thread will
* not try to open webcam even if it was manually closed in different
* thread.
*
* @return True if mode is enabled, false otherwise
*/
public static boolean isAutoOpenMode() {
return autoOpen;
}
/**
* Add new webcam discovery listener.
*
* @param l the listener to be added
* @return True, if listeners list size has been changed, false otherwise
* @throws IllegalArgumentException when argument is null
*/
public static boolean addDiscoveryListener(WebcamDiscoveryListener l) {
if (l == null) {
throw new IllegalArgumentException("Webcam discovery listener cannot be null!");
}
return DISCOVERY_LISTENERS.add(l);
}
public static WebcamDiscoveryListener[] getDiscoveryListeners() {
return DISCOVERY_LISTENERS.toArray(new WebcamDiscoveryListener[DISCOVERY_LISTENERS.size()]);
}
/**
* Remove discovery listener
*
* @param l the listener to be removed
* @return True if listeners list contained the specified element
*/
public static boolean removeDiscoveryListener(WebcamDiscoveryListener l) {
return DISCOVERY_LISTENERS.remove(l);
}
/**
* Return discovery service.
*
* @return Discovery service
*/
public static synchronized WebcamDiscoveryService getDiscoveryService() {
if (discovery == null) {
discovery = new WebcamDiscoveryService(getDriver());
}
return discovery;
}
}