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

ローカルの画像を加工してから保存

ローカルの画像を加工してから保存
やりたいことはできた。

がんばればサーバに保存もできそう。

読み込んだ画像をクリックして
丸を表示。

参考サイト
http://cuaoar.jp/2008/07/flash-player-10-5.html

http://hakuhin.jp/as3/graphics.html#GRAPHICS_06
Get Adobe Flash player
by otherone 07 Nov 2011
/**
 * Copyright otherone ( http://wonderfl.net/user/otherone )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lGdQ
 */

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.GradientType;
    import flash.display.Graphics;
    import flash.display.Loader;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.ProgressEvent;
    import flash.geom.Matrix;
    import flash.net.FileReference;
    import flash.events.Event;
    import flash.net.FileFilter;
    import flash.text.TextField;
    import flash.utils.ByteArray;
    //import ru.inspirit.encoders.png.PNGEncoder;
    import com.adobe.images.PNGEncoder;

    /**
     * ...
     * @author moriya
     */
    public class Main extends Sprite
    {
        private const STW:uint = stage.stageWidth, STH:uint = stage.stageHeight;
        private var fileDetails:TextField = new TextField();
        private var by:ByteArray;
        private var loader:Loader;

        private var bitmapData:BitmapData;
        private var bmp:Bitmap;
        private var coverContainer:Sprite;
        private var fr:FileReference;
        private var imagesFilter:FileFilter;
        private var docFilter:FileFilter;
        private var container:Sprite;
        private var processFlg:Boolean;

        public function Main():void
        {
            if (stage)
            init();
            else
            addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            processFlg = false;

            container = new Sprite();
            coverContainer = new Sprite();
            var _g:Graphics = coverContainer.graphics;
            _g.beginFill(0xFFFFFF, 0.5);
            _g.drawRect(0, 0, STW, 62);
            _g.endFill();

            var brws:ButtonSet = new ButtonSet(coverContainer, 100, 20, 'BROWSE', null, null, browseClicked);
            brws.x = 210, brws.y = 10;
            var saveBtn:ButtonSet = new ButtonSet(coverContainer, 100, 20, 'SAVE', null, null, saveHandler);
            saveBtn.x = 320, saveBtn.y = 10;

            fileDetails.x = 2, fileDetails.y = 10;
            fileDetails.width = 200,fileDetails.height = 40;
            fileDetails.border = true;

            coverContainer.addChild(fileDetails);
            bmp = new Bitmap(bitmapData);
            container.addChild(bmp);
            container.addChild(coverContainer);
            addChild(container);

            var _g2:Graphics=container.graphics
            _g2.beginFill(0x000000, 1);
            _g2.drawRect(0, 0, STW, STH);
            _g2.endFill();

            fr = new FileReference();
            imagesFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
            docFilter = new FileFilter("Documents", "*.pdf;*.doc;*.txt");

            fr.addEventListener(Event.SELECT, fnSelected);
            //
            container.addEventListener(MouseEvent.CLICK, fnCircle);

        }

        public function fnCircle(mouseEvent:MouseEvent):void
        {

            var p_x:int = mouseX;    // 中心座標
            var p_y:int = mouseY;

            if ((p_x > bmp.x && p_x < bmp.x + bmp.width) && (p_y > coverContainer.height && p_y > bmp.y && p_y < bmp.y + bmp.height))
            {
                var radius:int = Math.random()*50;    // 線の太さ
                var scale:Number = 1.0 / 1638.4 * radius * 2;
                var m :Matrix = new Matrix();
                m.identity();        // 正規化
                m.scale(scale , scale);    // 行列 *= スケーリング
                m.translate( p_x,p_y);    // 行列 *= 平行移動

                var shape:Shape = new Shape();
                container.addChild(shape);
                var g:Graphics = shape.graphics;

                //var grt:GradientType;
                //g.lineStyle (1, 0x000000, 1.0);    // 線のスタイル
                g.beginGradientFill (        // 面のスタイル
                GradientType.RADIAL,
                [Math.random()*0xFFFFFF , Math.random()*0xFFFFFF , Math.random()*0xFFFFFF],
                [     Math.random() ,      Math.random() ,      Math.random()],
                [       0 ,      127 ,      255],
                m
                );

                g.drawCircle (p_x, p_y , radius);
            }
        }

        private function browseClicked():void
        {
            if(!processFlg)fr.browse([imagesFilter, docFilter]);
        }

        private function fnSelected(e:Event):void
        {
            processFlg = true;
            fileDetails.text = e.target.name;
            fr.load();
            fr.addEventListener(ProgressEvent.PROGRESS, onProgress);
            fr.addEventListener(Event.COMPLETE, onCompleted);
        }

        private function onProgress(e:ProgressEvent):void
        {
            fileDetails.text = e.target.name+"\n"+"Loading.." + e.bytesLoaded + "/" + e.bytesTotal;
        }

        /*
        * サーバに保存したい場合
        * */
        //private function __uploadClicked():void
        //{
        //var _req:URLRequest = new URLRequest('upload.php');
        //fr.upload(_req);
        //}

        private function onCompleted(e:Event):void
        {
            fileDetails.text=e.target.name+'\nロード完了';
            by = new ByteArray();
            by = fr.data
            //リスナー削除する
            processFlg=false
            loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgConversion);
            loader.loadBytes(by);
        }

        private function imgConversion(e:Event):void
        {

            bitmapData = new BitmapData(loader.width, loader.height);
            bitmapData.draw(loader);
            bmp.bitmapData = bitmapData;
            bmp.x = (STW - loader.width) * .5;
            bmp.y = (STH - loader.height) * .5
        }

        public function saveHandler():void
        {
            if (!processFlg)
            {
                var cap:BitmapData = new BitmapData(container.width,container.height);
                cap.draw(container);
                ///var byteArray:ByteArray
                by= PNGEncoder.encode(cap);
                var fr:FileReference = new FileReference();
                fr.save(by,"img_name01.png");
            }
        }
    }

}

//********************************************************************************

import flash.display.Bitmap;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

class ButtonSet extends Sprite
{
    private var btnOut:*;
    private var btnOver:*;
    private var _function:Function;
    private var w:int;
    private var h:int;
    private var overImg:Bitmap;
    private var outImg:Bitmap;

    public function ButtonSet(_trgt:Object, _width:int = 100, _height:int = 20, _name:String = '', _overImg:Bitmap = null, _outImg:Bitmap = null, _fnc:Function = null):void
    {
        w = _width;
        h = _height;
        var txt:TextField = new TextField();
        txt.textColor = 0xFFFFFF;
        txt.selectable = false;
        txt.autoSize = TextFieldAutoSize.CENTER;
        overImg = _overImg;
        outImg = _outImg;

        //イメージ画像が無い場合は、デフォルトを設定する
        if (_overImg == null || _outImg == null)
        {
            btnOut = new Shape();
            btnOver = new Shape();
            fnDraw();
            txt.text = _name;
            txt.selectable = false;
        }
        else
        {
            //イメージ画像が有りの場合
            btnOut = new Sprite();
            btnOver = new Sprite();
            btnOut.addChild(_outImg);
            btnOver.addChild(_overImg);
        }

        this.addChild(btnOut);
        this.addChild(btnOver);
        this.addChild(txt);

        btnOver.alpha = 0;
        this.buttonMode = true;
        this.addEventListener(MouseEvent.CLICK, fnClick);
        this.addEventListener(MouseEvent.ROLL_OVER, fnOver);
        this.addEventListener(MouseEvent.ROLL_OUT, fnOut);

        _function = _fnc;
        _trgt.addChild(this);

    }

    private function fnDraw():void
    {
        if (w <= 100)
        w = 100;
        if (h <= 20)
        h = 20;
        btnOut.graphics.clear();
        btnOut.graphics.beginFill(0x666666, 1);
        btnOut.graphics.drawRect(0, 0, w, h);

        btnOver.graphics.clear();
        btnOver.graphics.beginFill(0x444444, 1);
        btnOver.graphics.drawRect(0, 0, w, h);
    }

    public function set _width(value:int):void
    {
        w = value;
        if (overImg == null || outImg == null)
        fnDraw();
    }

    public function set _height(value:int):void
    {
        h = value;
        if (overImg == null || outImg == null)
        fnDraw();
    }

    private function fnOver(e:MouseEvent):void
    {
        btnOver.alpha = 1;
    }

    private function fnOut(e:MouseEvent):void
    {
        btnOver.alpha = 0;
    }

    private function fnClick(e:MouseEvent):void
    {
        (_function != null) ? (_function()) : (0);
    }

}