flash摄像头的使用

//摄像头的使用 具体代码
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
var my_Camera:Camera;
var my_Video:Video;
var my_BitmapDataBefore:BitmapData;
var my_BitmapDataCurrent:BitmapData;
var bitmapData:BitmapData;
var rectOne:Rectangle;
var rectTwo:Rectangle;
var time:Timer=new Timer(100,0);  
time.addEventListener(TimerEvent.TIMER,comparisonOnTime); 

//矩形框 
rectOne = new Rectangle(MovieClip(this.parent).jx1.x,MovieClip(this.parent).jx1.y,MovieClip(this.parent).jx1.width,MovieClip(this.parent).jx1.height);
rectTwo = new Rectangle(MovieClip(this.parent).jx2.x,MovieClip(this.parent).jx2.y,MovieClip(this.parent).jx2.width,MovieClip(this.parent).jx2.height);
var rectOne1:Rectangle = new Rectangle(rectOne.x,rectOne.y,rectOne.width/2,rectOne.height/2);
var rectOne2:Rectangle = new Rectangle(rectOne.x+rectOne.width/2,rectOne.y,rectOne.width/2,rectOne.height/2);
var rectOne3:Rectangle = new Rectangle(rectOne.x,rectOne.y+rectOne.height/2,rectOne.width/2,rectOne.height/2);
var rectOne4:Rectangle = new Rectangle(rectOne.x+rectOne.width/2,rectOne.y+rectOne.height/2,rectOne.width/2,rectOne.height/2);

var rectTwo1:Rectangle = new Rectangle(rectTwo.x,rectTwo.y,rectTwo.width/2,rectTwo.height/2);
var rectTwo2:Rectangle = new Rectangle(rectTwo.x+rectTwo.width/2,rectTwo.y,rectTwo.width/2,rectTwo.height/2);
var rectTwo3:Rectangle = new Rectangle(rectTwo.x,rectTwo.y+rectTwo.height/2,rectTwo.width/2,rectTwo.height/2);
var rectTwo4:Rectangle = new Rectangle(rectTwo.x+rectTwo.width/2,rectTwo.y+rectTwo.height/2,rectTwo.width/2,rectTwo.height/2);
//初始化摄像头
my_Camera= Camera.getCamera();
if (my_Camera != null) {
  my_Camera.addEventListener(StatusEvent.STATUS,loadCamera);
  my_Video = new Video(400,300);
  my_Video.clear();
  my_Video.attachCamera(my_Camera); 

  my_BitmapDataBefore = new BitmapData(my_Camera.width,my_Camera.height,true,0);
  my_BitmapDataBefore.draw(my_Video)
} else {
  trace("错误:找不到可用的摄像头");
}

//显示摄像头中的内容
function loadCamera(e:StatusEvent):void {
  my_Video.x = 0;
  my_Video.y = 0;
  my_Camera.setMode(my_Video.width,my_Video.height,24);
  addChild(my_Video);
  time.start();
}
function comparisonOnTime(e:TimerEvent){
    doClearance();
    my_BitmapDataCurrent = getCurrentBitmapData();
    //显示比较图片 (直接使用比较,效率比先赋值再使用要高一倍)
    if(!isAlickForTowBitmapData(my_BitmapDataBefore,my_BitmapDataCurrent,rectOne1)||!isAlickForTowBitmapData(my_BitmapDataBefore,my_BitmapDataCurrent,rectOne2)||!isAlickForTowBitmapData(my_BitmapDataBefore,my_BitmapDataCurrent,rectOne3)||!isAlickForTowBitmapData(my_BitmapDataBefore,my_BitmapDataCurrent,rectOne4)){//不像似则表示发生了移动

        MovieClip(MovieClip(this.parent).parent).block_1.visible=true ;

        }else{MovieClip(MovieClip(this.parent).parent).block_1.visible= false;}

    if(!isAlickForTowBitmapData(my_BitmapDataBefore,my_BitmapDataCurrent,rectTwo1)||!isAlickForTowBitmapData(my_BitmapDataBefore,my_BitmapDataCurrent,rectTwo2)||!isAlickForTowBitmapData(my_BitmapDataBefore,my_BitmapDataCurrent,rectTwo3)||!isAlickForTowBitmapData(my_BitmapDataBefore,my_BitmapDataCurrent,rectTwo4)){

       MovieClip(MovieClip(this.parent).parent).block_2.visible=true ;

        }else{MovieClip(MovieClip(this.parent).parent).block_2.visible= false;}
      my_BitmapDataBefore = my_BitmapDataCurrent.clone(); 
}

//捕获当前照片
function getCurrentBitmapData():BitmapData {
    if(!bitmapData){ bitmapData = new BitmapData(my_Camera.width,my_Camera.height,true,0);} 
    bitmapData.draw(my_Video);   
     if(bitmapData){return bitmapData;} 
     return my_BitmapDataBefore;
}

//比较两幅图指定位置是否相似
function isAlickForTowBitmapData(firstImage:BitmapData, lastImage:BitmapData,rect:Rectangle):Boolean{


    var rect:Rectangle = rect;
    var pt:Point = new Point(0, 0);
    var bdFirst:BitmapData = new BitmapData(rect.width,rect.height);
    bdFirst.copyPixels(firstImage, rect, pt);
    var bdLast:BitmapData = new BitmapData(rect.width,rect.height);
    bdLast.copyPixels(lastImage, rect, pt); 


   if(bdFirst.compare(bdLast)){ 
      var diffBmpData:BitmapData = bdFirst.compare(bdLast) as BitmapData; 
      var alikeCount:int = 0;
      var allCount:int = 0;
      var w:uint = diffBmpData.rect.width;
      var h:uint = diffBmpData.rect.height;
      for(var i:int=0;i<w-1;i=i+2){
          for(var j:int=0;j<h-1;j=j+2){
          if(diffBmpData.getPixel(i,j).toString(16)=="0"){alikeCount++;} 
           allCount++;
          } 
       } 
        diffBmpData.dispose();  
    }
//  释放内存
 bdFirst.dispose();
 bdLast.dispose();
//trace("相似度数"+allCount); 
 if(alikeCount/allCount>MovieClip(this.parent).slider.getSliderValue()){return true;}
 else{return false;}

}
 function doClearance() : void { 
     try{
          new LocalConnection().connect("foo");
          new LocalConnection().connect("foo");
     }catch(error : Error){}                        
 } 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值