[AS3.0] AnimationGIFSaverクラスに挑戦!

SWF上で描いた画像をローカルに保存するAnimationGIFSaverクラスをAS3.0クラスで作ってみる。 :bouzu:

FileReferenceクラスを使うよ。 :boy:

:caution: 要 Flash Player 10 以上

This movie requires Flash Player 10.0.0

GIFEncoder.as (org.bytearray.gif.encoder.GIFEncoder.as)
:check: as3gif - Project Hosting on Google Code

AnimationGIFSaver.as (net.AnimationGIFSaver.as)
package net {

  import flash.events.EventDispatcher;
  import flash.net.FileReference;
  import flash.utils.ByteArray;
  import flash.display.BitmapData;
  import flash.events.Event;
  import flash.events.ProgressEvent;
  import flash.events.IOErrorEvent;
  import flash.events.HTTPStatusEvent;
  import flash.events.SecurityErrorEvent;

  import org.bytearray.gif.encoder.GIFEncoder;

  public class AnimationGIFSaver extends EventDispatcher {
    // プロパティ
    private var file:FileReference;

    // コンストラクタ
    public function AnimationGIFSaver() {
      init();
    }

    // メソッド
    private function init():void {
      file = new FileReference();
      file.addEventListener(Event.OPEN, open, false, 0, true);
      file.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
      file.addEventListener(Event.COMPLETE, complete, false, 0, true);
      file.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
      file.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
      file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);

    }
    public function save(bdList:Array, repeat:uint, interval:uint, defaultName:String):void {
      var encoder:GIFEncoder = new GIFEncoder();
      encoder.start();
      encoder.setRepeat(repeat);
      encoder.setDelay(interval);

      for (var n:uint = 0; n < bdList.length; n++) {
        var bd:BitmapData = bdList[n];
        var bitmapData:BitmapData = new BitmapData(bd.width, bd.height);
        bitmapData.lock();
        bitmapData.draw(bd);
        bitmapData.unlock();

        encoder.addFrame(bitmapData);
      }
      encoder.finish();
      var byteArray:ByteArray = encoder.stream;

      file.save(byteArray, defaultName);
    }
    private function open(evt:Event):void {
      dispatchEvent(evt);
    }
    private function progress(evt:ProgressEvent):void {
      dispatchEvent(evt);
    }
    private function complete(evt:Event):void {
      dispatchEvent(evt);
    }
    private function ioerror(evt:IOErrorEvent):void {
      dispatchEvent(evt);
    }
    private function httpstatus(evt:HTTPStatusEvent):void {
      dispatchEvent(evt);
    }
    private function securityerror(evt:SecurityErrorEvent):void {
      dispatchEvent(evt);
    }

  }

}

Main.as
package {

  import flash.display.Sprite;
  import flash.display.BitmapData;
  import flash.display.Bitmap;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import net.AnimationGIFSaver;
  import controls.Btn;


  [SWF(backgroundColor="#FFFFFF", width="440", height="480", frameRate="30")]

  public class Main extends Sprite {
    // プロパティ
    private var saver:AnimationGIFSaver;
    private var saveBtn:Btn;
    private var container:Sprite;
    private var bitmapDataList:Array;

    // コンストラクタ
    public function Main() {
      init();
    }

    // メソッド
    private function init():void {
      saver = new AnimationGIFSaver();
      saver.addEventListener(Event.COMPLETE, complete, false, 0, true);
      saveBtn = new Btn();
      addChild(saveBtn);
      saveBtn.x = 220;
      saveBtn.y = 455;
      saveBtn.init({label: "save"});
      saveBtn.addEventListener(MouseEvent.CLICK, save, false, 0, true);
      container = new Sprite();
      addChild(container);
      container.x = 18;
      container.y = 18;
      bitmapDataList = new Array();
      for (var n:uint = 0; n < 24; n++) {
        var Photo:Object = getDefinitionByName("Photo" + (n + 1));
        var photo:BitmapData = new Photo(80, 80);
        var bitmap:Bitmap = new Bitmap(photo);
        bitmap.x = 81*(n%5);
        bitmap.y = 81*Math.floor(n/5);
        container.addChild(bitmap);
        bitmapDataList.push(photo);
      }
    }
    private function save(evt:MouseEvent):void {
      saver.save(bitmapDataList, 0, 50, "cat.gif");
    }
    private function complete(evt:Event):void {
    }

  }

}