首页 > 代码库 > Flash Actionscript 多线程Worker 压缩图片
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连接就没问题 } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。