//将可视的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;
}
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;
}