-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
WebcamPanel.java
168 lines (132 loc) · 3.2 KB
/
WebcamPanel.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
package com.github.sarxos.webcam;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Simply implementation of JPanel allowing users to render pictures taken with
* webcam.
*
* @author Bartosz Firyn (SarXos)
*/
public class WebcamPanel extends JPanel implements WebcamListener {
private static final long serialVersionUID = 5792962512394656227L;
private static final Logger LOG = LoggerFactory.getLogger(WebcamPanel.class);
private double frequency = 5; // Hz
private class Repainter extends Thread {
public Repainter() {
setDaemon(true);
}
@Override
public void run() {
super.run();
while (webcam.isOpen()) {
image = webcam.getImage();
if (image == null) {
LOG.error("Image is null");
}
try {
if (paused) {
synchronized (this) {
this.wait();
}
}
Thread.sleep((long) (1000 / frequency));
} catch (InterruptedException e) {
LOG.error("Nasty interrupted exception");
}
repaint();
}
}
}
private Webcam webcam = null;
private BufferedImage image = null;
private Repainter repainter = null;
public WebcamPanel(Webcam webcam) {
this.webcam = webcam;
this.webcam.addWebcamListener(this);
if (!webcam.isOpen()) {
webcam.open();
}
setPreferredSize(webcam.getViewSize());
if (repainter == null) {
repainter = new Repainter();
repainter.start();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image == null) {
return;
}
g.drawImage(image, 0, 0, null);
}
@Override
public void webcamOpen(WebcamEvent we) {
if (repainter == null) {
repainter = new Repainter();
repainter.start();
}
setPreferredSize(webcam.getViewSize());
}
@Override
public void webcamClosed(WebcamEvent we) {
if (repainter != null) {
if (repainter.isAlive()) {
try {
repainter.join(1000);
} catch (InterruptedException e) {
throw new WebcamException("Thread interrupted", e);
}
}
repainter = null;
}
}
private volatile boolean paused = false;
/**
* Pause rendering.
*/
public void pause() {
if (paused) {
return;
}
paused = true;
}
/**
* Resume rendering.
*/
public void resume() {
if (!paused) {
return;
}
synchronized (repainter) {
repainter.notifyAll();
}
paused = false;
}
/**
* @return Rendering frequency (in Hz or FPS).
*/
public double getFrequency() {
return frequency;
}
private static final double MIN_FREQUENCY = 0.016; // 1 frame per minute
private static final double MAX_FREQUENCY = 25; // 25 frames per second
/**
* Set rendering frequency (in Hz or FPS). Minimum frequency is 0.016 (1
* frame per minute) and maximum is 25 (25 frames per second).
*
* @param frequency the frequency
*/
public void setFPS(double frequency) {
if (frequency > MAX_FREQUENCY) {
frequency = MAX_FREQUENCY;
}
if (frequency < MIN_FREQUENCY) {
frequency = MIN_FREQUENCY;
}
this.frequency = frequency;
}
}