ウェブカム Glitch

by shihu
ByteArray いじってみました。
♥0 | Line 80 | Modified 2010-10-12 23:54:33 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.ActivityEvent;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.utils.ByteArray;

    public class Main extends Sprite {
        //------- CONST ------------------------------------------------------------
        private static const _WIDTH:Number = 640;
        private static const _HEIGHT:Number = 480;
        //------- MEMBER -----------------------------------------------------------
        private var _srcRect:Rectangle;
        private var _srcBMD:BitmapData;
        private var _effectBMD:BitmapData;
        private var _bmd:BitmapData;
        //
        private var _camera:Camera;
        private var _video:Video;
        //------- PUBLIC -----------------------------------------------------------
        public function Main():void {
            _srcRect = new Rectangle( 0, 0, _WIDTH, _HEIGHT );
            _srcBMD = new BitmapData( _WIDTH, _HEIGHT );
            _effectBMD = new BitmapData( _WIDTH, _HEIGHT );
            _bmd = new BitmapData( _WIDTH, _HEIGHT );
            //
            if ( _checkAvailability() ) {
                _initialize();
            } else {
                addEventListener( Event.ENTER_FRAME, _onCheckEnterFrame );
            }
        }
        //------- PRIVATE ----------------------------------------------------------
        private function _checkAvailability():Boolean {
            _camera = Camera.getCamera( "0" );
            if ( _camera != null ) {
                return true;
            } else {
                return false;
            }
        }
        private function _onCheckEnterFrame( event:Event ):void {
            if ( _checkAvailability() ) {
                removeEventListener( Event.ENTER_FRAME, _onCheckEnterFrame );
                _initialize();
            }
        }
        private function _initialize():void {
            _camera.setMode( _WIDTH, _HEIGHT, stage.frameRate, false );
            _video = new Video( _WIDTH, _HEIGHT );
            _video.attachCamera( _camera );
            _camera.addEventListener( ActivityEvent.ACTIVITY, _onCameraActivity );
        }
        //--------------------------------------
        //  
        //--------------------------------------
        private function _onCameraActivity( event:ActivityEvent ):void {
            _camera.removeEventListener( ActivityEvent.ACTIVITY, _onCameraActivity );
            addChild( new Bitmap( _bmd ) );
            addEventListener( Event.ENTER_FRAME, _onEnterFrame );
        }
        //--------------------------------------
        //  
        //--------------------------------------
        private function _onEnterFrame( event:Event ):void {
            _effectBMD = _srcBMD.clone();
            _srcBMD.draw( _video );
            _bmd.setPixels( _srcRect, getGlitchByteArray( _srcBMD, _effectBMD, 128, 10 ) );
            //_bmd.setPixels( _srcRect, getGlitchByteArray( _srcBMD, _effectBMD, 64, 100 ) );
        }
        //--------------------------------------
        //  
        //--------------------------------------
        private function getGlitchByteArray(
              srcBMD:BitmapData
            , effectBMD:BitmapData
            , maxSize:int = 128
            , times:uint = 10
        ):ByteArray {
            var effectBytes:ByteArray = effectBMD.getPixels( new Rectangle( 0, 0, srcBMD.width, srcBMD.height ) );
            var bytes:ByteArray = new ByteArray();
            bytes.writeBytes( srcBMD.getPixels( new Rectangle( 0, 0, srcBMD.width, srcBMD.height ) ) );
            const BYTES_LENGTH:uint = effectBytes.length;
            for ( var idx:uint = 0; idx < times; idx++ ) {
                var size:int = int( maxSize * Math.random() );
                bytes.position = int( BYTES_LENGTH * Math.random() );
                bytes.writeBytes( effectBytes, int( ( BYTES_LENGTH - size ) * Math.random() ), size * Math.random() );
            }
            bytes.position = 0;
            return bytes;
        }
        //------- PROTECTED --------------------------------------------------------
        //------- INTERNAL ---------------------------------------------------------
    }
}