Motion Detection Webcam with Adobe Flash

An interesting feature and that few people know the Flash Player or Adobe AIR, is that in addition to access features like the webcam user, it can also detect whether there is movement in the camera. This can be useful if you’re implementing a system to stream live and want to save the bandwidth used. For example, the stream will only be published if you have any camera motion.

Flash Player or Adobe AIR, beyond access features such as webcam user can also detect whether there is movement in the camera.

Let’s see an example using Adobe Flash Professional CS5 and Flash Builder 4 (using the Flex framework 4).

Adobe Flash Professional CS5

  • Create a file of type ActionScript 3.0
  • Press F11 to open the library, ask to create a new symbol type “video”:
  • A new window appears, in the name of “cam” and leave the option “Video (ActionScript-controlled)” selected:

    cam

  • Drag the component you just created for the stage and the instance name of “cam”, then resize to 320×240.
  • Drag a ProgressBar component’s type library of the User Interface. Name the instance “pbar”.
  • After that you should have something like this:

    Stage ready to start programming

We have our stage ready to start programming ActionScript 3.0. Let’s start with asking the Flash Player to access the webcam and then put the image of the webcam in our component video.
[ACTIONSCRIPT3]
myCam = Camera.getCamera();
cam.attachCamera(myCam);
[/ACTIONSCRIPT3]
Now just to make our movie listen to the event “ActivityEvent” Webcam.
[ACTIONSCRIPT3]
myCam.addEventListener(ActivityEvent.ACTIVITY,handlerMotion);
[/ACTIONSCRIPT3]
And handlerMotion method, we implement the actions in the ProgressBar.
[ACTIONSCRIPT3]
function handlerMotion(evt:ActivityEvent):void
{
trace(evt.target.activityLevel);
pbar.setProgress(evt.target.activityLevel,100);
if(evt.target.activityLevel < 10) { trace("muito quieto! =õ("); } else { trace("eba! festa!!! \\o/\\O/\\o/"); } } [/ACTIONSCRIPT3] See example in operation: [SWF]http://www.leonardofranca.com.br/wp-content/uploads/2010/08/motion_camera.swf,450,400[/SWF] Adobe Flex 4
We will now see an example using Adobe Flex, I’ll grab the camera using component VideoDisplay.

  • Create a project of type web (Flex) or Desktop (AIR).
  • Drag a component of type VideoDisplay and another type ProgressBar to the stage.
  • We have something like this:
    [MXML]





    [/MXML]

Simply implement the ActionScript programming now, is very similar to the way we did in Flash. We will change little in the ActionScript 3.0 code.
[ACTIONSCRIPT3]
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
pbar.mode = “manual”;
myCam = Camera.getCamera();
var vd:Video = new Video(320,240);
vd.attachCamera(myCam);
cam.addChild(vd);
myCam.addEventListener(ActivityEvent.ACTIVITY, handlerMotion);
}

private function handlerMotion(evt:ActivityEvent):void
{
trace(evt.target.activityLevel);
pbar.setProgress(evt.target.activityLevel,100);
if(evt.target.activityLevel < 10) { pbar.label = "muito quieto! =õ("; } else { pbar.label = "eba! festa!!! \\o/\\O/\\o/"; } } [/ACTIONSCRIPT3] Now, we only apply the logic to be transmitted or not the stream of the webcam user.

DOWNLOAD SOURCE FLEX

More:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Camera.html?allClasses=1

Recommended Books:

Buy Softwares:

Was this article helpful? feel free to make a donation and help keep the blog in the air
ActionScript 3.0, Adobe AIR, Flash, Flex , , ,

3 comments


  1. Pingback: Anonymous

  2. Pingback: ActionScript Facile | 7Wins.eu

Leave a Reply