码迷,mamicode.com
首页 > 其他好文 > 详细

[ActionScript 3.0] AS3中的位图(BitmapData)应用

时间:2015-10-14 19:33:31      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

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

//创建一个矩形区域的BitmapData
var bmd:BitmapData = new BitmapData(80, 30, false, 0xffffff);
//画个红色的矩形
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 bmp:Bitmap = new Bitmap(bmd);
bmp.scaleX = bmp.scaleY = 2.0;
bmp.x = stage.stageWidth/2 -bmp.width/2;
bmp.y = stage.stageHeight/2 - bmp.height/2 ;
addChild(bmp);

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 bmp1:Bitmap = new Bitmap(bmd1);
this.addChild(bmp1);
bmp1.x =bmp1.y = 5;
 
var bmp2:Bitmap = new Bitmap(bmd2);
bmp2.x = 105;
bmp2.y =5;
this.addChild(bmp2);

3.颜色变换

var bmd:BitmapData = new BitmapData(80, 30, false, 0xFF0000);
var ct:ColorTransform = new ColorTransform();
ct.alphaMultiplier = 0.8;//设置透明度因子为0.8
//ct.color = 0xff00ff;
var rect:Rectangle = new Rectangle(0, 0, 40, 30);//定义左半边矩形区域
bmd.colorTransform(rect, ct);//对rect区域应用colorTransform
 
var bmp:Bitmap = new Bitmap(bmd);
addChild(bmp);
 
bmp.x = stage.stageWidth/2 - bmp.width/2;
bmp.y = stage.stageHeight/2 - bmp.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 bmp1:Bitmap = new Bitmap(bmd1);
addChild(bmp1);
bmp1.x = bmp1.y = 5;
var bmp2:Bitmap = new Bitmap(bmd2);
addChild(bmp2);
bmp2.x = 60;
bmp2.y = 5;
 
var bmpDiff = new Bitmap(diffBmpData);
addChild(bmpDiff);
bmpDiff.x = 115;
bmpDiff.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 bmp1:Bitmap = new Bitmap(bmd1);
addChild(bmp1);
bmp1.x = bmp1.y = 5;
var bmp2:Bitmap = new Bitmap(bmd2);
addChild(bmp2);
bmp2.x = 60;
bmp2.y = 5;
 
var bmpDiff = new Bitmap(diffBmpData);
addChild(bmpDiff);
bmpDiff.x = 115;
bmpDiff.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 bmp:Bitmap = new Bitmap(bmd);
this.addChild(bmp);

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 bmp1:Bitmap = new Bitmap(bmd1);
this.addChild(bmp1);
var bmp2:Bitmap = new Bitmap(bmd2);
this.addChild(bmp2);
bmp2.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 bmpd:BitmapData = new BitmapData(80, 20);
bmpd.draw(tf);
var bmp:Bitmap = new Bitmap(bmpd);
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 bmp:Bitmap = new Bitmap(myBitmapData);
addChild(bmp);
 
bmp.scaleX  = bmp.scaleY = 3.0;
bmp.x = stage.stageWidth/2 - bmp.width/2;
bmp.y = stage.stageHeight/2 - bmp.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 bmp1:Bitmap = new Bitmap(bmd1);
addChild(bmp1);
var bmp2:Bitmap = new Bitmap(bmd2);
addChild(bmp2);
bmp2.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 bmp1:Bitmap = new Bitmap(bmd1);
this.addChild(bmp1);
bmp1.x = 10;
bmp1.y = 10;
var bmp2:Bitmap = new Bitmap(bmd2);
this.addChild(bmp2);
bmp2.x = 100;
bmp2.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);
    bmp1.bitmapData = bmd1;
    bmp2.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 bmp:Bitmap = new Bitmap(bmd);
addChild(bmp);
bmp.x = stage.stageWidth/2 - bmp.width/2;
bmp.y = stage.stageHeight/2 - bmp.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);
    bmp.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.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.geom.Point;
import flash.geom.Rectangle;
 
var bmd1:BitmapData = new BitmapData(200, 200, true, 0xFFCCCCCC);
 
var seed:int = int(Math.random() * int.MAX_VALUE);
var channels:uint = BitmapDataChannel.RED | BitmapDataChannel.BLUE;
bmd1.perlinNoise(100, 80, 12, seed, false, true, channels, false, null);
 
var bitmap1:Bitmap = new Bitmap(bmd1);
addChild(bitmap1);
 
var bmd2:BitmapData = new BitmapData(200, 200, true, 0xFFCCCCCC);
var pt:Point = new Point(0, 0);
var rect:Rectangle = new Rectangle(0, 0, 200, 200);
var threshold:uint =  0x00800000;//50%的红色通道值 
var color:uint = 0x80FF0000;//替换后的颜色
var maskColor:uint = 0x00FF0000;//因为只查找红色通道,所以遮罩仅为纯红色通道
bmd2.threshold(bmd1, rect, pt, ">", threshold, color, maskColor, true);
 
var bitmap2:Bitmap = new Bitmap(bmd2);
bitmap2.x = bitmap1.x + bitmap1.width + 10;
addChild(bitmap2);
 
//addEventListener(Event.ENTER_FRAME,EnterFrameHandler);
 
function EnterFrameHandler(e:Event):void{
    seed = int(Math.random() * int.MAX_VALUE);
    bmd1.perlinNoise(100, 80, 12, seed, false, true, channels, false, null);
    bitmap1.bitmapData = bmd1;
    bmd2.threshold(bmd1, rect, pt, ">", threshold, color, maskColor, true);
    bitmap2.bitmapData = bmd2;
}

 

[ActionScript 3.0] AS3中的位图(BitmapData)应用

标签:

原文地址:http://www.cnblogs.com/frost-yen/p/4878036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!