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

flash on 2011-6-12

Get Adobe Flash player
by FabianBeiner 11 Jun 2011
    Embed
package {
    import flash.utils.setTimeout;
    import com.adobe.images.JPGEncoder;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.MovieClip;
    import flash.events.StatusEvent;
    import flash.external.ExternalInterface;
    import flash.external.ExternalInterface;
    import flash.filters.ColorMatrixFilter;
    import flash.filters.ConvolutionFilter;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.system.Security;
    import flash.utils.ByteArray;
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;

    public class MooCam extends MovieClip {
        // Variables.
        private var camera:Camera = null;
        private var imgBA:ByteArray;
        private var imgBD:BitmapData;
        private var imgBitmap:Bitmap;
        private var quality:Number = 90;
        private var video:Video = null;

        // MooCam - public constructor.
        public function MooCam() {
            flash.system.Security.allowDomain("*");
            setupCamera(stage.stageWidth, stage.stageHeight);

            // Function provided to JavaScript.
            ExternalInterface.addCallback("capture", capturePicture);
                  
            // Keyboard handler.
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
        }
        
        public function keyDownHandler(event:KeyboardEvent):void {
            switch(event.keyCode) {
                case Keyboard.SPACE:
                    capturePicture();
                    break;
                case Keyboard.ESCAPE:
                    cleanPicture();
                    break;
            }
            
        }

        public function setupCamera(camWidth:int, camHeight:int):void {
            for (var i:int = 0; i < Camera.names.length; i++) {
                var cameraName:String = Camera.names[i];
                switch (cameraName) {
                    // Prefer the Apple iSight camera over everything else on a Mac.
                    case "USB Video Class Video":
                        camera = Camera.getCamera(cameraName);
                        break;
                    // Otherwise select just any available camera. :)
                    default:
                        camera = Camera.getCamera();
                }
            }

            if (camera != null) {
                // Set some camera options.
                camera.setQuality(0, 100);
                camera.setMotionLevel(100);
                camera.setMode(camWidth, camHeight, stage.frameRate, false);

                // Attach the camera to a video stream.
                video = new Video(camWidth, camHeight);
                video.attachCamera(camera);
                addChild(video);

                camera.addEventListener(StatusEvent.STATUS, camStatusHandler);
            }
            else {
                ExternalInterface.call("MooCam.MsgFromFlash", "No camera detected");
            }
        }

        public function camStatusHandler(event:StatusEvent):void {
            switch (event.code) {
                case 'Camera.Muted':
                    ExternalInterface.call("MooCam.camMuted");
                    break;
                case 'Camera.Unmuted':
                    ExternalInterface.call("MooCam.camUnmuted");
                    break;
            }
        }

        private function capturePicture(filter:String = ""):Boolean {
            if (camera != null) {
                cleanPicture();
                imgBD = new BitmapData(video.width, video.height);
                imgBD.draw(video);

                imgBitmap = new Bitmap(imgBD);
                addChild(imgBitmap);

                // Filter?
                switch (filter) {
                    case "edges":
                        var edgesFilter:ConvolutionFilter = new ConvolutionFilter(3, 3, [0, -1, 0, -1, 4, -1, 0, -1, 0], 1);
                        imgBitmap.filters = [edgesFilter];
                        break;
                    case "emboss":
                    case "embossing":
                        var embossFilter:ConvolutionFilter = new ConvolutionFilter(3, 3, [-2, -1, 0 , -1, 1, 1 , 0, 1, 2], 1);
                        imgBitmap.filters = [embossFilter];
                        break;
                    case "invert":
                    case "inverted":
                        var invertFilter:ColorMatrixFilter = new ColorMatrixFilter([-1, 0, 0, 0, 255, 0 ,-1, 0, 0, 255, 0, 0, -1, 0, 255, 0, 0, 0, 1, 0]);
                        imgBitmap.filters = [invertFilter];
                        break;
                    case "monochrome":
                    case "blackwhite":
                    case "bw":
                        var monochromeFilter:ColorMatrixFilter = new ColorMatrixFilter([0.3, 0.6, 0.1, 0, 0, 0.3, 0.6, 0.1, 0, 0, 0.3, 0.6, 0.1, 0, 0, 0, 0, 0, 1, 0]);
                        imgBitmap.filters = [monochromeFilter];
                        break;
                    case "sepia":
                        var sepiaFilter:ColorMatrixFilter = new ColorMatrixFilter([0.3930000066757202, 0.7689999938011169, 0.1889999955892563, 0, 0, 0.3490000069141388, 0.6859999895095825, 0.1679999977350235, 0, 0, 0.2720000147819519, 0.5339999794960022, 0.1309999972581863, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]);
                        imgBitmap.filters = [sepiaFilter];
                        break;
                }
                return true;
            }
            return false;
        }

        private function cleanPicture():void {
                if (imgBitmap) {
                    removeChild(imgBitmap);
                }
        }
    }
}