//將可視的UIComponent組件轉換為ByteArray數組,我就是在UIComponent那裡放了一個圖片
public function BitmapDataToByteArray(target : UIComponent):ByteArray{
var imageWidth:uint = target.width;
var imageHeight:uint = target.height;
var srcBmp:BitmapData = new BitmapData( imageWidth, imageHeight );
//將組件讀取為BitmapData對象,bitmagData的資料來源
srcBmp.draw( target );
//getPixels方法用於讀取指定像素地區產生一個ByteArray,Rectangle是一個地區框,就是起始座標
var pixels:ByteArray = srcBmp.getPixels( new Rectangle(0,0,imageWidth,imageHeight) );
//下面倆行將資料來源的高和寬一起儲存到數組中,為翻轉的時候提供高度和寬度
pixels.writeShort(imageHeight);
pixels.writeShort(imageWidth);
return pixels;
}
//次方法的參數必須是像上面的ByteArray形式一樣的,即需要對象的大小;
//此方法返回的Bitmap可以直接賦值給Image的source屬性
public static function ByteArrayToBitmap(byArr:ByteArray):Bitmap{
if(byArr==null){
return null;
}
//讀取出存入時圖片的高和寬,因為是最後存入的資料,所以需要到尾部讀取
var bmd:ByteArray= byArr;
bmd.position=bmd.length-2;
var imageWidth:int = bmd.readShort();
bmd.position=bmd.length-4;
var imageHeight:int= bmd.readShort();
var copyBmp:BitmapData = new BitmapData( imageWidth, imageHeight, true );
//利用setPixel方法給圖片中的每一個像素賦值,做逆操作
//ByteArray數組從零開始儲存一直到最後都是圖片資料,因為讀入時的高和寬都是一樣的,所以當迴圈結束是正好讀完
bmd.position = 0;
for( var i:uint=0; i<imageHeight ; i++ )
{
for( var j:uint=0; j<imageWidth; j++ )
{
copyBmp.setPixel( j, i, bmd.readUnsignedInt() );
}
}
var bmp:Bitmap = new Bitmap( copyBmp );
return bmp;
}
public static const FORMAT_JPEG:uint = 0x00;
public static const FORMAT_PNG:uint = 0x01;
private static const EXT_JPEG:String = ".jpg";
private static const EXT_PNG:String = ".png";
private static const IMAGE_FOLDER:String = "SaveImage/images/";
private function saveImage(comp:DisplayObject,format:uint):void{
var bmpd:BitmapData = new BitmapData(comp.width,comp.height);
bmpd.draw(comp);
var imgByteArray:ByteArray;
var ext:String;
switch(format){
case FORMAT_JPEG:
ext = EXT_JPEG;
var jpgenc:JPEGEncoder = new JPEGEncoder(80);
imgByteArray = jpgenc.encode(bmpd);
break;
case FORMAT_PNG:
ext = EXT_PNG;
var pngenc:PNGEncoder = new PNGEncoder();
imgByteArray = pngenc.encode(bmpd);
break;
}
var fl:File = getNewImageFile(ext);
var fs:FileStream = new FileStream();
try{
fs.open(fl,FileMode.WRITE);
fs.writeBytes(imgByteArray);
fs.close();
this.updatephoto(fl);
}catch(e:Error){
trace(e.message);
}
}
private function getNewImageFile(ext:String):File{
var fileName:String = "image"+getNowTimestamp()+ext;
var fl:File = File.desktopDirectory.resolvePath(IMAGE_FOLDER+fileName);
if(fl.exists){
return getNewImageFile(ext);
}
return fl;
}
public function updatephoto(file:File):void
{
//showimgAvatar.bitmapData.
var urlrequest:URLRequest=new URLRequest(Config.API_UPDATEAVATAR);
var variables:URLVariables = new URLVariables();
variables.doctorID = doctorModel.doctorID;
urlrequest.data = variables;
urlrequest.method="post";
file.addEventListener(Event.COMPLETE, uploadComplete);
file.addEventListener(IOErrorEvent.IO_ERROR, ioError);
try {
file.upload(urlrequest);
} catch( e:Error ) {
trace( e );
}
}
private function getNowTimestamp():String{
var d:Date = new Date();
var tstamp:String = d.getFullYear().toString()+d.getMonth()+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()+d.getMilliseconds();
return tstamp;
}