In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: 動体検知

Get Adobe Flash player
by m0ose 13 Oct 2009
// forked from ll_koba_ll's 動体検知
// write as3 code here..
package {

    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.utils.Timer
    import flash.display.Shape

    [SWF(frameRate="10", backgroundColor="#000000")]
    public class MotionCamera extends Sprite
    {
        
        private var camera:Camera;
        private var video:Video;
        private var button:Sprite;
        private var bmd:BitmapData;

        private var now:BitmapData;
        private var before:BitmapData;
        private var rect:Rectangle;
        private var pt:Point;
        
        private var bounds:Rectangle = new Rectangle(0, 0, 80, 20);
        private var thresh_hold:uint= 0xff444444
        private var ttime:Timer
        public function MotionCamera()
        {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void
        {
            
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            camera = Camera.getCamera( )//(Camera.names.length - 1).toString() );

            if (camera != null)
            {
                setupCamera();
            }
                   
            bmd = new BitmapData(camera.width*2, camera.height*2 ,false,0xffffff);
            var bmp:DisplayObject =  addChild(new Bitmap(bmd));
            bmp.scaleX = 1.5;
            bmp.scaleY = 1.5;

            ttime = new Timer(1000 / camera.fps )
            ttime.addEventListener( TimerEvent.TIMER ,loop);
            ttime.start()

            now = new BitmapData(camera.width*2, camera.height*2);
            before = new BitmapData(camera.width*2, camera.height*2);
            rect = new Rectangle(0, 0, camera.width*2, camera.height*2);
            pt = new Point(0, 0);
            
        }


        private function setupCamera():void
        {
            video = new Video(camera.width*2, camera.height*2);
            video.attachCamera(camera);
            //addChild(video);
        }


        private function loop(e:Event):void
        {
            now.draw(video);
          
            now.draw(before, new Matrix(), new ColorTransform(), BlendMode.DIFFERENCE);
  
            var ret:uint = now.threshold(now, rect, pt, "<", thresh_hold, 0xff000000);
            var ret2:uint = now.threshold(now, rect, pt, ">=", thresh_hold, 0xFFFFFFFF);
                   
            var new_rect:Rectangle = now.getColorBoundsRect( 0xFFFFFFFF, 0xFFFFFFFF, true );
             if (  (new_rect.width + new_rect.height ) / 2  > 30 )
             {      
                bounds = new_rect  
             }
                   
            before.draw(video);
            var tmp:BitmapData = before.clone();
            
            var egg:Shape = new Shape()
            egg.graphics.beginFill(0x110000FF);
            egg.graphics.lineStyle(1);
            egg.graphics.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
            egg.graphics.endFill();
            tmp.draw(egg, null, null, "overlay" )
             //tmp.fillRect( bounds, 0x220000FF);
             bmd.draw(tmp);
            
        }
    }
}