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 2010-7-14

Get Adobe Flash player
by inaba 14 Jul 2010
    Embed
/**
 * Copyright inaba ( http://wonderfl.net/user/inaba )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/k6Qa
 */

package {
    import flash.geom.Rectangle;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.net.FileFilter;
    import flash.net.FileReference;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.HTTPStatusEvent;
    import flash.display.Sprite;
    import com.adobe.images.JPGEncoder;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    
    [SWF(width="465", height="465", backgroundColor="#000000", frameRate="30")]

    public class FlashTest extends Sprite
    {
        private var _menu:MenuBar;
        private var _dispArea:DispArea;       
        
        private var _fileRef:FileReference;
        private var _imgOrg:BitmapData;
        private var _imgDst:BitmapData;
        private var _faceRects:Array;
        
        public function FlashTest()
        {
            _dispArea = new DispArea(465, 465);
            addChild(_dispArea);
            
            _menu = new MenuBar();
            addChild(_menu);
            _menu.y = 465 - _menu.height;
            
            _menu.btnOpen.addEventListener(MouseEvent.CLICK, onBtnOpen);
            _menu.btnSave.addEventListener(MouseEvent.CLICK, onBtnSave);
            _menu.btnHide.addEventListener(MouseEvent.CLICK, onBtnHide);
        }
        
        private function onBtnOpen(e:MouseEvent):void
        {    
            _fileRef = new FileReference();
            _fileRef.addEventListener(Event.SELECT, onReferenceSelect);
            _fileRef.addEventListener(Event.COMPLETE, onReferenceComplete);
            _fileRef.browse([new FileFilter("JPEG Image", "*.jpg")]);
        }

        private function onBtnSave(e:MouseEvent):void
        {
            var enc:JPGEncoder = new JPGEncoder(80);
            _fileRef.save(enc.encode(_imgDst), "Save.jpg");
        }
        
        private function onBtnHide(e:MouseEvent):void
        {
            doDetectFace();
        }
        
        private function onReferenceSelect(e:Event):void
        {
            _fileRef.removeEventListener(Event.SELECT, onReferenceSelect);

            _fileRef.load();
        }

        private function onReferenceComplete(e:Event):void
        {
            _fileRef.removeEventListener(Event.COMPLETE, onReferenceComplete);

            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
            loader.loadBytes(_fileRef.data);
        }
        
        private function onLoaderComplete(e:Event):void
        {
            if (_imgOrg)
            {
                _imgOrg.dispose();
                _imgOrg = null;
            }
            
            var loader:Loader = e.currentTarget.loader;
            _imgOrg = new BitmapData(loader.width, loader.height);
            _imgOrg.draw(loader);
            
            loader.unload();
            
            _dispArea.setImg(_imgOrg);
        }
        
        private function doDetectFace():void
        {
            ShieldScreen.show(this, new Rectangle(0, 0, 465, 465), "Processing...");
            
            var req:URLRequest = new URLRequest();
            req.url = "http://detectFace.com/api/detect";
            req.method = URLRequestMethod.POST;
            req.contentType = "image/jpeg";
            req.data = _fileRef.data;
            
            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, onDetectFaceComplete);
            loader.load(req);
        }
        
        private function onDetectFaceComplete(e:Event):void
        {
            var loader:URLLoader = e.currentTarget as URLLoader;
            var data:XML = new XML(loader.data);
            loader.close();
            
            _faceRects = [];
           
            var faces:XMLList = data.face;
            for each (var face:XML in faces)
            {
                if (face.hasOwnProperty("bounds"))
                {
                    var x:Number = face.bounds.attribute("x");
                    var y:Number = face.bounds.attribute("y");
                    var w:Number = face.bounds.attribute("width");
                    var h:Number = face.bounds.attribute("height");

                    _faceRects.push(new Rectangle(x, y, w, h));                    
                }
            }
            
            _imgDst = doFacePaint(_imgOrg, _faceRects);
            _dispArea.changeImg(_imgDst, 0.5, 1.0);
            
            ShieldScreen.close();
        }
        
        private function doFacePaint(img:BitmapData, rects:Array):BitmapData
        {
            var canvas:Sprite = new Sprite();
            canvas.graphics.beginBitmapFill(img);
            canvas.graphics.drawRect(0, 0, img.width, img.height);
            canvas.graphics.endFill();   
            
            canvas.graphics.beginFill(0x000000, 1);
            for each (var rect:Rectangle in rects)
            {
                canvas.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
            }
            canvas.graphics.endFill();
            
            var dstImg:BitmapData = new BitmapData(img.width, img.height);
            dstImg.draw(canvas);
            
            return dstImg;
        }
    }
}
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;
import flash.display.Sprite;
import flash.display.DisplayObjectContainer;
import caurina.transitions.Tweener;

class MenuBar extends Sprite
{
    public var btnOpen:TextBtn;    
    public var btnSave:TextBtn;    
    public var btnHide:TextBtn;    
    
    public function MenuBar()
    {
        btnOpen = new TextBtn("Open Image", 0x662222);
        addChild(btnOpen);
        btnOpen.x = 5;
        btnOpen.y = 5;   
        
        btnSave = new TextBtn("Save Image", 0x226622);
        addChild(btnSave);
        btnSave.x = 465 - btnSave.width - 5;
        btnSave.y = 5;   

        btnHide = new TextBtn("Hide Faces", 0x222266);
        addChild(btnHide);
        btnHide.x = btnSave.x - btnHide.width - 10;
        btnHide.y = 5;   
        
        graphics.beginFill(0x222222, 0.7);
        graphics.drawRect(0, 0, 465, btnOpen.height + 10);
        graphics.endFill();
    }
}

class TextBtn extends Sprite
{
    public function TextBtn(text:String, color:uint)
    {
        mouseEnabled = true;
        useHandCursor = true;
        buttonMode = true;
        
        var tf:TextField = new TextField();
        tf.autoSize = TextFieldAutoSize.LEFT;
        tf.textColor = 0xffffff;
        tf.text = text;
        
        graphics.beginFill(color, 1);
        graphics.drawRoundRect(0, 0, tf.width + 20, tf.height + 10, 10); 
        graphics.endFill();
        
        addChild(tf);
        tf.mouseEnabled = false;
        tf.x = 10;
        tf.y = 5;
    }
}

class ShieldScreen
{
    private static var _shield:Sprite
    private static var _close:Function;
    
    public static function show(target:DisplayObjectContainer, rect:Rectangle, text:String):void
    {
        close();
        
        _shield = new Sprite;
        _shield.graphics.beginFill(0x000000, 0.7);
        _shield.graphics.drawRect(0, 0, rect.width, rect.height);
        _shield.graphics.endFill();

        var tf:TextField = new TextField();
        tf.autoSize = TextFieldAutoSize.LEFT;
        tf.textColor = 0xffffff;
        tf.text = text;
        
        _shield.addChild(tf);
        tf.x = (_shield.width - tf.width) / 2;
        tf.y = (_shield.height - tf.height) / 2;
        
        target.addChild(_shield);
        _shield.x = rect.x;
        _shield.y = rect.y;
        _shield.alpha = 0;
        
        Tweener.addTween(_shield, { alpha:1, time:0.5, transition:"easeOutSine" } );
        
        _close = function():void
            {
                _shield.removeChild(tf);
                
                target.removeChild(_shield);
                _shield = null;
            };
    }
    
    public static function close():void
    {
        if (_shield == null)
        {
            return;
        }
        
        Tweener.addTween(_shield,
            {
                alpha:0, time:0.5, transition:"easeInSine",
                onComplete:function():void
                {
                    _close();
                    _close = null;
                }
            } );
    }
}

class DispArea extends Sprite
{
    private var _img:Sprite;
    private var _areaW:Number;
    private var _areaH:Number;
    
    public function DispArea(w:Number, h:Number)
    {
        _areaW = w;
        _areaH = h;
        
        _img = new Sprite();
        addChild(_img);
    }
    
    public function setImg(img:BitmapData):void
    {
        var rect:Rectangle = getInnerRectByAspect(new Rectangle(0, 0, _areaW, _areaH), img.width / img.height);

        var scale:Number = rect.width / img.width;
        var mat:Matrix = new Matrix(scale, 0, 0, scale, 0, 0);
        
        _img.graphics.clear();
        _img.graphics.beginBitmapFill(img, mat, false, true);
        _img.graphics.drawRect(0, 0, rect.width, rect.height);
        _img.graphics.endFill();
        _img.x = rect.left;
        _img.y = rect.top;
    }

    public function changeImg(img:BitmapData, time:Number, delay:Number, onFinish:Function = null):void
    {
        var oldImg:Sprite = _img;
        _img = new Sprite();
        
        var rect:Rectangle = oldImg.getRect(this);
        var scale:Number = rect.width / img.width;
        var mat:Matrix = new Matrix(scale, 0, 0, scale, 0, 0);
        
        _img.graphics.beginBitmapFill(img, mat, false, true);
        _img.graphics.drawRect(0, 0, rect.width, rect.height);
        _img.graphics.endFill();
        _img.x = rect.left;
        _img.y = rect.top;
        
        addChild(_img);
        _img.alpha = 0;

        Tweener.addTween(_img,
            {
                alpha:1, time:time, delay:delay, transition:"linear",
                onComplete:function():void
                {
                    removeChild(oldImg);
                    
                    if (onFinish != null)
                    {
                        onFinish();
                    }
                }
            } );
    }

    private function getInnerRectByAspect(rectSrc:Rectangle, aspect:Number):Rectangle
    {
        var rectDst:Rectangle;
        
        if (aspect < (rectSrc.width / rectSrc.height))
        {
            var newW:Number = rectSrc.height * aspect;
            var newX:Number = rectSrc.x - ((newW - rectSrc.width) / 2.0);
            rectDst = new Rectangle(newX, rectSrc.y, newW, rectSrc.height);
        }
        else
        {
            var newH:Number = rectSrc.width / aspect;
            var newY:Number = rectSrc.y - ((newH - rectSrc.height) / 2.0);
            rectDst = new Rectangle(rectSrc.x, newY, rectSrc.width, newH);
        }
        
        return rectDst;
    }    
}