• 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连接就没问题 } } }
  • 相关阅读:
    test
    【转载】ASP.NET MVC 3 —— Model远程验证
    【转载】富有客户端技术之——jQuery EasyUI
    【转载】基于ASP.NET Web Application的插件实现,附DEMO
    【转载】浅谈C#中的延迟加载(1)——善用委托
    【转载】Winform开发框架之权限管理系统
    【转载】基于我的Winform开发框架扩展而成的WCF开发框架
    [转载]10大优秀的移动Web应用程序开发框架推荐
    [转载]C#泛型列表List<T>基本用法总结
    [转载]推荐一个被大家忽视的微软的反跨站脚本库AntiXSS V3.1
  • 原文地址:https://www.cnblogs.com/kenkofox/p/3892900.html
Copyright © 2020-2023  润新知