Fast or asynchronous AS3 JPEG encoding

天涯浪子 提交于 2019-12-29 07:05:31

问题


I'm currently using the JPGEncoder from the AS3 core lib to encode a bitmap to JPEG

 var enc:JPGEncoder = new JPGEncoder(90);
 var jpg:ByteArray = enc.encode(bitmap);

Because the bitmap is rather large (3000 x 2000) the encoding takes a long while (about 20 seconds), causing the application to seemingly freeze while encoding. To solve this, I need either:

  • An asynchronous encoder so I can keep updating the screen (with a progress bar or something) while encoding
  • An alternative encoder which is simply faster

Is either possible, and how can I do it?


回答1:


Setting up the encoder to be asynchronous would likely be your best bet.

Here are two examples from Adobe

This example is with actionscript/flex, but its the same idea.




回答2:


I found an asynchronous encoder: http://www.switchonthecode.com/tutorials/flex-tutorial-an-asynchronous-jpeg-encoder




回答3:


You can do it much faster with Alchemy: http://www.websector.de/blog/2009/06/21/speed-up-jpeg-encoding-using-alchemy/

http://segfaultlabs.com/devlogs/alchemy-asynchronous-jpeg-encoding-2




回答4:


You can use the alchemy encoder. It is really fast and you can encode images asynchronously. You can use this class to abstract it.

public class JPGAlchemyEncoder {

    private static var alchemyWrapper:Object;
    private var quality:Number;

    public function JPGAlchemyEncoder(quality:Number) {
        this.quality = quality;
        if (!alchemyWrapper){
            var loader:CLibInit = new CLibInit;
            alchemyWrapper = loader.init();
        }
    }

    public function encode(bitmapData:BitmapData):ByteArray{
        var data: ByteArray = bitmapData.clone().getPixels( bitmapData.rect );
        data.position = 0;
        return alchemyWrapper.write_jpeg_file(data, bitmapData.width, bitmapData.height, 3, 2, quality);
    }

    public function encodeAsync(bitmapData:BitmapData, completeHandler:Function):void{
        var encodedData:ByteArray = new ByteArray();
        var data: ByteArray = bitmapData.clone().getPixels(bitmapData.rect);
        data.position = 0;
        var encodeComplete:Function = function():void{
            completeHandler(encodedData);
        };
        alchemyWrapper.encodeAsync(encodeComplete, data, encodedData, bitmapData.width, bitmapData.height, quality);
    }
}
}



回答5:


asynchronous decode the png picture in separate thread ,supported by new version ...

var loaderContext:LoaderContext = new LoaderContext();
loaderContext.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD;

var loader:Loader = new Loader();
loader.load(new URLRequest("...png"),loaderContext);
addChild(loader);

that's official.



来源:https://stackoverflow.com/questions/2509554/fast-or-asynchronous-as3-jpeg-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!