Flash对视频语音的支持是非常不错,可以简单的获取到视频,之后您可以加工成自己喜欢的样子.
本文便是演示如何通过Flash将视频数据捕获到,并进行网格化处理.
首选,您的电脑上应该有视频头并且可以正常使用!
1.在Flash builder中建立action script工程,或在Flash cs4中建立相应的文档类.
2.建立类GridView,包 org.av
3.声明类属性
private var numRows:int; //行数
private var numCells:int; //总网格数
private var grid:Vector.<Bitmap>; //网格
private var v:Video; //视频可视对象
private var c:Camera; //摄影机对象
private var canvas:BitmapData;
private var w:int; //网格的宽度,高度
private var h:int;
private var hgap:int; //网格的横纵间隔
private var vgap:int;
4.完成GridView类的构造方法:
public function GridVideo(rows:int,cols:int,hgap:int=5,vgap:int=5)
{
numCols = cols;
numRows = rows;
this.hgap = hgap;
this.vgap = vgap;
numCells = numCols * numRows;
grid = new Vector.<Bitmap>(numCells);
c = Camera.getCamera();
c.setCursor(true);
c.setMode(320,240,30);
c.setMotionLevel(85,100);
c.setQuality(0,100);
v = new Video();
v.attachCamera(c);
//视频的图像
canvas = new BitmapData(320,240,false);
canvas.floodFill(0,0,0xabcdef);
assignToGrid();
//
addEventListener(Event.ENTER_FRAME,enterFrame);
}
5.将 视频图像分割外单个小网格:
/**
* 分配到网格
* */
private function assignToGrid():void{
w = canvas.width/numRows;
h = canvas.height/numCols;
//序列的算法: 行标 * 列数 + 列标,
for(var i:int=0;i<numRows;i++){
for(var j:int=0;j<numCols;j++){
var index:int = i * numCols + j;
var bmp:Bitmap = new Bitmap(new BitmapData(w,h));
bmp.x = i * (w+hgap);
bmp.y = j * (h+vgap);
addChild(bmp);
grid[index] = bmp;
}
}
}
6.分别截取并显示单个网格的图像
/**
* 显示
* */
private function show():void{
for(var i:int=0;i<numRows;i++){
for(var j:int=0;j<numCols;j++){
var index:int = i * numCols + j;
var bmp:Bitmap = grid[index];
bmp.bitmapData.copyPixels(canvas,new Rectangle(i*w,j*h,w,h),new Point(0,0));
}
}
}
//-------------------------------------------------完整的代码如下:包结构:org.av
//-----------View工具类,包结构为:org.view
//------------最后用 文档类来测试效果:
运行后的效果图: