Skip to content

Commit

Permalink
More motion detection examples, refs #250
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Aug 8, 2014
1 parent 27c56f3 commit 9edbe37
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
67 changes: 67 additions & 0 deletions webcam-capture/src/example/java/DetectMotionEventsExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.io.IOException;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamMotionDetector;
import com.github.sarxos.webcam.WebcamResolution;


/**
* The goal of this example is to demonstrate the idea behind detecting that
* motion has stopped.
*
* @author Bartosz Firyn (sarxos)
*/
public class DetectMotionEventsExample {

Webcam webcam;
WebcamMotionDetector detector;

public DetectMotionEventsExample() {

webcam = Webcam.getDefault();
webcam.setViewSize(WebcamResolution.VGA.getSize());

detector = new WebcamMotionDetector(webcam);
detector.setInterval(200); // one check per 200 ms
detector.setInertia(2000); // keep "motion" state for 2 seconds
detector.start();

Thread t = new Thread("motion-printer") {

@Override
public void run() {

boolean motion = false;
long now = 0;

while (true) {
now = System.currentTimeMillis();
if (detector.isMotion()) {
if (!motion) {
motion = true;
System.out.println(now + " MOTION STARTED");
}
} else {
if (motion) {
motion = false;
System.out.println(now + " MOTION STOPPED");
}
}
try {
Thread.sleep(50); // must be smaller than interval
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};

t.setDaemon(true);
t.start();
}

public static void main(String[] args) throws IOException {
new DetectMotionEventsExample();
System.in.read(); // keep program open
}
}
2 changes: 1 addition & 1 deletion webcam-capture/src/example/java/DetectMotionExample2.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void run() {
}

public static void main(String[] args) throws IOException {
new DetectMotionExample();
new DetectMotionExample2();
System.in.read(); // keep program open
}
}
11 changes: 6 additions & 5 deletions webcam-capture/src/example/java/DetectMotionExample3.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamMotionDetector;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;


/**
Expand All @@ -24,18 +25,18 @@ public class DetectMotionExample3 extends JFrame implements WebcamPanel.Painter

private final Webcam webcam;
private final WebcamPanel panel;
private final WebcamPanel.Painter painter;
private final WebcamMotionDetector detector;

public DetectMotionExample3() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Motion Detector Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

webcam = Webcam.getDefault();
webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.open(true);

panel = new WebcamPanel(webcam, false);
painter = panel.getPainter(); // store default painter
panel.setPainter(this);
panel.start();

Expand All @@ -56,7 +57,7 @@ public static void main(String[] args) throws IOException {

@Override
public void paintPanel(WebcamPanel panel, Graphics2D g2) {
painter.paintPanel(panel, g2);
panel.getDefaultPainter().paintPanel(panel, g2);
}

@Override
Expand All @@ -80,6 +81,6 @@ public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) {

g.dispose();

painter.paintImage(panel, image, g2);
panel.getDefaultPainter().paintImage(panel, image, g2);
}
}

0 comments on commit 9edbe37

Please sign in to comment.