ActionScript3(AS3)类库学习之(十二)——BitmapData类

AS3用BitmapData类来处理Bitmap对象的数据,通过直接操作 BitmapData 对象,我们可以创建复杂的图像。


一.构造BitmapData类


var myBitmap:BitmapData = new BitmapData(width:Number, height:Number, transparent:Boolean, fillColor:uinit);


通过这个构造函数,我们等于为BitmapData定义了下面的属性。

 

width:位图图像的宽度,以像素为单位。
height:位图图像的高度,以像素为单位。
rect:定义位图图像大小和位置的矩形。
transparent:定义位图图像是否支持每个像素具有不同的透明度。


位图属性在创建位图时定义,定义后只能获取,不能修改。


二.操作位图数据


a.进行一系列的位图数据操作之前应使用lock()来锁定图像,以使引用 BitmapData 对象的任何对象(如 Bitmap 对象)在此 BitmapData 对象更改时不会更新。在一系列的操作后再用unlock()来更新对象。


b.获取和设置单个像素的RGB值用getPixel和setPixel,获取和设置单个像素的RGBA值用getPixel32和setPixel32


c.获取和设置一组RGBA像素值用getPixels和setPixels.


三.复制位图数据

 

clone():将位图数据从一个 BitmapData 对象克隆或采样到另一个对象。调用此方法时,此方法返回一个新的 BitmapData 对象,它是与被复制的原始实例完全一样的克隆。
copyPixels():将像素从一个 BitmapData 对象复制到另一个 BitmapData 对象的快速且简便的方法。该方法会拍摄源图像的矩形快照(由 sourceRect 参数定义),并将其复制到另一个矩形区域(大小相等)。新“粘贴”的矩形的位置在 destPoint 参数中定义。
copyPixelsToByteArray():根据像素数据的矩形区域填充一个字节数组。
copyChannel():从源 BitmapData 对象中采集预定义的颜色通道值(Alpha、红、绿或蓝),并将此值复制到目标 BitmapData 对象的通道中。调用此方法不会影响目标 BitmapData 对象中的其他通道。
draw() 和 drawWithQuality():将源子画面、影片剪辑或其他显示对象中的图形内容绘制或呈现在新位图上。使用 matrix、colorTransform、blendMode 和目标 clipRect 参数,可以修改新位图的呈示方式。

 

四.生成位图数据

 

fillRect(rect:Rectangle, color:uint):void
使用指定的 ARGB 颜色填充一个矩形像素区域。
noise(randomSeed:int, low:uint = 0, high:uint = 255, channelOptions:uint = 7, grayScale:Boolean = false):void
使用表示随机杂点的像素填充图像。
perlinNoise(baseX:Number, baseY:Number, numOctaves:uint, randomSeed:int, stitch:Boolean, fractalNoise:Boolean, channelOptions:uint = 7, grayScale:Boolean = false, offsets:Array = null):void
生成 Perlin 杂点图像。可生成逼真、有机的纹理,是用于烟雾、云彩、水、火或爆炸的理想图案。

 


五.碰撞检测


hitTest(firstPoint:Point, firstAlphaThreshold:uint, secondObject:Object, secondBitmapDataPoint:Point = null, secondAlphaThreshold:uint = 1):Boolean

在一个位图图像与一个点、矩形或其他位图图像之间执行像素级的点击检测。在执行点击测试时,将不会考虑两个对象中任何一个对象的拉伸、旋转或其他变形。

如果某个图像是不透明图像,则此方法会将其视为完全不透明的矩形。两个图像必须是透明图像才能执行判断透明度的像素级点击测试。当您在测试两个透明图像时,Alpha 阈值参数将控制哪些 Alpha 通道值(从 0 到 255)将被视为是不透明的。

firstPoint:任意坐标空间中 BitmapData 图像的左上角的位置。在定义 secondBitmapPoint 参数时,使用了相同的坐标空间。
firstAlphaThreshold:透明度大于该值的像素被认为是不透明的。
secondObject:一个 Rectangle、Point、Bitmap 或 BitmapData 对象。
secondBitmapDataPoint:一个点,用于定义第二个 BitmapData 对象中的一个像素位置。仅当 secondObject 的值是 BitmapData 对象时使用此参数。
secondAlphaThreshold:透明度大于或等于该值的像素被视为不透明的。仅当 secondObject 的值是 BitmapData 对象,并且这两个 BitmapData 对象都为透明时使用此参数。 


六.图像转换

 

applyFilter(sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point, filter:BitmapFilter):void
取得一个源图像和一个滤镜对象,并生成过滤的图像。     
generateFilterRect(sourceRect:Rectangle, filter:BitmapFilter):Rectangle
已知 BitmapData 对象、源矩形和滤镜对象,确定 applyFilter() 方法调用所影响的目标矩形。
colorTransform(rect:Rectangle, colorTransform:flash.geom:ColorTransform):void
使用 ColorTransform 对象调整位图图像的指定区域中的颜色值。
floodFill(x:int, y:int, color:uint):void
对图像执行倾倒填充操作,从 (x, y) 坐标开始,填充一种特定的颜色。
merge(sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point, redMultiplier:uint, greenMultiplier:uint, blueMultiplier:uint, alphaMultiplier:uint):void
对每个通道执行从源图像向目标图像的混合。
paletteMap(sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point, redArray:Array = null, greenArray:Array = null, blueArray:Array = null, alphaArray:Array = null):void
重新映射一个具有最多四组调色板数据(每个通道一组)的图像中的颜色通道值。     
pixelDissolve(sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point, randomSeed:int = 0, numPixels:int = 0, fillColor:uint = 0):int
执行源图像到目标图像的像素溶解,或使用同一图像执行像素溶解。
threshold(sourceBitmapData:BitmapData, sourceRect:Rectangle, destPoint:Point, operation:String, threshold:uint, color:uint = 0, mask:uint = 0xFFFFFFFF, copySource:Boolean = false):uint
根据指定的阈值测试图像中的像素值,并将通过测试的像素设置为新的颜色值。


七.其他方法

 

scroll(x:int, y:int):void
按某一 (x, y) 像素量滚动图像。
dispose():void
释放用来存储 BitmapData 对象的内存。    
compare(otherBitmapData:BitmapData):Object
比较两个 BitmapData 对象。     
getColorBoundsRect(mask:uint, color:uint, findColor:Boolean = true):Rectangle
确定矩形区域是将位图图像中指定颜色的所有像素完全包括起来(如果将 findColor 参数设置为 true),还是将不包括指定颜色的所有像素完全包括起来(如果将 findColor 参数设置为 false)。
histogram(hRect:Rectangle = null):Vector.<Vector.<Number>>
计算 BitmapData 对象的 256 值二进制数直方图。
encode(rect:Rectangle, compressor:Object, byteArray:ByteArray = null):ByteArray
使用选定的压缩程序算法压缩此 BitmapData 对象,并返回一个新 ByteArray 对象。

 

posted @ 2013-04-18 21:21  Jingle Guo  阅读(1777)  评论(0编辑  收藏  举报