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

webcam test 3

Get Adobe Flash player
by devon_o 07 Nov 2010
/**
 * Copyright devon_o ( http://wonderfl.net/user/devon_o )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cXYy
 */

package {

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Matrix;
    import flash.media.Camera;
    import flash.media.Video;
    
    /**
     * Still more webcam hijinks...
     * @author Devon O.
     */
    [SWF(width='465', height='465', backgroundColor='#000000', frameRate='31')]
    public class Main extends Sprite {
        
        private var _original:BitmapData;
        private var _mirror:BitmapData;
        private var _video:Video;
        private var _mat:Matrix;
        
        public function Main():void {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(event:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            _original = new BitmapData(465, 465, false, 0x000000);
            addChild(new Bitmap(_original));
            _mirror = new BitmapData(465 >> 1, 465, false, 0x000000);
            var mirror:Bitmap = new Bitmap(_mirror);
            mirror.x = 465 >> 1;
            addChild(mirror);
            _mat = new Matrix();
            _mat.scale( -1, 1);
            _mat.translate(465 >> 1, 0);
            
            if (initVideo())
                addEventListener(Event.ENTER_FRAME, drawImage);
                
        }
        
        private function initVideo():Boolean {
            _video = new Video(465, 465);
            
            var camIndex:int = 0;
            for ( var i : int = 0 ; i < Camera.names.length ; i++ ) {
                if ( Camera.names[ i ] == "USB Video Class Video" ) {
                    camIndex = i;
                    break;
                }
            }
            var cam:Camera = Camera.getCamera(String(camIndex));
            
            if (cam != null) {
                cam.setMode(465, 465, 15);
                _video.attachCamera(cam);
                return true;
            }
            
            return false;
        }
        
        private function drawImage(event:Event):void {
            _original.draw(_video);
            _mirror.draw(_video, _mat);
        }
    }
}