Skip to content

Commit b9784df

Browse files
committed
Merge pull request #306 from krok32/master
Add reference to current and previous images in WebcamMotionEvent
2 parents bcfdee1 + c6380cd commit b9784df

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamMotionDetector.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,13 @@ public void run() {
149149
/**
150150
* Previously captured image.
151151
*/
152-
private BufferedImage previous = null;
152+
private BufferedImage previousOriginal = null;
153153

154+
/**
155+
* Previously captured image with blur and gray filters applied.
156+
*/
157+
private BufferedImage previousModified = null;
158+
154159
/**
155160
* Webcam to be used to detect motion.
156161
*/
@@ -279,30 +284,30 @@ protected void detect() {
279284
return;
280285
}
281286

282-
BufferedImage current = webcam.getImage();
287+
BufferedImage currentOriginal = webcam.getImage();
283288

284-
if (current == null) {
289+
if (currentOriginal == null) {
285290
motion = false;
286291
return;
287292
}
288293

289-
current = blur.filter(current, null);
290-
current = gray.filter(current, null);
294+
BufferedImage currentModified = blur.filter(currentOriginal, null);
295+
currentModified = gray.filter(currentModified, null);
291296

292297
int p = 0;
293298

294299
int cogX = 0;
295300
int cogY = 0;
296301

297-
int w = current.getWidth();
298-
int h = current.getHeight();
302+
int w = currentModified.getWidth();
303+
int h = currentModified.getHeight();
299304

300-
if (previous != null) {
305+
if (previousModified != null) {
301306
for (int x = 0; x < w; x++) {
302307
for (int y = 0; y < h; y++) {
303308

304-
int cpx = current.getRGB(x, y);
305-
int ppx = previous.getRGB(x, y);
309+
int cpx = currentModified.getRGB(x, y);
310+
int ppx = previousModified.getRGB(x, y);
306311
int pid = combinePixels(cpx, ppx) & 0x000000ff;
307312

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

326-
notifyMotionListeners();
331+
notifyMotionListeners(currentOriginal);
327332

328333
} else {
329334
cog = new Point(w / 2, h / 2);
330335
}
331336

332-
previous = current;
337+
previousOriginal = currentOriginal;
338+
previousModified = currentModified;
333339
}
334340

335341
/**
336342
* Will notify all attached motion listeners.
343+
* @param image with the motion detected
337344
*/
338-
private void notifyMotionListeners() {
339-
WebcamMotionEvent wme = new WebcamMotionEvent(this, area, cog);
345+
private void notifyMotionListeners(BufferedImage currentOriginal) {
346+
WebcamMotionEvent wme = new WebcamMotionEvent(this, previousOriginal, currentOriginal, area, cog);
340347
for (WebcamMotionListener l : listeners) {
341348
try {
342349
l.motionDetected(wme);

webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamMotionEvent.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.sarxos.webcam;
22

33
import java.awt.Point;
4+
import java.awt.image.BufferedImage;
45
import java.util.EventObject;
56

67

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

1617
private final double strength;
1718
private final Point cog;
19+
private final BufferedImage previousImage;
20+
private final BufferedImage currentImage;
1821

1922
/**
2023
* Create detected motion event.
@@ -24,13 +27,26 @@ public class WebcamMotionEvent extends EventObject {
2427
* @param cog center of motion gravity
2528
*/
2629
public WebcamMotionEvent(WebcamMotionDetector detector, double strength, Point cog) {
30+
this(detector, null, null, strength, cog);
31+
}
2732

33+
/**
34+
* Create detected motion event.
35+
*
36+
* @param detector
37+
* @param previousImage
38+
* @param currentImage
39+
* @param strength
40+
* @param cog center of motion gravity
41+
*/
42+
public WebcamMotionEvent(WebcamMotionDetector detector, BufferedImage previousImage, BufferedImage currentImage, double strength, Point cog) {
2843
super(detector);
29-
44+
this.previousImage = previousImage;
45+
this.currentImage = currentImage;
3046
this.strength = strength;
3147
this.cog = cog;
3248
}
33-
49+
3450
/**
3551
* Get percentage fraction of image covered by motion. 0 is no motion on
3652
* image, and 100 is full image covered by motion.
@@ -49,6 +65,22 @@ public Webcam getWebcam() {
4965
return ((WebcamMotionDetector) getSource()).getWebcam();
5066
}
5167

68+
/**
69+
* Returns last image before the motion.
70+
* Instance is shared among the listeners, so if you need to change the image, create a copy.
71+
*/
72+
public BufferedImage getPreviousImage() {
73+
return previousImage;
74+
}
75+
76+
/**
77+
* Returns image with the motion detected.
78+
* Instance is shared among the listeners, so if you need to change the image, create a copy.
79+
*/
80+
public BufferedImage getCurrentImage() {
81+
return currentImage;
82+
}
83+
5284
@Override
5385
public WebcamMotionDetector getSource() {
5486
return (WebcamMotionDetector) super.getSource();

0 commit comments

Comments
 (0)