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();
//start encoding for 20 milliseconds per frame
trace("encoding progress:", Math.floor(event.percentComplete)+"% complete");
//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.
If Adobe’s recent clusterfuck hasn’t sent you running into the arms of html5 here are some nice resources you might like…
Following on from part one and two, we now deal with sending messages to specific peers in a large group (where large means 15+ members).
Following on from part 1, i have adapted the code to to enable message sending to specific peers (or in p2p jargon, direct routing). I have added a label to display the instances’ (truncated) ID and a dropdown list to select the recipient of the message:
Flash Player has provided an API for peer-to-peer communication since 10.1 (and in AIR since 2.0). P2P over the Internet requires some server-side technology (such as Flash Media Server) but on a local network it’s really easy; no server stuff and it can be set up in a few lines of code.
I’ve noticed that this question pops up from time to time on forums, and it’s very straight-forward to do; we can use the draw() method of BitmapData to copy any visual element (in this case, the stage itself) into a BitmapData object. Then we can use the getPixel() method to extract the colour.
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.