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

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

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

:caution: 要 Flash Player 10 以上

This movie requires Flash Player 10.0.0

JPGEncoder.as (com.adobe.images.JPGEncoder.as)
:check: as3corelib - Project Hosting on Google Code

JPGSaver.as (net.JPGSaver.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 com.adobe.images.JPGEncoder;

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

    // コンストラクタ
    public function JPGSaver() {
      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(bd:BitmapData, defaultName:String):void {
      var bitmapData:BitmapData = new BitmapData(bd.width, bd.height);
      bitmapData.lock();
      bitmapData.draw(bd);
      bitmapData.unlock();

      var encoder:JPGEncoder = new JPGEncoder(100);
      var byteArray:ByteArray = encoder.encode(bitmapData);

      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.JPGSaver;
  import controls.Btn;


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

  public class Main extends Sprite {
    // プロパティ
    private var saver:JPGSaver;
    private var saveBtn:Btn;
    private var bitmap:Bitmap;

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

    // メソッド
    private function init():void {
      saver = new JPGSaver();
      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);
      var photo:Photo = new Photo(400, 400);
      bitmap = new Bitmap(photo);
      addChild(bitmap);
      bitmap.x = 20;
      bitmap.y = 20;
    }
    private function save(evt:MouseEvent):void {
      var bitmapData:BitmapData = bitmap.bitmapData;
      saver.save(bitmapData, "piyo.jpg");
    }
    private function complete(evt:Event):void {
    }

  }

}