Skip to content

Commit 13b0b61

Browse files
committed
Redisign IP camera driver and fix FPS calculation
1 parent 6a47494 commit 13b0b61

File tree

4 files changed

+402
-348
lines changed

4 files changed

+402
-348
lines changed

webcam-capture-drivers/driver-ipcam/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
<dependency>
3737
<groupId>org.apache.httpcomponents</groupId>
3838
<artifactId>httpclient</artifactId>
39-
<version>4.2.3</version>
39+
<version>4.5.3</version>
4040
</dependency>
4141
<dependency>
4242
<groupId>org.apache.httpcomponents</groupId>
4343
<artifactId>httpmime</artifactId>
44-
<version>4.2.3</version>
44+
<version>4.5.3</version>
4545
</dependency>
4646
</dependencies>
4747

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import java.awt.Dimension;
2+
import java.awt.FlowLayout;
3+
import java.awt.event.ActionEvent;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.net.MalformedURLException;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.concurrent.Executor;
10+
import java.util.concurrent.Executors;
11+
12+
import javax.imageio.ImageIO;
13+
import javax.swing.AbstractAction;
14+
import javax.swing.JButton;
15+
import javax.swing.JFrame;
16+
import javax.swing.JLabel;
17+
18+
import com.github.sarxos.webcam.Webcam;
19+
import com.github.sarxos.webcam.WebcamPanel;
20+
import com.github.sarxos.webcam.WebcamPanel.DrawMode;
21+
import com.github.sarxos.webcam.ds.ipcam.IpCamDeviceRegistry;
22+
import com.github.sarxos.webcam.ds.ipcam.IpCamDriver;
23+
import com.github.sarxos.webcam.ds.ipcam.IpCamMode;
24+
25+
@SuppressWarnings("serial")
26+
public class LignanoBeachPushModeIpCameraExample extends JFrame {
27+
28+
// IMPORTANT! For IP camera you have to use IpCamDriver
29+
30+
static {
31+
Webcam.setDriver(new IpCamDriver());
32+
}
33+
34+
// IMPORTANT! IP cameras are not automatically discovered like USB, you have
35+
// to register them manually
36+
37+
static {
38+
try {
39+
IpCamDeviceRegistry.register("Lignano Beach IP Camera", "http://195.31.81.138/mjpg/video.mjpg", IpCamMode.PUSH);
40+
} catch (MalformedURLException e) {
41+
throw new IllegalStateException(e);
42+
}
43+
}
44+
45+
/**
46+
* Action to be performed when Snapshot JButton is clicked.
47+
*/
48+
private class SnapMeAction extends AbstractAction {
49+
50+
public SnapMeAction() {
51+
super("Snapshot");
52+
}
53+
54+
@Override
55+
public void actionPerformed(ActionEvent e) {
56+
try {
57+
for (int i = 0; i < webcams.size(); i++) {
58+
Webcam webcam = webcams.get(i);
59+
File file = new File(String.format("test-%d.jpg", i));
60+
ImageIO.write(webcam.getImage(), "JPG", file);
61+
System.out.format("Image for %s saved in %s \n", webcam.getName(), file);
62+
}
63+
} catch (IOException e1) {
64+
e1.printStackTrace();
65+
}
66+
}
67+
}
68+
69+
/**
70+
* Action to be performed when Start JButton is clicked.
71+
*/
72+
private class StartAction extends AbstractAction implements Runnable {
73+
74+
public StartAction() {
75+
super("Start");
76+
}
77+
78+
@Override
79+
public void actionPerformed(ActionEvent e) {
80+
81+
btStart.setEnabled(false);
82+
btSnapMe.setEnabled(true);
83+
84+
// remember to start panel asynchronously - otherwise GUI will be
85+
// blocked while OS is opening webcam HW (will have to wait for
86+
// webcam to be ready) and this causes GUI to hang, stop responding
87+
// and repainting
88+
89+
executor.execute(this);
90+
}
91+
92+
@Override
93+
public void run() {
94+
95+
btStop.setEnabled(true);
96+
97+
for (WebcamPanel panel : panels) {
98+
panel.start();
99+
}
100+
}
101+
}
102+
103+
/**
104+
* Action to be performed when Stop JButton is clicked.
105+
*/
106+
private class StopAction extends AbstractAction {
107+
108+
public StopAction() {
109+
super("Stop");
110+
}
111+
112+
@Override
113+
public void actionPerformed(ActionEvent e) {
114+
115+
btStart.setEnabled(true);
116+
btSnapMe.setEnabled(false);
117+
btStop.setEnabled(false);
118+
119+
for (WebcamPanel panel : panels) {
120+
panel.stop();
121+
}
122+
}
123+
}
124+
125+
private Executor executor = Executors.newSingleThreadExecutor();
126+
127+
private List<Webcam> webcams = Webcam.getWebcams();
128+
private List<WebcamPanel> panels = new ArrayList<WebcamPanel>();
129+
130+
private JButton btSnapMe = new JButton(new SnapMeAction());
131+
private JButton btStart = new JButton(new StartAction());
132+
private JButton btStop = new JButton(new StopAction());
133+
134+
public LignanoBeachPushModeIpCameraExample() {
135+
136+
super("Lignano Beach IP Camera Example");
137+
138+
setLayout(new FlowLayout());
139+
140+
JLabel label = new JLabel("Please wait... IP camera user interface initialization in progress");
141+
add(label);
142+
143+
pack();
144+
setVisible(true);
145+
146+
for (final Webcam webcam : webcams) {
147+
final Dimension size = webcam.getViewSizes()[0];
148+
webcam.setViewSize(size);
149+
final WebcamPanel panel = new WebcamPanel(webcam, size, false);
150+
panel.setFPSDisplayed(true);
151+
panel.setDrawMode(DrawMode.FIT);
152+
panels.add(panel);
153+
}
154+
155+
// start application with disable snapshot button - we enable it when
156+
// webcam is started
157+
158+
btSnapMe.setEnabled(false);
159+
btStop.setEnabled(false);
160+
161+
for (WebcamPanel panel : panels) {
162+
add(panel);
163+
}
164+
165+
add(btSnapMe);
166+
add(btStart);
167+
add(btStop);
168+
169+
label.setVisible(false);
170+
171+
pack();
172+
setVisible(true);
173+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
174+
}
175+
176+
public static void main(String[] args) {
177+
new LignanoBeachPushModeIpCameraExample();
178+
}
179+
}

0 commit comments

Comments
 (0)