« File and FileStream within AIR | Main | AIR HTML Control »
Create Images with AIR and JPEGEncoder
| By Rich Tretola | February 25, 2008 | Print This Post
|
| 2,989 views |
I have had requests on how to take snap shot and save it to disk. So here is a vary basic example that takes a picture of itself and saves it to disk using the file name provided. Here is the guts of the function that saves the image:

1 2 3 4 5 6 7 8 9 10 11 | private function snagPic():void { bitmapData = new BitmapData(this.width,this.height); bitmapData.draw(this,new Matrix()); var bitmap : Bitmap = new Bitmap(bitmapData); var jpg:JPEGEncoder = new JPEGEncoder(); var ba:ByteArray = jpg.encode(bitmapData); newImage = File.desktopDirectory.resolvePath("Images/" + fileName.text + ".jpg"); fileStream = new FileStream(); fileStream.open(newImage, FileMode.UPDATE); fileStream.writeBytes(ba); } |
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 | <?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="450" height="200"> <mx:Script> <![CDATA[ import mx.graphics.codec.JPEGEncoder; import mx.controls.Alert; import flash.display.Bitmap; import flash.filesystem.*; private var bitmapData:BitmapData; private var newImage:File; private var fileStream:FileStream; private function init():void{ fileStream = new FileStream(); } private function snagPic():void { bitmapData = new BitmapData(this.width,this.height); bitmapData.draw(this,new Matrix()); var bitmap : Bitmap = new Bitmap(bitmapData); var jpg:JPEGEncoder = new JPEGEncoder(); var ba:ByteArray = jpg.encode(bitmapData); newImage = File.desktopDirectory.resolvePath("Images/" + fileName.text + ".jpg"); fileStream = new FileStream(); fileStream.open(newImage, FileMode.UPDATE); fileStream.writeBytes(ba); } ]]> </mx:Script> <mx:TextInput id="fileName" text="MyNewFile" x="78" y="50"/> <mx:Button click="snagPic()" label="Snap Picture" x="246" y="50"/> <mx:Label text="Images will be saved to:" y="78" x="10"/> <mx:Label text="{File.desktopDirectory.resolvePath('Images/').nativePath}" x="10" y="104" width="428"/> <mx:Label x="10" y="52" text="File Name"/> </mx:WindowedApplication> |
Share this Post
Topics: Adobe AIR |









May 18th, 2008 at 5:24 pm
nice tutorial!
but is possible to take a screenshot of the screen? like the desktop?
i mean something that is not rendered inside flash?
thank a lot!
May 18th, 2008 at 6:09 pm
No, this is not possible without the help of outside libraries. See this post where I used the Merapi project with AIR to accomplish this.
http://blog.everythingflex.com/2008/04/24/do-a-screen-capture-with-adobe-air/
October 12th, 2008 at 1:52 am
I’m writing an Air app to convert images to swf files. Everything works great except the swf files are about 100 times larger than if created by the Flash IDE. Is there anything else I can do to shrink the file size of the swf output?
Thanks! (See Code Below)
var file:File = directory;
var fs:FileStream = new FileStream();
fs.open( file, FileMode.WRITE );
/* compressed */
var swf_head : ByteArray = new ByteArray();
swf_head.endian = Endian.LITTLE_ENDIAN;
swf_head.writeBytes( (evt.target as LoaderInfo).bytes, 0, 8 );
swf_head[0] = 0×43; // ‘C’ letter;
swf_head.position = 0;
var swf_body : ByteArray = new ByteArray();
swf_body.endian = Endian.LITTLE_ENDIAN;
swf_body.writeBytes( (evt.target as LoaderInfo).bytes, 8 );
swf_body.position = 0;
swf_body.compress();
swf_body.position = 0;
fs.writeBytes( swf_head );
fs.writeBytes( swf_body );
fs.close();
February 3rd, 2009 at 1:09 pm
Thanks a lot,
This is really helpful for me.