Skip to content

Commit 36e1138

Browse files
committed
Add example to list in README, clean file format, refs #350
1 parent 3eafb77 commit 36e1138

File tree

2 files changed

+132
-110
lines changed

2 files changed

+132
-110
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Below are the very pretty basic examples demonstrating of how Webcam Capture API
129129
* [How to save captured image in PNG / JPG / GIF / BMP etc](https://github.com/sarxos/webcam-capture/blob/master/webcam-capture/src/example/java/DifferentFileFormatsExample.java)
130130
* [How to capture with many parallel threads](https://github.com/sarxos/webcam-capture/blob/master/webcam-capture/src/example/java/ConcurrentThreadsExample.java)
131131
* [How to detect motion (text mode only)](https://github.com/sarxos/webcam-capture/blob/master/webcam-capture/src/example/java/DetectMotionExample.java)
132+
* [How to perform multipoint motion detection](https://github.com/sarxos/webcam-capture/blob/master/webcam-capture/src/example/java/MultipointMotionDetectionExample.java)
132133
* [How to display images from multiple IP cameras exposing pictures in JPG format](https://github.com/sarxos/webcam-capture/blob/master/webcam-capture-drivers/driver-ipcam/src/examples/java/JpegDasdingStudioExample.java)
133134
* [How to display image from IP camera exposing MJPEG stream](https://github.com/sarxos/webcam-capture/blob/master/webcam-capture-drivers/driver-ipcam/src/examples/java/MjpegLignanoBeachExample.java)
134135
* [How to use composite driver to display both, build-in and IP camera images](https://github.com/sarxos/webcam-capture/blob/master/webcam-capture-drivers/driver-ipcam/src/examples/java/DualNativeAndMjpegWebcamExample.java)
Lines changed: 131 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,163 @@
1-
package com.github.sarxos.webcam;
2-
3-
import javax.swing.*;
4-
import java.awt.*;
1+
import java.awt.BasicStroke;
2+
import java.awt.Color;
3+
import java.awt.Graphics2D;
4+
import java.awt.Point;
5+
import java.awt.Rectangle;
6+
import java.awt.Stroke;
57
import java.awt.image.BufferedImage;
68
import java.util.ArrayList;
79
import java.util.HashMap;
810
import java.util.Map;
911

10-
public class MultipointMotionDetectionExample implements WebcamMotionListener, WebcamPanel.Painter {
11-
12-
public static void main(String[] args) throws InterruptedException {
13-
new MultipointMotionDetectionExample();
14-
}
15-
16-
private static final int INTERVAL = 100; // ms
17-
18-
public static Webcam webcam;
19-
public static WebcamPanel.Painter painter = null;
20-
21-
public MultipointMotionDetectionExample(){
22-
webcam = Webcam.getDefault();
23-
webcam.setViewSize(WebcamResolution.VGA.getSize());
24-
25-
WebcamPanel panel = new WebcamPanel(webcam);
26-
panel.setPreferredSize(WebcamResolution.VGA.getSize());
27-
panel.setPainter(this);
28-
panel.setFPSDisplayed(true);
29-
panel.setFPSLimited(true);
30-
panel.setFPSLimit(20);
31-
panel.setPainter(this);
32-
panel.start();
33-
34-
painter = panel.getDefaultPainter();
12+
import javax.swing.JFrame;
3513

36-
JFrame window = new JFrame("Multipoint-motion detection");
37-
window.add(panel);
38-
window.setResizable(true);
39-
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40-
window.pack();
41-
window.setVisible(true);
14+
import com.github.sarxos.webcam.Webcam;
15+
import com.github.sarxos.webcam.WebcamMotionDetector;
16+
import com.github.sarxos.webcam.WebcamMotionEvent;
17+
import com.github.sarxos.webcam.WebcamMotionListener;
18+
import com.github.sarxos.webcam.WebcamPanel;
19+
import com.github.sarxos.webcam.WebcamResolution;
4220

4321

44-
WebcamMotionDetector detector = new WebcamMotionDetector(webcam);
45-
46-
//Sets the max amount of motion points to 300 and the minimum range between them to 40
47-
detector.setMaxMotionPoints(300);
48-
detector.setPointRange(40);
49-
50-
detector.setInterval(INTERVAL);
51-
detector.addMotionListener(this);
52-
53-
detector.start();
54-
}
55-
56-
57-
//A HashMap to store all the current points and the current amount of times it has been rendered
58-
//Time rendered is used to remove the point after a certain amount of time
59-
public static HashMap<Point, Integer> motionPoints = new HashMap<Point, Integer>();
60-
61-
//Gets the motion points from the motion detector and adds it to the HashMap
62-
@Override
63-
public void motionDetected(WebcamMotionEvent wme) {
64-
for(Point p : wme.getPoints()){
65-
motionPoints.put(p, 0);
66-
}
67-
}
22+
/**
23+
* This is example demonstrating how to use multipoint motion detection feature.
24+
*
25+
* @author tm1990 (https://github.com/tm1990)
26+
*/
27+
public class MultipointMotionDetectionExample implements WebcamMotionListener, WebcamPanel.Painter {
6828

69-
@Override
70-
public void paintPanel(WebcamPanel panel, Graphics2D g2) {
71-
if (painter != null) {
72-
painter.paintPanel(panel, g2);
73-
}
74-
}
29+
public static void main(String[] args) throws InterruptedException {
30+
new MultipointMotionDetectionExample();
31+
}
32+
33+
private static final int INTERVAL = 100; // ms
7534

35+
public static Webcam webcam;
36+
public static WebcamPanel.Painter painter = null;
7637

77-
//Used to render the effect for the motion points
78-
private static final Stroke STROKE = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[] { 1.0f }, 0.0f);
38+
public MultipointMotionDetectionExample() {
39+
40+
webcam = Webcam.getDefault();
41+
webcam.setViewSize(WebcamResolution.VGA.getSize());
7942

80-
//The amount of time each point should be rendered for before being removed
81-
public static final int renderTime = 3;
43+
WebcamPanel panel = new WebcamPanel(webcam);
44+
panel.setPreferredSize(WebcamResolution.VGA.getSize());
45+
panel.setPainter(this);
46+
panel.setFPSDisplayed(true);
47+
panel.setFPSLimited(true);
48+
panel.setFPSLimit(20);
49+
panel.setPainter(this);
50+
panel.start();
8251

83-
//The actual size of the rendered effect for each point
84-
public static final int renderSize = 20;
52+
painter = panel.getDefaultPainter();
8553

86-
@Override
87-
public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) {
54+
JFrame window = new JFrame("Multipoint-motion detection");
55+
window.add(panel);
56+
window.setResizable(true);
57+
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
58+
window.pack();
59+
window.setVisible(true);
8860

61+
WebcamMotionDetector detector = new WebcamMotionDetector(webcam);
8962

90-
if (painter != null) {
91-
painter.paintImage(panel, image, g2);
92-
}
63+
// Sets the max amount of motion points to 300 and the minimum range between them to 40
64+
detector.setMaxMotionPoints(300);
65+
detector.setPointRange(40);
9366

94-
//Gets all the points and updates the amount of time they have been rendered for
95-
//And removes the ones that exceed the renderTime variable
67+
detector.setInterval(INTERVAL);
68+
detector.addMotionListener(this);
9669

97-
ArrayList<Point> rem = new ArrayList<Point>();
70+
detector.start();
71+
}
9872

99-
for (Map.Entry<Point, Integer> ent : motionPoints.entrySet()) {
100-
Point p = ent.getKey();
73+
/**
74+
* A HashMap to store all the current points and the current amount of times it has been
75+
* rendered. Time rendered is used to remove the point after a certain amount of time.
76+
*/
77+
public static HashMap<Point, Integer> motionPoints = new HashMap<Point, Integer>();
10178

102-
if (ent.getValue() != null) {
103-
int tt = ent.getValue();
104-
if (tt >= renderTime) {
105-
rem.add(ent.getKey());
79+
// Gets the motion points from the motion detector and adds it to the HashMap
80+
@Override
81+
public void motionDetected(WebcamMotionEvent wme) {
82+
for (Point p : wme.getPoints()) {
83+
motionPoints.put(p, 0);
84+
}
85+
}
10686

107-
} else {
108-
int temp = ent.getValue() + 1;
109-
motionPoints.put(p, temp);
110-
}
87+
@Override
88+
public void paintPanel(WebcamPanel panel, Graphics2D g2) {
89+
if (painter != null) {
90+
painter.paintPanel(panel, g2);
91+
}
92+
}
11193

112-
}
113-
}
94+
/**
95+
* Used to render the effect for the motion points.
96+
*/
97+
private static final Stroke STROKE = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[] { 1.0f }, 0.0f);
11498

115-
for(Point p : rem){
116-
motionPoints.remove(p);
117-
}
99+
/**
100+
* The amount of time each point should be rendered for before being removed.
101+
*/
102+
public static final int renderTime = 3;
118103

104+
/**
105+
* The actual size of the rendered effect for each point.
106+
*/
107+
public static final int renderSize = 20;
119108

120-
//Gets all the remaining points after removing the exceeded ones and then renders the current ones as a red square
121-
for(Map.Entry<Point, Integer> ent : motionPoints.entrySet()){
122-
Point p = ent.getKey();
109+
@Override
110+
public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) {
123111

124-
int xx = p.x - (renderSize / 2), yy = p.y - (renderSize / 2);
112+
if (painter != null) {
113+
painter.paintImage(panel, image, g2);
114+
}
125115

126-
Rectangle bounds = new Rectangle(xx, yy, renderSize, renderSize);
116+
// Gets all the points and updates the amount of time they have been rendered for
117+
// And removes the ones that exceed the renderTime variable
127118

128-
int dx = (int) (0.1 * bounds.width);
129-
int dy = (int) (0.2 * bounds.height);
130-
int x = (int) bounds.x - dx;
131-
int y = (int) bounds.y - dy;
132-
int w = (int) bounds.width + 2 * dx;
133-
int h = (int) bounds.height + dy;
119+
ArrayList<Point> rem = new ArrayList<Point>();
134120

135-
g2.setStroke(STROKE);
136-
g2.setColor(Color.RED);
137-
g2.drawRect(x, y, w, h);
121+
for (Map.Entry<Point, Integer> ent : motionPoints.entrySet()) {
122+
Point p = ent.getKey();
138123

139-
}
124+
if (ent.getValue() != null) {
125+
int tt = ent.getValue();
126+
if (tt >= renderTime) {
127+
rem.add(ent.getKey());
140128

141-
}
129+
} else {
130+
int temp = ent.getValue() + 1;
131+
motionPoints.put(p, temp);
132+
}
133+
134+
}
135+
}
136+
137+
for (Point p : rem) {
138+
motionPoints.remove(p);
139+
}
140+
141+
// Gets all the remaining points after removing the exceeded ones and then renders the
142+
// current ones as a red square
143+
144+
for (Map.Entry<Point, Integer> ent : motionPoints.entrySet()) {
145+
146+
Point p = ent.getKey();
147+
int xx = p.x - (renderSize / 2), yy = p.y - (renderSize / 2);
148+
149+
Rectangle bounds = new Rectangle(xx, yy, renderSize, renderSize);
150+
151+
int dx = (int) (0.1 * bounds.width);
152+
int dy = (int) (0.2 * bounds.height);
153+
int x = bounds.x - dx;
154+
int y = bounds.y - dy;
155+
int w = bounds.width + 2 * dx;
156+
int h = bounds.height + dy;
157+
158+
g2.setStroke(STROKE);
159+
g2.setColor(Color.RED);
160+
g2.drawRect(x, y, w, h);
161+
}
162+
}
142163
}

0 commit comments

Comments
 (0)