Flash 网格视频图像

Flash对视频语音的支持是非常不错,可以简单的获取到视频,之后您可以加工成自己喜欢的样子.
本文便是演示如何通过Flash将视频数据捕获到,并进行网格化处理.
首选,您的电脑上应该有视频头并且可以正常使用!


1.在Flash builder中建立action script工程,或在Flash cs4中建立相应的文档类.
2.建立类GridView,包 org.av
3.声明类属性
  private var numCols:int;   //列数
		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
package org.av
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.Camera;
	import flash.media.Video;
	
	import org.view.View;

	public class GridVideo extends View
	{
		private var numCols:int;
		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;
		
		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);
		}
		
		override protected function init(e:Event) : void{
			super.init(e);
			addEventListener(Event.ENTER_FRAME,enterFrame);
		}
		
		private function enterFrame(e:Event):void{
			var m:Matrix = new Matrix();
				m.createBox(-1,1,0,canvas.width,0);
			canvas.draw(v,m);
			show();
		}
		/**
		 * 分配到网格
		 * */
				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));
						
					grid[index] = bmp;
					//开启sprite操作
					var sprite:Sprite = new Sprite();
						sprite.x = i * (w+hgap);
						sprite.y = j * (h+vgap);
						sprite.addChild(bmp);
						sprite.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
						sprite.buttonMode = true;
						addChild(sprite);
				}
			}
		}
		/**
		 * 显示
		 * */
		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));
				}
			}
		}
	}
}
		private function mouseDown(e:MouseEvent):void{
			var sprite:Sprite = e.currentTarget as Sprite;
				sprite.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
				sprite.startDrag();
				
			setChildIndex(sprite,numChildren-1);
			
		}
		private function mouseUp(e:MouseEvent):void{
			var sprite:Sprite = e.currentTarget as Sprite;
				sprite.stopDrag();
				sprite.removeEventListener(MouseEvent.MOUSE_UP,mouseUp);
		}

//-----------View工具类,包结构为:org.view
package org.view
{
	import flash.display.Sprite;
	import flash.events.Event;

	public class View extends Sprite
	{
		public function View()
		{
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		protected function init(e:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE,init);
			addEventListener(Event.REMOVED_FROM_STAGE,deInit);
		}
		protected function deInit(e:Event):void{
			removeEventListener(Event.REMOVED_FROM_STAGE,deInit);
		}
	}
}

//------------最后用 文档类来测试效果:
package {
	import flash.display.Sprite;
	
	import org.av.GridVideo;
	

	[SWF(width=400,height=300)]
	public class animal extends Sprite
	{
		public function animal()
		{
                        //水平,竖直网格数
                        //水平,竖直网格的间隔
			var v:GridVideo = new GridVideo(10,10,1,1);
			addChild(v);
		}
	}
}


运行后的效果图:
Flash 网格视频图像

当然,如果您有足够的兴趣,完全可以从此类继续改造,变成您喜欢的游戏或应用.
代码是as3,可惜这里没as3代码的样式,先用java顶着....

你可能感兴趣的:(C++,c,C#,Flash,J#)