import fl.video.*;
import flash.display.BitmapData; 
var myVideo:Video;  
//var myCamera:Camera = Camera.get();  
//myVideo.attachVideo("640x480.flv");  
var snapshot : BitmapData; 
snapshot = new BitmapData(myVideo.width,myVideo.height);  
//function takeSnapshot()  
//{  
//   snapshot.draw(myVideo);  
//}  
// call takeSnapshot every 100ms (0.1s)  
//setInterval(this,"takeSnapshot",100);  
var now : BitmapData; 
var before: BitmapData;  
now = new BitmapData(myVideo.width,myVideo.height);  
function takeSnapshot2()  
{  
   now.draw(myVideo);  
// 1. capture 'now' 2. Do something 3. copy now to before   
   before=now.clone();  
}  
oldC = computeAverageColour(before,10,10,20,20);  
newC = computeAverageColour(now,10,10,20,20);  
if (oldC != newC) {  
   trace('Activity... in region');  
} 
function computeAverageColour(bm:BitmapData, x:int, y:int, w:int, h:int):Number{  
   var totalR:Number = 0; // 32-bit colour is formatted  
   var totalG:Number = 0; // 8 bits 8 bits 8 bits 8 bits  
   var totalB:Number = 0; //    A      R      G       B  
   var count:Number = 0;  
   for (var j:Number = y; j < (y + h); j++) {  
     for (var i:Number = x;  i < (x + w); i++) {  
        var col:Number = bm.getPixel(i, j);  
        var red:Number = (col & 0xFF0000) >> 16;  
        var green:Number = (col & 0x00FF00) >> 8;  
        var blue:Number = col & 0x0000FF;  
        totalR += red;  
        totalG += green;  
        totalB += blue;  
        count++;  
     }  
  }  
// Calculate the average of each colour channel  
  var tR:Number = Math.round(totalR/count);  
  var tG:Number = Math.round(totalG/count);  
  var tB:Number = Math.round(totalB/count);  
// Join them together  
  var targetCol:Number = tR <<>
  return  targetCol;  
}   
Experience issues with oldC and newC. Today I'll strive to resolve them.
 
No comments:
Post a Comment