WebCam snippet
I figured around the time of the cam ripples post id share my webcam snippet.
in changing over from AS2 to AS3 i had to search out the new code for camera interaction.
as with all things theres many ways to do it.
ive come up with a snippet i use all the time. it’s set to a camera resolution of 960 x 720.
i have a logitech 9000 pro.
it kicks arse
change the resolution if you want but i think flash defaults to the highest available resolution if your hardware doesnt support whats called for.
First we set our width and height, grab the default camera ,create a video object to attach the camera to, create our “when camera becomes active” listener, and add the camera to the display list.
then theres a little bitmapData action happening in there, followed by its partner in crime, the bitmap.
when the camera is active we add our holy enterframe listener and in the enterframe event we have that bitmapData draw() our video object giving us access to fresh bitmapdata from the camera when ever we need it.
the first line there, stagecacheAsBitmap; isnt necessary but i spent DAYS looking for those words in that order, i wanted to draw() the stage and couldnt for the life of me. So i’m leaving that line there hoping someone will stumble upon it and either end their search or be inspired by a new possability.
stage.cacheAsBitmap;
var camW:int = 960;
var camH:int = 720;
// Create the camera
var cam:Camera = Camera.getCamera();
cam.setMode( camW, camH, stage.frameRate );
cam.addEventListener(Event.ACTIVATE, active);
// Create a video
var vid:Video = new Video( camW, camH );
vid.attachCamera( cam );
addChild(vid);
var mainBmd:BitmapData= new BitmapData(960, 720, false, 0);
var mainBm:Bitmap = new Bitmap;
function active(event:Event){
addEventListener(Event.ENTER_FRAME, onEnterFrame );
} ;
function onEnterFrame(event:Event) : void
{
mainBmd.draw(vid);
var mainBm:Bitmap = new Bitmap(mainBmd);
//addChild(mainBm);
};