ActionScript 3 encoding JPEG, alchemy

Lets assume you want to save an image (or any DisplayObject) from flash to disk, or upload to facebook etc. What you need is to encode your BitmapData into an Jpeg and send ByteArray raw data to be saved/uploaded. There are few ways how to do it.

First, you can use original adobe Sadly, this encoder is the slowest. There were attemps to make it faster by otpimizing code by, that resulted into two times faster org.bytearray.images.JPEGEncoder. Usage is pretty simple:

import org.bytearray.JPEGEncoder;

function encode():ByteArray
    var bitmapData:BitmapData = new BitmapData(this.width, this.height, false);
    var encoder:JPEGEncoder = new JPEGEncoder(80);
    return encoder.encode(bitmapData);

But not fast enough :-). Since Flash Player 10 introduced alchemy (compiled C and C++ code within flash apps), it was possible to use optimized C++ encoder to do the work. Encoding time is more than 30 times faster than the original JPEGEncoder, pretty cool huh? Code looks like this

import cmodule.jpegencoder.CLibInit;

function encode():ByteArray
    var bitmapData:BitmapData = new BitmapData(this.width, this.height, false);
    var init:CLibInit = new CLibInit();
    var lib:Object = init.init();
    var byteArray:ByteArray = new ByteArray();
    var byteArrayOut:ByteArray = new ByteArray();
    byteArray = bitmapData.getPixels(bitmapData.rect);
    byteArray.position = 0;
    lib.encode(byteArray, byteArrayOut, bitmapData.width, bitmapData.height, 80);
    return byteArrayOut;

You can also run your encoding in asynchronous mode… Read more and see tests here:

5 comments so far

  1. Julio Ruiz October 13, 2009 18:30

    Hello, that’s excellent news, but have a few questions.

    Can I use this within a FP10 as3 player for the web, and if so how do I associate / import the classes of the SWC. I’ve added it to my external library path and flash is picking it up on export, but as soon as I try to encode something I get a CLBInit not found…

    Brilliant news this…



  2. Jozef Chúťka October 13, 2009 21:41

    Hi Julio,
    yes it can be used in flash player 10. To import .swc to flex go to:
    project / properties / flex build path / library path / add swc … ok
    clean your project after and you are ready to go

  3. Caroline October 14, 2009 20:40

    Hi there,
    Thanks for the info.. very helpful! I am wondering if you know whether it is possible to send jpegs to the fms 3.5, I know it is possible for vid and audio.. I would like to view them from flash app then. I can’t figure it out and badly need help!
    Thanks in advance!!

  4. Jozef Chúťka October 14, 2009 21:50

    Hi, yes you can send it to fms, if you want to have it without serverside scripting, you can use SharedObject and send data via byteArray or serialized to String ( , ). Other clients will see image after synchronization… good luck

  5. Klas Lundberg April 14, 2010 02:18

    I have just put up a tutorial/guide on how to use the Alchemy JPEG encoder in Flash. It’s also an example on how to use a progressbar to monitor the encoding. Check out

Leave a comment

Please be polite and on topic. Your e-mail will never be published.