Capturing UIComponents as Bitmap or encoded ByteArray
Posted by cipri in AIR, Actionscript, Flex on April 25th, 2009
We found usefull to have a simple static class helping us to quick capture parts of the flex application(or specific components) as Bitmap or encoded ByteArray, ready to be sent server-side to be saved as image (or simply saved from AIR app). Here is the class, maybe you can find it usefull for your projects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.geom.Matrix; import flash.geom.Rectangle; import flash.utils.ByteArray; import mx.core.IUIComponent; import mx.graphics.codec.JPEGEncoder; import mx.graphics.codec.PNGEncoder; public class ScreenCapture { static private var bitmap:Bitmap; static private var bitmapData:BitmapData; static public const JPEG:String = "jpeg"; static public const PNG:String = "png"; /** * Return a bitmap instance based on params */ static public function getBitmap(area:Rectangle, fromTarget:IUIComponent):Bitmap { bitmapData = new BitmapData(area.width,area.height); bitmap = new Bitmap(bitmapData); var matrix:Matrix = new Matrix(); matrix.tx = area.x; matrix.ty = area.y; bitmapData.draw(fromTarget,matrix); return bitmap; } /** * returns a byte array encoded as specified in imageFormat argument * Values are JPEG and PNG. */ static public function getImage(area:Rectangle, fromTarget:IUIComponent,imageFormat:String="jpeg",quality:Number=80):ByteArray { getBitmap(area, fromTarget); if ( imageFormat == PNG) { return encodePNG(); } return encodeJPEG(quality); } /** * Encodes as jpeg with specified quality */ static private function encodeJPEG(quality:Number):ByteArray { return (new JPEGEncoder(quality)).encode(bitmapData); } /** * Encodes as png */ static private function encodePNG():ByteArray { return (new PNGEncoder()).encode(bitmapData); } } } |
and here is an example of use.
Enjoy it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Example function to get Bitmap object */ public function getCapture():void { var bitmap:Bitmap = ScreenCapture.getBitmap(new Rectangle(0,0,cmp.width,cmp.height),cmp); rawChildren.addChild(bitmap); } /** * Example function to get enoded ByteArray as jpg or png */ public function getImageEncoded(imageType:String):void { var bytearray:ByteArray = ScreenCapture.getImage(new Rectangle(0,0,cmp.width,cmp.height),cmp,ScreenCapture.JPEG,85); // var bytearray:ByteArray = ScreenCapture.getImage(new Rectangle(0,0,cmp.width,cmp.height),cmp,ScreenCapture.PNG); // .... rest of code to handle bytearray to be saved ...(server side or using AIR...) } |
New blog, old flash project
Posted by cipri in Actionscript, Flash, Php on April 24th, 2009
Hello world!
We just started our blog and we choose to post one of the old flash project (actionscript 2) we made ~1-1.5 years ago. The project is live only on our server, but anyway … enjoy it.
PS: “Save” functionality is not working, guess why…
More to come.
