Flash Actionscript 多线程Worker 压缩图片

package
{
        import flash.display.Bitmap;
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.external.ExternalInterface;
        import flash.geom.Rectangle;
        import flash.system.MessageChannel;
        import flash.system.Worker;
        import flash.system.WorkerDomain;
        import flash.utils.ByteArray;public class WorkerTest extends Sprite
       {
               protected var mainToWorker:MessageChannel;
               protected var workerToMain:MessageChannel;
              
               protected var worker:Worker;
              
              [ Embed(source= "1.jpg")]
               public var Image1:Class;
              
               public function WorkerTest()
              {
                      /**
                      * Start Main thread
                      **/
                      if(Worker.current.isPrimordial){
                            //Create worker from our own loaderInfo.bytes
                           worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes);
                           
                            //Create messaging channels for 2-way messaging
                           mainToWorker = Worker.current.createMessageChannel(worker);
                           workerToMain = worker.createMessageChannel(Worker.current);
                           
                            var byteArray:ByteArray = Bitmap(new Image1()).bitmapData.getPixels(new Rectangle(0,0,300,300));
                           byteArray. shareable = true;        //使bytearray可以在两个线程中使用
                           
                            //Inject messaging channels as a shared property
                           worker.setSharedProperty( "data", byteArray);
                           
                            //Listen to the response from our worker
                           workerToMain.addEventListener(Event.CHANNEL_MESSAGE, onWorkerToMain);
                           
                            //Start worker (re-run document class)
                           worker.start();
                           
                     }
                      /**
                      * Start Worker thread
                      **/
                      else {
                           
                            //Inside of our worker, we can use static methods to
                            //access the shared messgaeChannel's
                            var data:ByteArray = Worker.current.getSharedProperty( "data");
                           workerToMain = Worker.current.getSharedProperty( "workerToMain" );
                           
                //这里可以做一些压缩的工作
                data.compress(); workerToMain.send(data);  //只要byteArray是shareable的,可以直接send
} } //Messages to the worker thread protected function onWorkerToMain(event:Event): void { //Trace out whatever message the worker has sent us. var a:* = workerToMain.receive(); trace("[Worker] " + a); ExternalInterface.call( "alert", a); //debug版Flashbuilder4.6运行会收不到消息,但release版或断开debug连接就没问题 } } }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值