-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
OpenImajDevice.java
115 lines (87 loc) · 2.47 KB
/
OpenImajDevice.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
package com.github.sarxos.webcam.ds.openimaj;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import org.openimaj.image.ImageUtilities;
import org.openimaj.video.capture.Device;
import org.openimaj.video.capture.VideoCapture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.sarxos.webcam.WebcamDevice;
public class OpenImajDevice implements WebcamDevice {
private static final Logger LOG = LoggerFactory.getLogger(OpenImajDriver.class);
/**
* Artificial view sizes. I'm really not sure if will fit into other webcams
* but hope that OpenIMAJ can handle this.
*/
private final static Dimension[] DIMENSIONS = new Dimension[] {
new Dimension(176, 144),
new Dimension(320, 240),
new Dimension(352, 288),
new Dimension(640, 400),
new Dimension(640, 480),
new Dimension(1280, 720),
};
private Device device = null;
private VideoCapture capture = null;
private Dimension size = null;
private boolean open = false;
public OpenImajDevice(Device device) {
this.device = device;
}
@Override
public String getName() {
return device.getNameStr();
}
@Override
public Dimension[] getSizes() {
return DIMENSIONS;
}
@Override
public Dimension getSize() {
return size;
}
@Override
public void setSize(Dimension size) {
if (open) {
throw new RuntimeException("Cannot set new size when device is open, please close it first");
}
this.size = size;
}
@Override
public BufferedImage getImage() {
if (!open) {
throw new RuntimeException("Cannot get image from closed device");
}
// TODO scale to dimension if not equal
return ImageUtilities.createBufferedImage(capture.getNextFrame());
}
@Override
public void open() {
if (open) {
return;
}
capture = new VideoCapture(size.width, size.height, device);
open = true;
// what the hell is that something below? that's ugly w/a for blue
// images at the very capture beginning, if you have some other idea of
// how to remove them, please share or fix
int i = 0;
do {
// capture.getNextFrame();
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// }
} while (i++ < 5);
LOG.info("OpenIMAJ webcam device has been initialized");
}
@Override
public void close() {
if (!open) {
return;
}
capture.stopCapture();
open = false;
LOG.info("OpenIMAJ webcam device has been closed");
}
}