Tag Archives: Actionscript

Asynchronous Image Encoders

A while back i posted some ThreadedImageEncoder classes to convert BitmapData objects to .PNG or .JPG format over multiple Flash frames. With the arrival of ActionScript Workers i figured they would become obsolete, but we’re still waiting for Workers in mobile AIR so i’m still using them. Anyway, i needed a ‘real’ project to practice generating ASDocs with, so i chose the image encoder classes. I wasn’t planning on doing more than adding some comments, but i got carried away and ended up refactoring a lot of the code too.

Now they’re a bit more polished, i’ve renamed them to AsyncImageEncoders and given them their own github repository. In the repo you’ll find all the source code, a precompiled SWC, ASDocs for the classes and instructions on using them.

They’re easy to use:

//generate a BitmapData object to encode
var myBitmapData:BitmapData = new BitmapData(1000, 1000, true, 0x80FF9900);

//create an encoder
var encoder:IAsyncImageEncoder = new AsyncPNGEncoder();

//add listeners
encoder.addEventListener(AsyncImageEncoderEvent.PROGRESS, encodeProgressHandler);
encoder.addEventListener(AsyncImageEncoderEvent.COMPLETE, encodeCompleteHandler);

//start encoding for 20 milliseconds per frame
encoder.start(myBitmapData, 20);

function encodeProgressHandler(event:AsyncImageEncoderEvent):void
{
    //trace progress
    trace("encoding progress:", Math.floor(event.percentComplete)+"% complete");
}

function encodeCompleteHandler(event:AsyncImageEncoderEvent):void
{
    encoder.removeEventListener(AsyncImageEncoderEvent.PROGRESS, encodeProgressHandler);
    encoder.removeEventListener(AsyncImageEncoderEvent.COMPLETE, encodeCompleteHandler);
    //trace size of result
    trace("encoding completed:", encoder.encodedBytes.length+" bytes");
    //do something with the bytes...
    //..save to filesystem?
    //..upload to server?
    //..set as source for flex Image component?
}

There is also a stop() method if you get impatient with a long encode (large JPEGs on mobile can take ages) and an isRunning property which should need no explanation.

Lastly, the AsyncImageEncoderBase class can be extended to create other asynchronous encoders; hopefully, i will demonstrate that by adding a .BMP encoder in the next few days it took me 5 minutes to create a .BMP encoder.

JavaScript for Flashers Part 1: SWFObject

As fantastic as Flash is, it can’t do everything; sometimes we need to use other technologies to get results. For Flash running in a browser, JavaScript is a useful (and sometimes essential) tool.

I categorise my use of JavaScript into three areas: 1. Essential: embedding Flash in HTML (and providing alternative content), 2. Semi-essential: deep-linking (using browser back/next buttons for Flash navigation) and 3. Optional: everything else. I will cover 2 & 3 in later posts, but first…
Continue reading

Blatant Plugging

You may have noticed that the frequency of posts on this blog has dropped off recently. Mainly because i have been busy with a client project, but also because i was determined to finally get a personal Flex project finished – i got a sharp reminder to get on with it when i received an email reminding me to renew the domain name for another year.

Its not totally my fault – if Adobe are going to release Spark (Flex 4 SDK) and make it much better than Halo (Flex 3) then i can hardly be blamed for wanting to completely rewrite my app (and get to grips with Spark in the process). Anyway, only one year late on delivery – not bad for an IT project – it’s finally finished.
Continue reading