WebCam motion detection (simplified code)
Guy Watson has a good article on Webcam motion detection. It makes good use of the new BitmapData methods and blendmode so the code can run pretty fast. His code is really impressive.
However the fla provided in that page is not the most ’simplified’ code and ppl who just started using Flash 8 may have some problems in understanding the code. Therefore I simplified the code a bit and the effect is just similar
The code is as follows. You can also download the fla here.
import flash.display.*;
import flash.geom.*;
cam = Camera.get();
vid.attachVideo(cam);
//
now = new BitmapData(cam.width, cam.height);
before = new BitmapData(cam.width, cam.height);
rect = new Rectangle(0, 0, cam.width, cam.height);
pt = new Point(0, 0);
//
onEnterFrame = function () {
// detect motion if user allow us to access the webcam
if (!cam.muted) {
now.draw(vid);
now.draw(before,new Matrix(),new ColorTransform(),’difference’);
now.threshold(now,rect,pt,’>',0xff111111,0xffff0000);
before.draw(vid);
}
};
_root.attachBitmap(now,10);