Calendar

February 2008
M T W T F S S
« Jan   Mar »
 123
45678910
11121314151617
18192021222324
2526272829  

Tag Cloud

Categories

Archives

Highest Rated

Most Viewed

Recent Posts

Recent Comments


« File and FileStream within AIR | Main | AIR HTML Control »

Create Images with AIR and JPEGEncoder

By Rich Tretola | February 25, 2008Print This Post Print This Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...
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:

snapit.jpg

  1. private function snagPic():void {
  2.     bitmapData = new BitmapData(this.width,this.height);
  3.     bitmapData.draw(this,new Matrix());
  4.     var bitmap : Bitmap = new Bitmap(bitmapData);
  5.     var jpg:JPEGEncoder = new JPEGEncoder();
  6.     var ba:ByteArray = jpg.encode(bitmapData);
  7.     newImage = File.desktopDirectory.resolvePath("Images/" + fileName.text + ".jpg");
  8.     fileStream = new FileStream();
  9.     fileStream.open(newImage, FileMode.UPDATE);
  10.     fileStream.writeBytes(ba);
  11. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
  3.     width="450" height="200">
  4.     <mx:Script>
  5.         <![CDATA[
  6.         import mx.graphics.codec.JPEGEncoder;
  7.         import mx.controls.Alert;
  8.  
  9.         import flash.display.Bitmap;
  10.       import flash.filesystem.*;
  11.  
  12.       private var bitmapData:BitmapData;
  13.         private var newImage:File;
  14.         private var fileStream:FileStream;
  15.  
  16.             private function init():void{
  17.                 fileStream = new FileStream();
  18.             }
  19.  
  20.             private function snagPic():void {
  21.             bitmapData = new BitmapData(this.width,this.height);
  22.                 bitmapData.draw(this,new Matrix());
  23.                 var bitmap : Bitmap = new Bitmap(bitmapData);
  24.                var jpg:JPEGEncoder = new JPEGEncoder();
  25.                 var ba:ByteArray = jpg.encode(bitmapData);
  26.                 newImage = File.desktopDirectory.resolvePath("Images/" + fileName.text + ".jpg");
  27.                 fileStream = new FileStream();
  28.                 fileStream.open(newImage, FileMode.UPDATE);
  29.                 fileStream.writeBytes(ba);
  30.             }
  31.  
  32.         ]]>
  33.     </mx:Script>
  34.     <mx:TextInput id="fileName" text="MyNewFile" x="78" y="50"/>
  35.     <mx:Button click="snagPic()" label="Snap Picture" x="246" y="50"/>
  36.     <mx:Label text="Images will be saved to:"  y="78" x="10"/>
  37.     <mx:Label text="{File.desktopDirectory.resolvePath('Images/').nativePath}"  x="10" y="104" width="428"/>
  38.     <mx:Label x="10" y="52" text="File Name"/>
  39. </mx:WindowedApplication>

Share/Save/Bookmark

Topics: Adobe AIR |

2 Responses to “Create Images with AIR and JPEGEncoder”

  1. gino8080 Says:
    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!

  2. everythingflex Says:
    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/

Comments