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 mx.graphics.codec.JPEGEncoder. Sadly, this encoder is the slowest. There were attemps to make it faster by otpimizing code by bytearray.org, 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);
    bitmapData.draw(this);
    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);
    bitmapData.draw(this);
    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…

    Cheers

    Julio

  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 ( http://blog.yoz.sk/2009/10/serialization/ , http://blog.yoz.sk/2009/10/sharedobject-goes-rangeerror-error-2006/ ). 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 http://last.instinct.se/graphics-and-effects/using-the-fast-asynchronous-alchemy-jpeg-encoder-in-flash/640

Leave a comment

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