« File and FileStream within AIR | Main | AIR HTML Control »
Create Images with AIR and JPEGEncoder
| By Rich Tretola | February 25, 2008 | Print This Post
|
| 179 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:

- 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);
- }
- <?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>
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/