Exporting Images from AIR

March 1st, 2011 by aabhaanjan

333

Lets learn today to export images in png and jpeg format from AIR . It will be simillar to the drawing application where
we draw something and export it as images.

Step 1:
Download the Encoder classes from Adobe
For Saving the image we have used here File,FileStream and FileMode classes.
Here you can save your avatar in both png and jpeg format.

Step 2:
I have designed a mc MovieClip in the stage . Now I will choose the
different colors after completing the color changing option
will save the image in format of PNG or JPEG.

Here I will place color picker components for
Colour option for the MovieClip.

Step 3:
Many of have faced or facing the problem of transparency in PNG format files.
here is the solution add follwing parameter in the new instance of BitmapData
like I have added here :
‘new BitmapData(mc.width,mc.height,true,0xDEDEDE)’;

Here is your complete code :
[xml]
package{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import flash.events.Event;
import flash.display.BitmapData;
import flash.utils.ByteArray;
import fl.events.ColorPickerEvent;
import flash.geom.ColorTransform;
import flash.text.TextFormat;
import flash.text.Font;
/*

*/

import com.adobe.images.PNGEncoder;
import com.adobe.images.JPGEncoder;
import flash.display.MovieClip;
import fl.motion.Color;

public class Main extends MovieClip{
private var _fileRef:File = File.documentsDirectory;
private var imageByteArray:ByteArray;
private var _fileType:String;

function Main(){
init();
}

/*

*/
private function init():void{
handleColorpicker();
save_btn.addEventListener(MouseEvent.MOUSE_DOWN,onEncode);
}

private function handleColorpicker():void{
colorPickerShirt.addEventListener(ColorPickerEvent.CHANGE,onColorChange);

}
private function onEncode(e:MouseEvent):void{
switch(img_radio_btn.selected){
case true:
saveAsJPG();
break;
case false:
saveAsPNG();
break;
}
}

private function getBitmapData():BitmapData{
var bd:BitmapData = new BitmapData(mc.width,mc.height,true,0xDEDEDE);
bd.draw(mc);
return bd;
}
private function saveAsPNG():void{
imageByteArray = PNGEncoder.encode(getBitmapData());
browseForSave(“PNG”);
}
private function saveAsJPG():void{
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
imageByteArray = jpgEncoder.encode(getBitmapData());
browseForSave(“JPG”);
}
private function browseForSave(fileType:String):void{
_fileType = fileType;
_fileRef.browseForSave(“Save As ” + fileType);
_fileRef.addEventListener(Event.SELECT, onSaveAsSelected);
}
private function onSaveAsSelected(e:Event):void{
_fileRef = e.target as File;
var ext:String;
switch(_fileType){
case “JPG”:
ext = “.jpg”;
break;
case “PNG”:
ext = “.png”;
break;
}
if(_fileRef.extension == null){
_fileRef.nativePath += ext;
}
var stream:FileStream = new FileStream();
stream.open(_fileRef, FileMode.WRITE);
stream.writeBytes(imageByteArray, 0, imageByteArray.length);
stream.close();
}

private function onColorChange(e:ColorPickerEvent):void{

ChangeObjectColor(mc, e.target.selectedColor)

}

private function ChangeObjectColor(_mc:MovieClip,_color:uint):void{
var colorTransform:ColorTransform;
colorTransform = _mc.transform.colorTransform;
colorTransform.color = _color;
_mc.transform.colorTransform = colorTransform;
}

}
}
[/xml]