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:
Is either possible, and how can I do it?
I found an asynchronous encoder: http://www.switchonthecode.com/tutorials/flex-tutorial-an-asynchronous-jpeg-encoder
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.
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
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);
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With