Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast or asynchronous AS3 JPEG encoding

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?

like image 259
Bart van Heukelom Avatar asked Mar 24 '10 16:03

Bart van Heukelom


5 Answers

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

like image 81
Bart van Heukelom Avatar answered Nov 15 '22 12:11

Bart van Heukelom


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.

like image 42
Jason Avatar answered Nov 15 '22 11:11

Jason


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

like image 29
forresto Avatar answered Nov 15 '22 13:11

forresto


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);
    }
}
}
like image 43
nakib Avatar answered Nov 15 '22 13:11

nakib


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.

like image 23
phantomjia Avatar answered Nov 15 '22 13:11

phantomjia