package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.utils.ByteArray;
public class ByteArray_BitmapData extends Sprite
{
private var bys:ByteArray= new ByteArray();
public function ByteArray_BitmapData()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
super();
//加载图像文件
var request:URLRequest = new URLRequest("t.gif");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE , onComplete);
loader.load(request);
function onComplete( event:Event ):void{
var _loader:LoaderInfo = LoaderInfo(event.target);
var image:Bitmap = Bitmap(_loader.content);
addChild(image);
bys = encodeByteArray(image.bitmapData);
var bm:Bitmap = new Bitmap(encodeBitmapData(bys));
bm.x = 500 ;
addChild(bm)
}
}
private function encodeByteArray( bmd:BitmapData ):ByteArray{
var bytes:ByteArray = bmd.getPixels(bmd.rect)
bytes.writeShort(bmd.width);
bytes.writeShort(bmd.height);
bytes.writeBoolean(bmd.transparent);
bytes.compress();
return bytes;
}
private function encodeBitmapData( bytes:ByteArray ):BitmapData{
if(bytes == null){
throw new Error("bytes参数不能为空!");
}
bytes.uncompress();
if(bytes.length < 6){
throw new Error("bytes参数为无效值!");
}
bytes.position = bytes.length - 1;
var transparent:Boolean = bytes.readBoolean();
bytes.position = bytes.length - 3;
var height:int = bytes.readShort();
bytes.position = bytes.length - 5;
var width:int = bytes.readShort();
trace(width , height );
bytes.position = 0;
var data:ByteArray = new ByteArray();
bytes.readBytes(data,0 , bytes.length-5);
var bmd:BitmapData = new BitmapData(width , height , transparent) ;
bmd.setPixels(new Rectangle(0,0,width , height),data)
return bmd;
}
}
}
bytearray和bitmapdata之间的转换
最新推荐文章于 2024-02-04 00:15:00 发布