Skip to content

Bitmapdata “timelapsed snapshots” from Camera

November 24, 2009

demonstration:  http://nitinmalik.com/flash/bitmapCam/bitmapCam.html

In the actionscript coding done here, I have figured out how to display  timelapsed snapshots from my webcam. The snapshots are are displayed in a bitmap object on the stage and every one second, a new image comes up. Actually in this case I displayed two bitmaps and their one second cycles are offset by 0.5 second. This makes one of the images on stage update every 0.5 seconds (alternating). What I hope to get out of doing this code is to further code to compare one older image with one newer image, but that will be a later post hopefully.

I tested out the code by counting on my fingers to see if the bitmap images update in sequential order. I found that when counting, a new number might appear on the second image spot before it showed up on the first. I realized that this was a matter of relativity.  As soon as a new image from the video is drawn to the bitmap image located on stage, it is immediately visible. Therefore if each bitmap is redrawn in 1 second cycles and the cycles of the two bitmap are offset by half a second, then every half a second: one of the two images on stage will be updated. This means, that the newest image will alternate between bitmaps.

The actionscript code is shown below (copying & pasting in to Flash and autoformating makes it easier to read). Comments and questions are welcome!

</pre>
package bitmapCamCode{
import flash.display.MovieClip;
import flash.media.Camera;
import flash.media.Video;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class BitmapCam extends MovieClip {

private var cam:Camera;
private var vid:Video;
private var older:BitmapData; //first frame
private var newer:BitmapData; //second frame
private var timer0:Timer;
private var timer1:Timer;
private var bmpOld:Bitmap;
private var bmpNew:Bitmap;

public function BitmapCam() {
cam=Camera.getCamera();
trace("cam width = "+cam.width);
trace("cam height = "+cam.height);
vid=new Video();
var moveY:Number = 240;

vid.attachCamera(cam);
addChild(vid);
vid.y=moveY;

// 3 second (3000 ms) delay to allow for User to Accept Camera Dialog Box
timer0= new Timer(3000, 1);
timer0.addEventListener(TimerEvent.TIMER, startLag1);
timer0.start();

// timer 1 offsets your snapshots by 500 milliseconds
timer1= new Timer(3500, 1);
timer1.addEventListener(TimerEvent.TIMER, startLag2);
timer1.start();

older = new BitmapData(vid.width, vid.height); //holds the data of your first snapshot
bmpOld= new Bitmap(); //bitmap to place on stage and display bitmap data
addChild(bmpOld); //placing bitmap on stage
bmpOld.bitmapData=older; //bmp on stage displays the value of Bitmapdata

newer = new BitmapData(vid.width, vid.height);
bmpNew= new Bitmap();
bmpNew.x= 320; //moving bitmap on stage
addChild(bmpNew);
bmpNew.bitmapData=newer; //bitmap (bmpNew) will display bitmapdata(newer)

} //BitmapCam()

private function startLag1 (e:TimerEvent) //triggers the capture of the first snapshot
{
//trace("startLag1");
var timer2:Timer = new Timer(1000, 0);
timer2.addEventListener(TimerEvent.TIMER, getOldFrame);
timer2.start();
timer0.removeEventListener(TimerEvent.TIMER, startLag1);
} //startLag1()

private function startLag2 (e:TimerEvent) //triggers the capture of the second snapshot
{
//trace("startLag2");
var timer3:Timer = new Timer(1000, 0);
timer3.addEventListener(TimerEvent.TIMER, getNewFrame);
timer3.start();
timer1.removeEventListener(TimerEvent.TIMER, startLag2);
} //startLag2()

private function getOldFrame(e:TimerEvent)
{
//trace("getOldFrame");

older.draw(vid); // inputs current frame of video into Bitmapdata

} //getOldFrame()

private function getNewFrame(e:TimerEvent)
{
//trace("getNewFrame");

newer.draw(vid);

} //getNewFrame()

}//class
}//package

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: