5

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?

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301

5 Answers5

6

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

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
2

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.

Community
  • 1
  • 1
Jason
  • 2,503
  • 3
  • 38
  • 46
1

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);
    }
}
}
nakib
  • 4,304
  • 2
  • 28
  • 39
1

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.

phantomjia
  • 76
  • 3
1

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

forresto
  • 12,078
  • 7
  • 45
  • 64
  • You should not use Alchemy anymore. This is a deprecated feature as of Flash Player 11.2. – Warren Seine Jan 04 '12 at 13:43
  • There will be a new version of Alchemy for 11.2+ – forresto Jan 06 '12 at 09:57
  • But the old code will not work unless recompiled with the new version. – Warren Seine Jan 06 '12 at 20:26
  • "Starting with Flash Player 11.2 and AIR 3.2, content targeting Flash Player 11 and AIR 3 (i.e., content using SWF version 13 and above) will not support the experimental Alchemy prototype. Existing Alchemy experiments targeting Flash Player 10.x and AIR 2.x are unaffected." http://blogs.adobe.com/flashplayer/2011/09/updates-from-the-lab.html – forresto Jan 14 '12 at 20:09
  • Yes, that's my point: a .swc compiled with the experimental Alchemy will not work with Flash Player 11.2. You need to recompile your library to make it work with future Flash Players. – Warren Seine Jan 16 '12 at 17:41
  • not to mention, using Alchemy 2 along with Stage3D, depending on product revenue, may carry some significant licensing fees. http://www.adobe.com/devnet/flashplayer/articles/premium-features.html – ericsoco May 18 '12 at 22:31
  • 1
    @WarrenSeine Granted, it isn't good practice this point, but I rest my case that using the old Alchemy and _targeting_ Flash Player 10 still works on Flash Player 11.2+: Screenshot: http://imgur.com/9bWvk Demo: http://meemoo.org/iframework/#gist/2759842 – forresto May 20 '12 at 22:56