BitmapData
可以使用BitmapData对象加载图片,BitmapData允许使用像素层级来控制位图,其优势如下:
?
可以复制、粘贴整个图像、部分图案,或者是每一个独立的像素。可以鉴别或是改变像素或像素群组的颜色。可以应用滤镜。可以创建随机的像素(noise或perlin noise)等。可以把位图通过encodeBase64方法生成Base64编码字符串存入数据库,使用时通过decodeBase64还原成BitmapData位图对象就可以了。?1、使用BitmapData得到图像数据1.1、使用BitmapData的draw方法得到图像数据:[Bindable][Embed(source="assets/img/music.png")]private var img:Class;private function init():void{var bd:BitmapData = new BitmapData(img1.width, img1.height);bd.draw(img1);img2.source = new BitmapAsset(bd);}<s:VGroup id="vg" width="200" height="200"><s:Image id="img1" source="{img}" /><s:Image id="img2" /></s:VGroup>?1.2、在flex3中,也可以用如下方法得到图像数据:
private function init():void{var bd:BitmapData = Bitmap(img1.content).bitmapData;img2.source = new BitmapAsset(bd);}
[Bindable][Embed(source="assets/img/music.png")]private var img:Class;protected function btn_clickHandler(event:MouseEvent):void{var bd:BitmapData = new BitmapData(img1.width, img1.height);bd.draw(img1);//也可以使用JPEG格式 new JPEGEncoder(100);var encoder:PNGEncoder = new PNGEncoder();//转换为二进制数据var bytes:ByteArray = encoder.encode(bd);var base64:Base64Encoder = new Base64Encoder();base64.encodeBytes(bytes);//把ByteArray转为Base64编码的字符串var imgStr:String = base64.toString();ta.text = imgStr;}<s:Image id="img1" source="{img}" /><s:Button id="btn" label="Encode" click="btn_clickHandler(event)"/><s:TextArea id="ta" width="300" height="200" />
protected function btn1_clickHandler(event:MouseEvent):void{var base64Dec:Base64Decoder = new Base64Decoder();base64Dec.decode(ta.text);var bytes:ByteArray = base64Dec.toByteArray();img2.load(bytes);}<s:Image id="img1" source="{img}" /><s:Button id="btn" label="Encode" click="btn_clickHandler(event)"/><s:TextArea id="ta" width="300" height="200" /><s:Button id="btn1" label="Show" click="btn1_clickHandler(event)" /><mx:Image id="img2" />深入理解BitmapData和Bitmap类:BitmapData存储图片的像素数据,可以看做是加载的或动态创建的位图图像中包含的像素的照片快照。Bitmap可以看做是BitmapData对象的包装。多用于Flash中,通常的用法是将BitmapData作为参数实例化Bitmap类(new Bitmap(bitmapData),实例化得到的这个Bitmap对象就是一幅图片了,将该对象添加到舞台或sprite容器即可。例如:
var myImage:Bitmap = new Bitmap(myBitmapDataObject);addChild(myImage);Bitmap类不是InteractiveObject类的子类,因此它无法调度鼠标事件。可以使用包含Bitmap对象的容器(例如sprite)来调度鼠标事件。在Flex中,Bitmap是无法直接添加到Flex的舞台或容器中的,所以在得到BitmapData数据后一般会使用Image组件(Bitmap或BitmapAsset作为Image的source)将图片显示出来。加载ByteArray数据时也是用Image的load方法来显示图片。用Bitmap加载ByteArray数据代码如下:
protected function btn1_clickHandler(event:MouseEvent):void{var base64Dec:Base64Decoder = new Base64Decoder();base64Dec.decode(ta.text);var bytes:ByteArray = base64Dec.toByteArray();var load:Loader = new Loader();load.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);load.loadBytes(bytes);}private function onComplete(event:Event):void{var bm:Bitmap = event.target.content as Bitmap;var ui:UIComponent = new UIComponent();ui.addChild(bm);this.addChild(ui);}
private var jpegEnc:JPEGEncoder = new JPEGEncoder();private var pngEnc:PNGEncoder = new PNGEncoder();private function capImg(imgEnc:IImageEncoder):void{var snap:ImageSnapshot;snap = ImageSnapshot.captureImage(img1, 0 ,imgEnc);ta.text = ImageSnapshot.encodeImageAsBase64(snap);var bytes:ByteArray = snap.data as ByteArray;img2.load(bytes);}<s:Image id="img1" source="{img}" /><s:Button id="btn" label="capture jpeg" click="capImg(jpegEnc)"/><s:Button id="btn1" label="capture png" click="capImg(pngEnc)" /><s:TextArea id="ta" width="300" height="200" /><mx:Image id="img2" />
protected function img1_mouseMoveHandler(event:MouseEvent):void{var point:int = bd.getPixel(event.localX, event.localY);pl.setStyle("backgroundColor", point);ta.text = "#" + point.toString(16).toUpperCase();}<s:Image id="img1" source="{img}" mouseMove="img1_mouseMoveHandler(event)" /><s:Panel id="pl" width="200" height="200" /> <s:TextArea id="ta" width="300" height="200" />
[Bindable]private var acImage:ArrayCollection = new ArrayCollection();protected function btn_clickHandler(event:MouseEvent):void{var bd:BitmapData = new BitmapData(img1.width, img1.height);bd.draw(img1);var bm:Bitmap = new Bitmap(bd); acImage.addItem({image:bm});}<mx:Image id="img1" source="{img}" /><mx:Button id="btn" label="copy image" click="btn_clickHandler(event)" /><mx:Repeater id="repeat" dataProvider="{acImage}"><mx:Image source="{repeat.currentItem.image}" /></mx:Repeater>
protected function btn_clickHandler(event:MouseEvent):void{var bd:BitmapData = new BitmapData(img1.width, img1.height);bd.draw(img1);var rect:Rectangle = new Rectangle(0,0,100,100);var bd1:BitmapData = new BitmapData(rect.width, rect.height);var point:Point = new Point(0,0);bd1.copyPixels(bd,rect,point,null,null,false);var bm:Bitmap = new Bitmap(bd1);img2.load(bm);}<s:Image id="img1" source="{img}" /><s:Button id="btn" label="cut image" click="btn_clickHandler(event)" /><mx:Image id="img2" />使用copyPixels方法复制原始图像的一部分来实现截取,主要设置前三个参数:原始图像数据、复制区域大小、复制的起始点。