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: 動体検知テスト

[SWF(width="320", height="240", backgroundColor="#000000", frameRate="15")]
Get Adobe Flash player
by tepe 06 Nov 2015
/**
 * Copyright tepe ( http://wonderfl.net/user/tepe )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/Y680
 */

// forked from tepe's 動体検知テスト
package{
    import flash.text.TextField;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.media.*;
    import flash.filters.*;
    //[SWF(width="320", height="240", backgroundColor="#000000", frameRate="15")] 
    
    

    public class Main extends MovieClip{

        private var video:Video;
        
        private var bmpMC:MovieClip;
        private var bmp:Bitmap;//表示画像
        private var bmpDat:BitmapData;//前フレーム画像データ
        private var bmpDat2:BitmapData;//表示画像用データ(加工用)
        private var backBmp:BitmapData;//背景取得用
        
        private var txt:TextField = new TextField();
        private var s:Sprite = new Sprite();
        private var camera:Camera;
        
        public function Main(){
             
            camera = Camera.getCamera();//カメラ使用許可 
            
            if ( camera != null ) {
                //ビデオ初期化
                video = new Video( 320, 240 );//フレームサイズ 
                video.attachCamera( camera );//使用カメラ
                camera.setMode(320,240,30);
                //camera.setMode(160,80,60);
                //video.visible=false;//非表示
                
                
                //画像データ
                bmpDat = new BitmapData(video.width, video.height, false, 0x000000);
                bmpDat2 = new BitmapData(video.width, video.height, false, 0x000000);
                backBmp = new BitmapData(video.width,video.height, false, 0x000000);
                
                bmp = new Bitmap( bmpDat2 );

                //ムービークリップ
                bmpMC=new MovieClip();
                bmpMC.addChild( bmp );// bmp画像を表示
                //addChild(video);
                addChild(bmpMC);
                txt.text = "aaa";
                txt.textColor = 0xffffff;
                addChild(txt);
                addChild(s);
                addChild(s2);
                //イベント追加
                addEventListener( Event.ENTER_FRAME , capVideo );
            }
        }
        
        private var s2:Sprite = new Sprite();
        private var buf:Vector.<Number> = new Vector.<Number>(20,true);
        private var cnt1:int=0;
        private function updateBuf(val:Number=0):void{
            buf[cnt1] = val;
            cnt1++;
            cnt1%=buf.length;
        }
        private function get getValue():Number{
            var total:Number=0;
            var result:Number=0;
            
            for each(var e:Number in buf){
                total += e;
            }
            result = total/buf.length;

            
            return result;
            
        }

        

        
        
        //毎フレーム処理
        private function capVideo( e:Event ):void{
            
            var source:BitmapData = new BitmapData(video.width, video.height, false, 0x000000);
            source.draw(video);//キャプチャデータ >>> source
            var diff:BitmapData = source.clone();//diff <<< sourceクローン
            var source2:BitmapData = new BitmapData(video.width, video.height, false, 0x000000);
            
            
            // 画像ブレンド(明暗差を視覚化)  キャプチャデータ & 1フレーム前のキャプチャデータ
            diff.draw(bmpDat, new Matrix(), new ColorTransform(), BlendMode.DIFFERENCE);
            diff.copyChannel(diff,diff.rect,new Point(), BitmapDataChannel.GREEN,BitmapDataChannel.ALPHA);
            source2.draw(diff,new Matrix(),new ColorTransform(),BlendMode.LAYER);
            var i:int,j:int;     

   
           //画像の変化量を評価する
            var val:int = 0;
            for(i=0;i<diff.width;i++){
                for(j=0;j<diff.height;j++){
                    var col:uint = diff.getPixel(i,j);//ピクセルデータ取得
                    col = (col>>8)&0xff;//閾値を超えたらカウント
                    if(50 < col){
                        val++;
                        
                    }

                }
            }
            var val2:Number = val;
            updateBuf(val2);
            
            //txt.text = val2.toString();
            txt.text = getValue.toString();
            //txt.appendText("\nsize:"+camera.width.toString()+"×"+camera.height.toString());
            //txt.appendText("\n"+camera.activityLevel.toString());




            
            
            // bmpDat2.カラーチャンネル <<< diff.カラーチャンネル 転送
            bmpDat2.copyChannel(diff, diff.rect, new Point(), BitmapDataChannel.GREEN, BitmapDataChannel.GREEN);
            //bmpDat2.copyChannel(diff, diff.rect, new Point(), BitmapDataChannel.GREEN, BitmapDataChannel.ALPHA);
            //bmpDat2.copyChannel(diff, diff.rect, new Point(), BitmapDataChannel.BLUE, BitmapDataChannel.BLUE);
            ///*

            bmpDat = source.clone();//前フレーム画像を更新
        }
        
        
        
    }
}