Skip to content

Commit

Permalink
Fix method signature and fix detector timing issue, fixes #150
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Oct 7, 2013
1 parent 6fae157 commit 8a116f4
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private void pick(Webcam w) {
webcam.addWebcamListener(this);

detector = new WebcamMotionDetector(webcam);
detector.setCheckInterval(5000);
detector.setInterval(5000);
detector.addMotionListener(this);
detector.start();

Expand Down Expand Up @@ -241,7 +241,7 @@ public Thread newThread(Runnable r) {
@Override
public void motionDetected(WebcamMotionEvent wme) {

LOG.info("{}: motion {}", idnum.incrementAndGet(), wme.getStrength());
LOG.info("{}: motion {}", idnum.incrementAndGet(), wme.getArea());

BufferedImage image = webcam.getImage();
if (image == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class DetectMotionExample extends JFrame implements Runnable {
private JLabel label = null;

private Webcam webcam = Webcam.getDefault();
private int threshold = WebcamMotionDetector.DEFAULT_THREASHOLD;
private int threshold = WebcamMotionDetector.DEFAULT_PIXEL_THREASHOLD;
private int inertia = 1000; // how long motion is valid

public DetectMotionExample() {
Expand Down Expand Up @@ -68,7 +68,7 @@ public static void main(String[] args) throws InterruptedException {
public void run() {

WebcamMotionDetector detector = new WebcamMotionDetector(webcam, threshold, inertia);
detector.setCheckInterval(INTERVAL);
detector.setInterval(INTERVAL);
detector.start();

while (true) {
Expand Down
7 changes: 3 additions & 4 deletions webcam-capture/src/example/java/DetectMotionExample.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


import java.io.IOException;

import com.github.sarxos.webcam.Webcam;
Expand All @@ -9,15 +7,16 @@


/**
* Detect motion.
* Detect motion. This example demonstrates how to use build-in motion detector
* and motion listener to fire motion events.
*
* @author Bartosz Firyn (SarXos)
*/
public class DetectMotionExample implements WebcamMotionListener {

public DetectMotionExample() {
WebcamMotionDetector detector = new WebcamMotionDetector(Webcam.getDefault());
detector.setCheckInterval(100); // one check per 100 ms
detector.setInterval(500); // one check per 500 ms
detector.addMotionListener(this);
detector.start();
}
Expand Down
46 changes: 46 additions & 0 deletions webcam-capture/src/example/java/DetectMotionExample2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.io.IOException;

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


/**
* Detect motion. This example demonstrates of how to use build-in motion
* detector feature without the motion listener. We simply run separate thread
* and check every 1000 milliseconds if there was a motion. In this example
* motion once detected is valid for 2 seconds.
*
* @author Bartosz Firyn (SarXos)
*/
public class DetectMotionExample2 {

public DetectMotionExample2() {

final WebcamMotionDetector detector = new WebcamMotionDetector(Webcam.getDefault());
detector.setInterval(500); // one check per 500 ms
detector.start();

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

@Override
public void run() {
do {
System.out.println(detector.isMotion());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
} while (true);
}
};

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

public static void main(String[] args) throws IOException {
new DetectMotionExample();
System.in.read(); // keep program open
}
}
85 changes: 85 additions & 0 deletions webcam-capture/src/example/java/DetectMotionExample3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JFrame;

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


/**
* Detect motion. This example demonstrates how to use build-in motion detector
* and motion listener to fire motion events.
*
* @author Bartosz Firyn (SarXos)
*/
public class DetectMotionExample3 extends JFrame implements WebcamPanel.Painter {

private static final long serialVersionUID = 1L;

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");

webcam = Webcam.getDefault();

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

detector = new WebcamMotionDetector(webcam);
detector.setInterval(500); // one check per 500 ms
detector.setPixelThreshold(20);
detector.start();

add(panel);

pack();
setVisible(true);
}

public static void main(String[] args) throws IOException {
new DetectMotionExample3();
}

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

@Override
public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) {

double s = detector.getMotionArea();
Point cog = detector.getMotionCog();

Graphics2D g = image.createGraphics();
g.setColor(Color.WHITE);
g.drawString(String.format("Area: %.2f%%", s), 10, 20);

if (detector.isMotion()) {
g.setStroke(new BasicStroke(2));
g.setColor(Color.RED);
g.drawOval(cog.x - 5, cog.y - 5, 10, 10);
} else {
g.setColor(Color.GREEN);
g.drawRect(cog.x - 5, cog.y - 5, 10, 10);
}

g.dispose();

painter.paintImage(panel, image, g2);
}
}
2 changes: 1 addition & 1 deletion webcam-capture/src/example/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<root level="debug">
<root level="warn">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Loading

0 comments on commit 8a116f4

Please sign in to comment.