/* WebcamAlphaOverlay.as Lee Felarca http://www.zeropointnine.com/blog 4-27-2007 v0.9 Overlays grayscale/alpha-ized webcam image onto desktop. Requires Apollo. Source code licensed under a Creative Commons Attribution 3.0 License. http://creativecommons.org/licenses/by/3.0/ Some Rights Reserved. */ package { import flash.display.*; import flash.events.*; import flash.utils.*; import flash.filters.*; import flash.media.Camera; import flash.media.Video; public class WebcamAlphaOverlay extends Sprite { private var cam:Camera; private var vidCam:Video; private var vidDummy:Video; private var sprVid:Sprite; private var bmp1:BitmapData; private var img1:Bitmap; private var spr1:Sprite; private var imgWm:Bitmap private var sprWm:Sprite; private var filterNum:int; private var filter:Array = new Array(); [Embed(source="watermark.png")] public var watermark:Class; public function WebcamAlphaOverlay() { cam = Camera.getCamera(); cam.setMode(1600,1200, 120, true); // 'max out' settings, then take a reading after activated cam.addEventListener(ActivityEvent.ACTIVITY, onCamActivity); cam.setQuality(999999,100); vidDummy = new Video(); addChild(vidDummy); vidDummy.attachCamera(cam); } private function onCamActivity(e:ActivityEvent):void { if (e.activating) init(); // camera now enabled... } private function init():void { // remove setup stuff cam.removeEventListener(ActivityEvent.ACTIVITY, onCamActivity); removeChild(vidDummy); vidDummy.attachCamera(null); vidDummy = null; // filter array filter.push(null); var mAlpha:Array = new Array(); mAlpha = mAlpha.concat([0, 0, 0, 0, 0]); mAlpha = mAlpha.concat([0, 0, 0, 0, 0]); mAlpha = mAlpha.concat([0, 0, 0, 0, 0]); mAlpha = mAlpha.concat([-1,-1,-1,1, 0]); filter.push(mAlpha); var mAlphaInverse:Array = new Array(); mAlphaInverse = mAlphaInverse.concat([0, 0, 0, 0, 0]); mAlphaInverse = mAlphaInverse.concat([0, 0, 0, 0, 0]); mAlphaInverse = mAlphaInverse.concat([0, 0, 0, 0, 0]); mAlphaInverse = mAlphaInverse.concat([.333,.333,.333,.333,3, 0]); filter.push(mAlphaInverse); // stage setup this.stage.stageFocusRect = false; stage.window.maximize(); stage.window.alwaysInFront = true; vidCam = new Video(cam.width, cam.height); // order of these displayobjects is important... vidCam.width = cam.width; vidCam.height = cam.height; vidCam.attachCamera(cam); sprVid = new Sprite(); // (never attached to stage) sprVid.addChild(vidCam); bmp1 = new BitmapData(cam.width,cam.height, false, 0x000000); img1 = new Bitmap(bmp1); spr1 = new Sprite(); spr1.addChild(img1); addChild(spr1); var m:Number = stage.window.height/ spr1.height; var x:int = (stage.window.width - spr1.width*m)/2; spr1.width *= m; spr1.height *= m; spr1.x = x; spr1.y = 0; spr1.buttonMode = true; sprWm = new Sprite(); imgWm = new watermark(); sprWm.addChild(imgWm); addChild(sprWm); sprWm.x = stage.window.width - x - sprWm.width - 10; sprWm.y = 10; sprWm.alpha = -1; addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); addEventListener(MouseEvent.CLICK, onSprClick); addEventListener( Event.ENTER_FRAME, fadeInWm ); setTimeout( fadeOutWmStart, 9000 ); setInterval( mainInterval, 1000/30 ); nextFilter(); } private function nextFilter():void { filterNum += 1; if (filterNum == filter.length) filterNum = 0; spr1.filters = [ new ColorMatrixFilter( filter[filterNum]) ]; } // EVENT-RELATED private function mainInterval():void { stage.focus = this; bmp1.draw( sprVid ); // Note we're copying from the sprite, not the video object. The sprite version shows the fullsize // detail of the camera, whereas the video object always shows 320x240, for some reason. } private function onKeyDown(e:KeyboardEvent):void { if (e.keyCode==32) nextFilter(); } private function onSprClick(e:MouseEvent):void { nextFilter(); } private function fadeInWm(e:Event):void { sprWm.alpha += 0.03; if (sprWm.alpha >= 1) { removeEventListener( Event.ENTER_FRAME, fadeInWm ); trace("one"); } } private function fadeOutWmStart():void { addEventListener( Event.ENTER_FRAME, fadeOutWmDo ); } private function fadeOutWmDo(e:Event):void { sprWm.alpha -= 0.05; if (sprWm.alpha <= 0) { removeEventListener( Event.ENTER_FRAME, fadeOutWmDo ); trace("two"); } } } // class } // package