前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flash Actionscript 多线程Worker 压缩图片

Flash Actionscript 多线程Worker 压缩图片

作者头像
用户1258909
发布2018-07-03 15:51:29
5030
发布2018-07-03 15:51:29
举报
文章被收录于专栏:拂晓风起
代码语言:javascript
复制
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连接就没问题
              }
       }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-08-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档