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: マーカー認識のための二値化

webCam
/**
 * Copyright tepe ( http://wonderfl.net/user/tepe )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1Ak3
 */

// forked from keno42's マーカー認識のための二値化
package  
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.filters.BlurFilter;
    import flash.filters.ColorMatrixFilter;
    import flash.filters.ConvolutionFilter;
    import flash.geom.Point;
    import flash.media.Camera;//webCam
    import flash.media.Video;
        import net.hires.debug.*;
    public class Test3 extends Sprite
    {
        private var camera:Camera;
        private var video:Video;
        private var bd:BitmapData;
        private var threshold:uint = 0xFF888888;
        private var grayConst:Array = [ 
            0.3, 0.59, 0.11, 0, 0,
            0.3, 0.59, 0.11, 0, 0,
            0.3, 0.59, 0.11, 0, 0,
            0, 0, 0, 0, 255
        ];
        
        
        public function Test3() 
        {
            camera = Camera.getCamera();
            if ( camera == null ) {
            } else {
                start();
            }
        }
        
        private function start():void {
            camera.setMode(465, 232, 60);//カメラ設定 width, height, frame
            video = new Video(465, 232);//ビデオサイズ width, height
            video.attachCamera(camera);
            bd = new BitmapData(video.width, video.height);//ビデオサイズのビットマップデータ
            this.addChild( video );
            this.addChild( new Bitmap( bd ) );
            this.getChildAt(1).y = 233;
                        this.addChild( new Stats() );
            this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        //毎フレーム処理
        private function onEnterFrame(e:Event):void {
            bd.lock();
            bd.draw(video);//ビデオデータ取得
            
            // グレー化
            bd.applyFilter(bd, bd.rect, new Point(), new ColorMatrixFilter(grayConst));
            bd.applyFilter(bd, bd.rect, new Point(), new ConvolutionFilter(5, 5, [
                0, -1, -1, -1, 0,
                -1, -1, -2, -1, -1,
                -1, -2, 25, -2, -1,
                -1, -1, -2, -1, -1,
                0, -1, -1, -1, 0
            ]));
            bd.applyFilter(bd, bd.rect, new Point(), new BlurFilter(3, 3));
            
            // 二値化
            bd.threshold(bd, bd.rect, new Point(), ">", threshold, 0xFFFFFFFF, 0x0000FF00);
            bd.threshold(bd, bd.rect, new Point(), "!=", 0xFFFFFFFF, 0xFF000000);
            bd.unlock();
        }
    }
}