Skip to content

Commit

Permalink
Merge pull request #306 from krok32/master
Browse files Browse the repository at this point in the history
Add reference to current and previous images in WebcamMotionEvent
  • Loading branch information
sarxos committed Feb 10, 2015
2 parents bcfdee1 + c6380cd commit b9784df
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,13 @@ public void run() {
/**
* Previously captured image.
*/
private BufferedImage previous = null;
private BufferedImage previousOriginal = null;

/**
* Previously captured image with blur and gray filters applied.
*/
private BufferedImage previousModified = null;

/**
* Webcam to be used to detect motion.
*/
Expand Down Expand Up @@ -279,30 +284,30 @@ protected void detect() {
return;
}

BufferedImage current = webcam.getImage();
BufferedImage currentOriginal = webcam.getImage();

if (current == null) {
if (currentOriginal == null) {
motion = false;
return;
}

current = blur.filter(current, null);
current = gray.filter(current, null);
BufferedImage currentModified = blur.filter(currentOriginal, null);
currentModified = gray.filter(currentModified, null);

int p = 0;

int cogX = 0;
int cogY = 0;

int w = current.getWidth();
int h = current.getHeight();
int w = currentModified.getWidth();
int h = currentModified.getHeight();

if (previous != null) {
if (previousModified != null) {
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {

int cpx = current.getRGB(x, y);
int ppx = previous.getRGB(x, y);
int cpx = currentModified.getRGB(x, y);
int ppx = previousModified.getRGB(x, y);
int pid = combinePixels(cpx, ppx) & 0x000000ff;

if (pid >= pixelThreshold) {
Expand All @@ -323,20 +328,22 @@ protected void detect() {
motion = true;
lastMotionTimestamp = System.currentTimeMillis();

notifyMotionListeners();
notifyMotionListeners(currentOriginal);

} else {
cog = new Point(w / 2, h / 2);
}

previous = current;
previousOriginal = currentOriginal;
previousModified = currentModified;
}

/**
* Will notify all attached motion listeners.
* @param image with the motion detected
*/
private void notifyMotionListeners() {
WebcamMotionEvent wme = new WebcamMotionEvent(this, area, cog);
private void notifyMotionListeners(BufferedImage currentOriginal) {
WebcamMotionEvent wme = new WebcamMotionEvent(this, previousOriginal, currentOriginal, area, cog);
for (WebcamMotionListener l : listeners) {
try {
l.motionDetected(wme);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.sarxos.webcam;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.EventObject;


Expand All @@ -15,6 +16,8 @@ public class WebcamMotionEvent extends EventObject {

private final double strength;
private final Point cog;
private final BufferedImage previousImage;
private final BufferedImage currentImage;

/**
* Create detected motion event.
Expand All @@ -24,13 +27,26 @@ public class WebcamMotionEvent extends EventObject {
* @param cog center of motion gravity
*/
public WebcamMotionEvent(WebcamMotionDetector detector, double strength, Point cog) {
this(detector, null, null, strength, cog);
}

/**
* Create detected motion event.
*
* @param detector
* @param previousImage
* @param currentImage
* @param strength
* @param cog center of motion gravity
*/
public WebcamMotionEvent(WebcamMotionDetector detector, BufferedImage previousImage, BufferedImage currentImage, double strength, Point cog) {
super(detector);

this.previousImage = previousImage;
this.currentImage = currentImage;
this.strength = strength;
this.cog = cog;
}

/**
* Get percentage fraction of image covered by motion. 0 is no motion on
* image, and 100 is full image covered by motion.
Expand All @@ -49,6 +65,22 @@ public Webcam getWebcam() {
return ((WebcamMotionDetector) getSource()).getWebcam();
}

/**
* Returns last image before the motion.
* Instance is shared among the listeners, so if you need to change the image, create a copy.
*/
public BufferedImage getPreviousImage() {
return previousImage;
}

/**
* Returns image with the motion detected.
* Instance is shared among the listeners, so if you need to change the image, create a copy.
*/
public BufferedImage getCurrentImage() {
return currentImage;
}

@Override
public WebcamMotionDetector getSource() {
return (WebcamMotionDetector) super.getSource();
Expand Down

0 comments on commit b9784df

Please sign in to comment.