zoukankan      html  css  js  c++  java
  • 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连接就没问题 } } }
  • 相关阅读:
    Spring Boot 使用 Dom4j XStream 操作 Xml
    Spring Boot 使用 JAX-WS 调用 WebService 服务
    Spring Boot 使用 CXF 调用 WebService 服务
    Spring Boot 开发 WebService 服务
    Spring Boot 中使用 HttpClient 进行 POST GET PUT DELETE
    Spring Boot Ftp Client 客户端示例支持断点续传
    Spring Boot 发送邮件
    Spring Boot 定时任务 Quartz 使用教程
    Spring Boot 缓存应用 Memcached 入门教程
    ThreadLocal,Java中特殊的线程绑定机制
  • 原文地址:https://www.cnblogs.com/kenkofox/p/3892900.html
Copyright © 2011-2022 走看看