Fast or asynchronous AS3 JPEG encoding

I am currently using JPGEncoderthe AS3 kernel to encode a bitmap in JPEG format

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

Since the bitmap is quite large (3000 x 2000), encoding takes a lot of time (about 20 seconds), which causes the application to seemingly freeze during encoding. To solve this problem I need:

  • Asynchronous encoder, so I can continue to refresh the screen (with a progress bar or something else) when encoding
  • Alternative encoder that is simply faster

Is it possible, and how can I do this?

+5
source share
5 answers

, , , .

Adobe

actionscript/flex, .

+2

You can use the alchemical encoder. It is very 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);
    }
}
}
+1
source

asynchronous decoding of png-image in a separate stream, supported by the 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 official.

+1
source

All Articles