package{ import com.gs.TweenLite; import com.gs.plugins.*; import com.joekromer.Encode; import com.joekromer.External; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.CapsStyle; import flash.display.Graphics; import flash.display.JointStyle; import flash.display.LineScaleMode; import flash.display.Sprite; import flash.events.ActivityEvent; import flash.events.Event; import flash.events.MouseEvent; import flash.events.StatusEvent; import flash.events.TimerEvent; import flash.external.ExternalInterface; import flash.geom.Rectangle; import flash.media.Camera; import flash.media.Video; import flash.system.Capabilities; import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextFormatAlign; import flash.utils.Timer; [SWF(width=282,height=471)] public class PhotoBooth extends Sprite{ private var countDownText:TextField; private var countDownFormat:TextFormat; private var video:Video; private var camera:Camera; public function PhotoBooth(){ TweenPlugin.activate([TintPlugin]); drawElements(); addCamera(); } private function drawElements():void{ var drop:Sprite = new Sprite(); var dGr:Graphics = drop.graphics; dGr.beginFill(0x000000,.5); dGr.drawRect(0,0,282,471); dGr.endFill(); addChild(drop); var backGround:Sprite = new Sprite(); var bGr:Graphics = backGround.graphics; bGr.beginFill(0xffffff); bGr.drawRect(5,5,drop.width-10, drop.height-10); bGr.endFill(); addChild(backGround); countDownFormat = new TextFormat(); countDownFormat.align = TextFormatAlign.CENTER; countDownFormat.color = 0xffffff; countDownFormat.size = 100; countDownText = new TextField(); countDownText.multiline = false; countDownText.alpha = 0; countDownText.width = 250; countDownText.height = 200; countDownText.y = 150; countDownText.x = stage.stageWidth/2 - countDownText.width/2; countDownText.setTextFormat(countDownFormat); } private function addCamera():void{ var bandwidth:int = 0; var quality:int = 2000; camera = Camera.getCamera(doMacFix()); if(camera != null){ addVideo(); camera.setMotionLevel(5,10000); camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler); camera.setQuality(bandwidth, quality); camera.setMode(500,744,30,false); }else{ ExternalInterface.call("writeError", "You must have a camera to use this page."); } } private function addVideo():void{ video = new Video(250,372); video.smoothing = true; video.attachCamera(camera); video.scaleX = -1; video.x = video.width/2 + stage.stageWidth/2; video.y = 15; addChild(video); } private function activityHandler(e:ActivityEvent):void{ var photoButton:Sprite = new Sprite(); var pGr:Graphics = photoButton.graphics; pGr.beginFill(0x999999); pGr.lineStyle(2,0x000000,1,true,LineScaleMode.NORMAL, CapsStyle.ROUND, JointStyle.ROUND); pGr.drawRect(0,0,200,50); pGr.endFill(); photoButton.x = stage.stageWidth/2 - photoButton.width/2; photoButton.y = 400; photoButton.buttonMode = true; photoButton.mouseChildren = false; photoButton.addEventListener(MouseEvent.CLICK, startCountDown); var pbtFormat:TextFormat = new TextFormat(); pbtFormat.color = 0xffffff; pbtFormat.size = 20; var pbText:TextField = new TextField(); pbText.selectable = false; pbText.text = "Take Photo"; pbText.setTextFormat(pbtFormat); pbText.x = photoButton.width/2 - pbText.width/2; pbText.y = 12; photoButton.addChild(pbText); addChild(photoButton); } private function startCountDown(e:MouseEvent):void{ var timer:Timer = new Timer(1000,3); timer.addEventListener(TimerEvent.TIMER, changeCount); timer.addEventListener(TimerEvent.TIMER_COMPLETE, takePhoto); timer.start(); addChild(countDownText); countDownText.alpha = .5; countDownText.text = "3"; countDownText.setTextFormat(countDownFormat); } private function changeCount(e:TimerEvent):void{ countDownText.text = String(3 - e.currentTarget.currentCount); countDownText.setTextFormat(countDownFormat); } private function takePhoto(e:TimerEvent):void{ //hide countdown number countDownText.alpha = 0; var snapShot:Bitmap = new Bitmap(); var bmd:BitmapData = new BitmapData(250,372,false, 0x0000ff); bmd.draw(video,null,null,null,null,true); snapShot.bitmapData = bmd; snapShot.x = stage.stageWidth/2 - snapShot.width/2; snapShot.y = video.y; TweenLite.to(video, 0, {tint:0xffffff}); addChild(snapShot); TweenLite.from(snapShot, 1, {alpha:0, delay:.2}); saveSnapShot(bmd); } private function saveSnapShot(bmd:BitmapData):void{ Encode.jpg(bmd); } private function doMacFix():String { if (Capabilities.os.indexOf("Mac OS") > -1) { var i:int; var length:Number = Camera.names.length while (i < length) { if (Camera.names[i] == "USB Video Class Video") { return i.toString(); } i++; } } return null; } } }