-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix method signature and fix detector timing issue, fixes #150
- Loading branch information
Showing
9 changed files
with
390 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.