sadad

by deschtex
Copyright deschtex ( http://wonderfl.net/user/deschtex )
MIT License ( http://www.opensource.org/licenses/mit-license.php )
Downloaded from: http://wonderfl.net/c/cSwB
♥1 | Line 248 | Modified 2011-03-17 17:04:06 | MIT License
play

ActionScript3 source code

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

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

package
{
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.SimpleButton;
    import flash.utils.ByteArray;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import flash.media.*;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;
    import flash.external.ExternalInterface;
    import com.adobe.images.JPGEncoder;

    [SWF(width="380", height="380", backgroundColor="0xFEFEFE", frameRate="30")]

    public class CameraView extends Sprite
    {    
        private static const ROOT_DIR:String = 'webcam/';
        
        private var camera:Camera;
        private var video:Video;
        private var captureSound:Sound;
        private var captureButton:SimpleButton;
        private var captureButtonLoader:Loader;
        private var captureButtonBG:Sprite;
        private var deleteButton:SimpleButton;
        private var deleteButtonLoader:Loader;
        private var request:URLRequest;
        private var snapshot:Bitmap;
        private var snapshotData:BitmapData;
        private var countdownDelta:int = 90;
        private var countdownText:TextField;
        private var countdownTextFormat:TextFormat;
        private var flash:Sprite = new Sprite();
        private var flashOpacity:Number = 1;
        
        private var helpers:CameraViewHelpers;
        
        function CameraView()
        {
            camera = Camera.getCamera();

            if (camera != null)
            {
                helpers = new CameraViewHelpers();
                
                camera.setMode(380, 380, 30, false);
                camera.setQuality(0, 100);
                camera.addEventListener(StatusEvent.STATUS, cameraStatusListener);
                
                video = new Video(380, 380);
                video.attachCamera(camera);
            }
        }
        
        private function log(obj:Object):void
        {
            ExternalInterface.call("console.log", obj);
        }
        
        private function cameraStatusListener(event:StatusEvent):void
        {
            if (!camera.muted)
            {
                initVideo();
                initCaptureButton();
                initDeleteButton();
                initCaptureSound();
                initSnapshot();
                initExternalInterface();
            }
            
            camera.removeEventListener(StatusEvent.STATUS, cameraStatusListener);
        }
        
        private function initVideo():void
        {
            video.smoothing = true;
            video.transform.matrix = new Matrix(-1, 0, 0, 1, video.width, 0);
            
            addChild(video);
        }

        private function initCaptureButton():void
        {
            captureButtonBG = new Sprite();
            
            captureButtonBG.graphics.clear();
            captureButtonBG.graphics.beginFill(0x888888, 0.3);
            captureButtonBG.graphics.drawRect(0, 315, 380, 65);
            captureButtonBG.graphics.endFill();
            
            addChild(captureButtonBG);
            
            captureButtonLoader = new Loader();
            captureButtonLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, captureButtonLoaderListener);
            captureButtonLoader.load(new URLRequest(ROOT_DIR + "capture.png"));
        }

        private function initDeleteButton():void
        {
            deleteButtonLoader = new Loader();
            deleteButtonLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, deleteButtonLoaderListener);
            deleteButtonLoader.load(new URLRequest(ROOT_DIR + "delete2.png"));
        }
        
        private function initCaptureSound():void
        {
            request = new URLRequest(ROOT_DIR + "capture.mp3");
            captureSound = new Sound();
            captureSound.load(request);
        }
        
        private function initSnapshot():void
        {
            snapshotData = new BitmapData(video.width, video.height);
            snapshot = new Bitmap(snapshotData);
        }
        
        private function initExternalInterface():void
        {
            ExternalInterface.addCallback("savePicture", savePicture);
        }
        
        private function captureButtonLoaderListener(event:Event):void
        {
            captureButton = helpers.createButton(event.target.content.bitmapData, [68, 35], [156, 330]);
            captureButton.addEventListener(MouseEvent.CLICK, captureButtonClickListener);
            
            addChild(captureButton);
        }
        
        private function captureButtonClickListener(event:MouseEvent):void
        {
            resetVideo();
            
            countdownTextFormat = new TextFormat();
            countdownTextFormat.font = "Lucida Grande, Tahoma, Verdana, Arial, sans-serif";
            countdownTextFormat.size = 50;
            
            countdownText = new TextField();
            
            countdownText.text = '3';
            countdownText.x = captureButton.x + 17;
            countdownText.y = captureButton.y - 15;
            countdownText.selectable = false;
            countdownText.textColor = 0xFFFFFF;
            countdownText.setTextFormat(countdownTextFormat);

            removeChild(captureButton);
            
            addEventListener(Event.ENTER_FRAME, countdown);
            addChild(countdownText);
        }

        private function deleteButtonLoaderListener(event:Event):void
        {
            deleteButton = helpers.createButton(event.target.content.bitmapData, [40, 19], [340, 20]);
            deleteButton.addEventListener(MouseEvent.CLICK, deleteButtonClickListener);
        }
        
        private function deleteButtonClickListener(event:MouseEvent):void
        {
            resetVideo();
        }
        
        private function resetVideo(fadeOut:Boolean = false):void
        {
            if (contains(snapshot))
            {
                video.visible = true;

                removeChild(snapshot);
                removeChild(deleteButton);

                ExternalInterface.call("disable_save");
            }
        }
        
        private function countdown(event:Event):void
        {
            if (countdownDelta > 0)
            {
                if (countdownDelta == 60)
                {
                    countdownText.text = '2';
                    countdownText.setTextFormat(countdownTextFormat);
                }
                else if (countdownDelta == 30)
                {
                    countdownText.text = '1';
                    countdownText.setTextFormat(countdownTextFormat);
                }
                
                countdownDelta -= 1;
            }
            else
            {
                countdownDelta = 90;
                
                removeEventListener(Event.ENTER_FRAME, countdown);
                removeChild(countdownText);
                
                addEventListener(Event.ENTER_FRAME, flasher);
                addChild(flash);
            }
        }
        
        private function flasher(event:Event):void
        {
            if (flashOpacity > 0)
            {
                flash.graphics.clear();
                flash.graphics.beginFill(0xFFFFFF, flashOpacity);
                flash.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
                flash.graphics.endFill();
                
                flashOpacity == 1 && capture();
                flashOpacity -= 0.05;
            }
            else
            {
                flashOpacity = 1;
                
                removeEventListener(Event.ENTER_FRAME, flasher);
                removeChild(flash);
            }
        }

        private function capture():void
        {    
            video.visible = false;
            captureSound.play();
            
            addChild(snapshot);

            swapChildren(flash, snapshot);
            swapChildren(captureButtonBG, snapshot);

            addChild(captureButton);
            addChild(deleteButton);
            
            snapshotData.draw(video, new Matrix(-1, 0, 0, 1, snapshot.width, 0));
            
            ExternalInterface.call("enable_save");
        }
        
        private function savePicture():void
        {
            var encoder:JPGEncoder = new JPGEncoder(100);
            var snapshotByteArray:ByteArray = encoder.encode(snapshotData);
            
            var savePictureHeader:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
            var savePictureRequest:URLRequest = new URLRequest(ROOT_DIR + "save_picture.php");
            
            savePictureRequest.requestHeaders.push(savePictureHeader);
            savePictureRequest.method = URLRequestMethod.POST;
            savePictureRequest.data = snapshotByteArray;

            var savePictureLoader:URLLoader = new URLLoader();
            
            savePictureLoader.addEventListener(Event.COMPLETE, onSavePictureComplete);
            savePictureLoader.load(savePictureRequest);
        }
        
        private function onSavePictureComplete(event:Event):void
        {
            ExternalInterface.call("show_picture");
        }
    }
}

import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.SimpleButton;
import flash.geom.Rectangle;

class CameraViewHelpers extends Sprite
{
    public function createButton(spriteBitmapData:BitmapData, size:Array, position:Array):SimpleButton
    {    
        var buttonNormState:Bitmap = new Bitmap(spriteBitmapData);
        var buttonOverState:Bitmap = new Bitmap(spriteBitmapData);
        var buttonDownState:Bitmap = new Bitmap(spriteBitmapData);
        
        buttonNormState.scrollRect = new Rectangle(0, 0, size[0], size[1]);        
        buttonOverState.scrollRect = new Rectangle(0, size[1], size[0], size[1]);
        buttonDownState.scrollRect = new Rectangle(0, size[1] * 2, size[0], size[1]);
        
        var button:SimpleButton = new SimpleButton();
        
        button.upState = buttonNormState;
        button.overState = buttonOverState;
        button.downState = buttonDownState;
        button.hitTestState = buttonNormState;
        
        button.x = position[0];
        button.y = position[1];
        button.useHandCursor = true;
        
        return button;
    }
}