[2011-3-9 12:59 ]As3.0中的位图(Bitmap/BitmapData)用法

时间:2022-07-06 17:03:18

1.位图使用(模糊)滤镜

//创建一个矩形区域的BitmapData 
var bmd:BitmapData = new BitmapData(80, 30, false, 0xefefef);

//画个红色的矩形
var rect:Rectangle = new Rectangle(20, 10, 40, 10); 
bmd.fillRect(rect, 0xFF0000);

//找到红色矩形的右上顶点 
var pt:Point = new Point(rect.left, rect.top);

//定义一个模糊滤镜 
var filter:BlurFilter = new BlurFilter(8,8,1);

//应用滤镜 
bmd.applyFilter(bmd, rect, pt, filter);

//创建一个位图对象,并加入到舞台 
var bm:Bitmap = new Bitmap(bmd);

bm.scaleX = bm.scaleY = 2.0; 
bm.x = stage.stageWidth/2 -bm.width/2; 
bm.y = stage.stageHeight/2 - bm.height/2 ;

addChild(bm);

2.像素拷贝及赋值

var bmd1:BitmapData = new BitmapData(90, 60, false, 0x00000000); 
var bmd2:BitmapData = bmd1.clone();//拷贝

//"画"一条白色的线 
for(var i:uint=10;i<=40;i++){
bmd1.setPixel32(i, i, 0xFFFFFFFF);
}

trace(bmd1.getPixel32(10, 10).toString(16)); // ffffffff 
trace(bmd2.getPixel32(10, 10).toString(16)); // ff000000

var bm1:Bitmap = new Bitmap(bmd1);
this.addChild(bm1); 
bm1.x =bm1.y = 5;

var bm2:Bitmap = new Bitmap(bmd2);
bm2.x = 105; 
bm2.y =5;
this.addChild(bm2);

3.颜色变换

var bmd:BitmapData = new BitmapData(80, 30, false, 0xFF0000);

var cTransform:ColorTransform = new ColorTransform(); 
cTransform.alphaMultiplier = 0.8;//设置透明度因子为0.8 
var rect:Rectangle = new Rectangle(0, 0, 40, 30);//定义左半边矩形区域 
bmd.colorTransform(rect, cTransform);//对rect区域应用colorTransform

var bm:Bitmap = new Bitmap(bmd);
addChild(bm);

bm.x = stage.stageWidth/2 - bm.width/2; 
bm.y = stage.stageHeight/2 - bm.height/2;

4.比较位图差异

var bmd1:BitmapData = new BitmapData(50, 50, true, 0xFFFFAA00); 
var bmd2:BitmapData = new BitmapData(50, 50, true, 0xCCFFAA00); 
var diffBmpData:BitmapData = BitmapData(bmd1.compare(bmd2)); 
var diffValue:String = diffBmpData.getPixel32(1, 1).toString(16); 
//解释:当二个位置尺寸大小相同,且仅alpha分量不同时,compare的值为 zzFFFFFF,其中zz为bmd1与bmd2的alpha分量差 
trace (diffValue); // 33ffffff

var bm1:Bitmap = new Bitmap(bmd1); 
addChild(bm1);
bm1.x = bm1.y = 5;
var bm2:Bitmap = new Bitmap(bmd2); 
addChild(bm2);
bm2.x = 60;
bm2.y = 5;

var bmDiff = new Bitmap(diffBmpData);
addChild(bmDiff); 
bmDiff.x = 115;
bmDiff.y = 5;

var bmd1:BitmapData = new BitmapData(50, 50, true, 0xFF00FF99); 
var bmd2:BitmapData = new BitmapData(50, 50, true, 0x99AA3366); 
var diffBmpData:BitmapData = BitmapData(bmd1.compare(bmd2)); 
var diffValue:String = diffBmpData.getPixel32(1, 1).toString(16); 
//解释:当二个位置尺寸大小相同,但RGB分量不同时,compare的值为 FFxxyyzz,其中xx,yy,zz分别为bm1与bm2的RGB分量差,同时alpha分量差将被忽略 
trace (diffValue); // ff56cc33

var bm1:Bitmap = new Bitmap(bmd1); 
addChild(bm1);
bm1.x = bm1.y = 5;
var bm2:Bitmap = new Bitmap(bmd2); 
addChild(bm2);
bm2.x = 60;
bm2.y = 5;

var bmDiff = new Bitmap(diffBmpData);
addChild(bmDiff); 
bmDiff.x = 115;
bmDiff.y = 5;

5.拷贝颜色通道

var bmd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x00FF0000);

var rect:Rectangle = new Rectangle(0, 0, 20, 40); 
var pt:Point = new Point(10, 10); 
bmd.copyChannel(bmd, rect, pt, BitmapDataChannel.RED, BitmapDataChannel.BLUE);//将红色通道复制到以(10,10)为顶点,宽为20,高为40的矩形区域的蓝色通道中

trace(bmd.getPixel32(10,10).toString(16));//ffff00ff,即矩形区域的最终颜色值为ff ff 00 ff(纯红 叠加 纯蓝)

var bm:Bitmap = new Bitmap(bmd);
this.addChild(bm);

6.截取位图的某一部分(像素)

var bmd1:BitmapData = new BitmapData(40, 40, false, 0x000000FF); 
var bmd2:BitmapData = new BitmapData(80, 40, false, 0x0000CC44);

var rect:Rectangle = new Rectangle(0, 0, 20, 20); 
var pt:Point = new Point(10, 10); 
bmd2.copyPixels(bmd1, rect, pt);//将bmd1中以pt为左上顶点的rect矩形像素复制到bmd2中

var bm1:Bitmap = new Bitmap(bmd1);
this.addChild(bm1); 
var bm2:Bitmap = new Bitmap(bmd2);
this.addChild(bm2);
bm2.x = 50;

7.将文本转换为位图

import flash.display.Bitmap; 
import flash.display.BitmapData;
import flash.text.TextField;

var tf:TextField = new TextField();
tf.text = "bitmap text";

var myBitmapData:BitmapData = new BitmapData(80, 20); 
myBitmapData.draw(tf);
var bmp:Bitmap = new Bitmap(myBitmapData); 
this.addChild(bmp);
bmp.x = stage.stageWidth/2 - bmp.width/2; 
bmp.y = stage.stageHeight/2 - bmp.height/2;

8.仿PS中的颜色填充工具

//准备一个40*40绿色背景的BitmapData 
var myBitmapData:BitmapData = new BitmapData(40, 40, false, 0x0000FF00);

//画二个有“交叉重叠”部分的“蓝色”矩形 
var rect:Rectangle = new Rectangle(0, 0, 25, 25); 
myBitmapData.fillRect(rect, 0x000000FF); 
rect = new Rectangle(20, 20, 20, 20); 
myBitmapData.fillRect(rect, 0x000000FF);

//从坐标(10,10)开始寻找与(10,10)坐标(像素点相同且连续的区域)填充红色 
myBitmapData.floodFill(10, 10, 0x00FF0000);

var bm:Bitmap = new Bitmap(myBitmapData);
addChild(bm);

bm.scaleX = bm.scaleY = 3.0;
bm.x = stage.stageWidth/2 - bm.width/2; 
bm.y = stage.stageHeight/2 - bm.height/2;

9.颜色融合

import flash.display.Bitmap; 
import flash.display.BitmapData;
import flash.geom.Rectangle; 
import flash.geom.Point;

var bmd1:BitmapData = new BitmapData(100, 80, true, 0xFF00FF00); 
var bmd2:BitmapData = new BitmapData(100, 80, true, 0xFFFF0000); 
var rect:Rectangle = new Rectangle(0, 0, 40, 40); 
var pt:Point = new Point(20, 20); 
var mult:uint = 0x80; // 50% ,各通道值均为128,即50% 
bmd1.merge(bmd2, rect, pt, mult, mult, mult, mult);

var bm1:Bitmap = new Bitmap(bmd1);
addChild(bm1); 
var bm2:Bitmap = new Bitmap(bmd2);
addChild(bm2);
bm2.x = 110;

//最终值 
//new redDest = [(redSrc * redMultiplier) + (redDest * (256 - redMultiplier))] / 256; 
trace(bmd1.getPixel32(20,20).toString(16)); //ff7f7f00

//解释: 
//返回值中Red分量为 7f = 0x00 * 0x80 + 0xff*(0x100-0x80)/0x100

10.噪点图

var bmd1:BitmapData = new BitmapData(80, 80); 
var bmd2:BitmapData = new BitmapData(80, 80);

var seed:int = int(Math.random() * int.MAX_VALUE); 
bmd1.noise(seed, 0, 0xFF, BitmapDataChannel.RED, false); 
bmd2.noise(seed, 0, 0xFF, BitmapDataChannel.RED, true);

var bm1:Bitmap = new Bitmap(bmd1);
this.addChild(bm1);
bm1.x = 10; 
bm1.y = 10;
var bm2:Bitmap = new Bitmap(bmd2);
this.addChild(bm2); 
bm2.x = 100;
bm2.y = 10;

stage.addEventListener(Event.ENTER_FRAME,enterFrameHandler);

function enterFrameHandler(e:Event):void{ 
seed = Math.floor(Math.random() * int.MAX_VALUE); 
bmd1.noise(seed, 0, 0xFF, BitmapDataChannel.RED, false); 
bmd2.noise(seed, 0, 0xFF, BitmapDataChannel.RED, true); 
bm1.bitmapData = bmd1;
bm2.bitmapData = bmd2;
}

11.另一种噪点图(有点类似卫星云图)

import flash.display.Bitmap; 
import flash.display.BitmapData;

var bmd:BitmapData = new BitmapData(200, 200, false, 0x00CCCCCC);

var seed:Number = Math.floor(Math.random() * 999999); 
var channels:uint = BitmapDataChannel.GREEN | BitmapDataChannel.BLUE; 
bmd.perlinNoise(100, 80, 6, seed, false, true, channels, false, null);

var bm:Bitmap = new Bitmap(bmd);
addChild(bm); 
bm.x = stage.stageWidth/2 - bm.width/2; 
bm.y = stage.stageHeight/2 - bm.height/2;

stage.addEventListener(Event.ENTER_FRAME,enterFrameHandler);

function enterFrameHandler(e:Event):void{ 
seed = Math.floor(Math.random() * 999999); 
bmd.perlinNoise(100, 80, 6, seed, false, true, channels, false, null); 
bm.bitmapData = bmd; 
}

12.像素融解

import flash.display.BitmapData; 
import flash.display.Bitmap;
import flash.geom.Point; 
import flash.geom.Rectangle;
import flash.utils.Timer; 
import flash.events.TimerEvent;

var rndColor:int = Math.random() * 0xffffffff;//随机背景色 
var fillColor:int = Math.random() * 0xffffffff;//随机填充色

var bmd:BitmapData = new BitmapData(100, 100, false, rndColor); 
var bitmap:Bitmap = new Bitmap(bmd);
addChild(bitmap);

bitmap.scaleX = bitmap.scaleY = 1.5; 
bitmap.x = stage.stageWidth/2 - bitmap.width/2; 
bitmap.y = stage.stageHeight/2 - bitmap.height/2;

var tim:Timer = new Timer(20);
tim.start(); 
tim.addEventListener(TimerEvent.TIMER, timerHandler);

function timerHandler(event:TimerEvent):void { 
var randomNum:Number = Math.floor(Math.random() * int.MAX_VALUE); 
dissolve(randomNum);
}

function dissolve(randomNum:Number):void { 
var rect:Rectangle = bmd.rect;
var pt:Point = new Point(0, 0); 
var numberOfPixels:uint = 100;//每次融解100个像素 
bmd.pixelDissolve(bmd, rect, pt, randomNum, numberOfPixels, fillColor); 
var grayRegion:Rectangle = bmd.getColorBoundsRect(0xFFFFFFFF, rndColor, true);

if(grayRegion.width == 0 && grayRegion.height == 0 ) { 
bmd.dispose();
rndColor = Math.random() * 0xffffffff; 
fillColor = Math.random() * 0xffffff; 
bmd = new BitmapData(100, 100, false, rndColor); 
bitmap.bitmapData = bmd;

//tim.stop();
}
}

13.查找满足条件的颜色并替换

import flash.display.BitmapData; 
import flash.display.Bitmap;
import flash.geom.Point; 
import flash.geom.Rectangle;
import flash.utils.Timer; 
import flash.events.TimerEvent;

var rndColor:int = Math.random() * 0xffffffff;//随机背景色 
var fillColor:int = Math.random() * 0xffffffff;//随机填充色

var bmd:BitmapData = new BitmapData(100, 100, false, rndColor); 
var bitmap:Bitmap = new Bitmap(bmd);
addChild(bitmap);

bitmap.scaleX = bitmap.scaleY = 1.5; 
bitmap.x = stage.stageWidth/2 - bitmap.width/2; 
bitmap.y = stage.stageHeight/2 - bitmap.height/2;

var tim:Timer = new Timer(20);
tim.start(); 
tim.addEventListener(TimerEvent.TIMER, timerHandler);

function timerHandler(event:TimerEvent):void { 
var randomNum:Number = Math.floor(Math.random() * int.MAX_VALUE); 
dissolve(randomNum);
}

function dissolve(randomNum:Number):void { 
var rect:Rectangle = bmd.rect;
var pt:Point = new Point(0, 0); 
var numberOfPixels:uint = 100;//每次融解100个像素 
bmd.pixelDissolve(bmd, rect, pt, randomNum, numberOfPixels, fillColor); 
var grayRegion:Rectangle = bmd.getColorBoundsRect(0xFFFFFFFF, rndColor, true);

if(grayRegion.width == 0 && grayRegion.height == 0 ) { 
bmd.dispose();
rndColor = Math.random() * 0xffffffff; 
fillColor = Math.random() * 0xffffff; 
bmd = new BitmapData(100, 100, false, rndColor); 
bitmap.bitmapData = bmd;

//tim.stop();
}
}

14.综合应用1:模仿MetaBall(当然这种方法并不精确)

var ballNum:uint=5; 
var balls:Array = new Array();
var sW:Number=stage.stageWidth; 
var sH:Number=stage.stageHeight;
var container:Sprite = new Sprite(); 
var bmd:BitmapData=new BitmapData(sW,sH,false,0x00000000); 
var bitmap:Bitmap;
var i:uint=0; 
var rect:Rectangle=new Rectangle(0,0,sW,sH);
var pt:Point=new Point(0,0); 
var filter:BlurFilter=new BlurFilter(15,15);

function init() { 
for (i=0; i<ballNum; i++) { 
var b:Ball=new Ball(30+Math.random()*40,0xffffff);
balls.push(b); 
b.x = (sW - b.width)*Math.random() + b.radius; 
b.y = (sH - b.width)*Math.random() + b.radius; 
b.vx=(Math.random()*2-1)*2;
b.vy=(Math.random()*2-1)*2; 
container.addChild(b);
}

bmd.draw(container); 
bmd.applyFilter(bmd, rect, pt, filter);
bitmap=new Bitmap(bmd); 
addChild(bitmap); 
addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}

function enterFrameHandler(e:Event):void { 
for (i=0; i<ballNum; i++) {
var b:Ball=balls[i];
b.x+=b.vx; 
b.y+=b.vy;
var adjust:uint=5;
if (b.x>=sW-b.radius-adjust) { 
b.x=sW-b.radius-adjust;
b.vx*=-1; 
} else if (b.x<b.radius+adjust) {
b.x=b.radius+adjust; 
b.vx*=-1;
}

if (b.y>=sH-b.radius-adjust) { 
b.y=sH-b.radius-adjust;
b.vy*=-1; 
} else if (b.y<b.radius+adjust) {
b.y=b.radius+adjust; 
b.vy*=-1;
}
}

bmd.dispose(); 
bmd=new BitmapData(sW,sH,false,0x00000000);
bmd.draw(container); 
bmd.applyFilter(bmd, rect, pt, filter);
bitmap.bitmapData=bmd;
}

init();