Friday, July 17, 2009

July 17th

Added Code from http://actionscriptexamples.com/2008/02/26/loading-flv-files-in-actionscript-30-using-the-netconnection-and-netstream-classes/ as a new loader in order to facilate video access.

var video:Video = new Video();
addChild(video);
 
var nc:NetConnection = new NetConnection();
nc.connect(null);
 
var ns:NetStream = new NetStream(nc);
ns.client = {};
ns.client.onMetaData = ns_onMetaData;
ns.client.onCuePoint = ns_onCuePoint;
ns.play("640x480.flv");
 
video.attachNetStream(ns);
 
function ns_onMetaData(item:Object):void {
    trace("metaData");
    // Resize video instance.
    video.width = item.width;
    video.height = item.height;
    // Center video instance on Stage.
    video.x = (stage.stageWidth - video.width) / 2;
    video.y = (stage.stageHeight - video.height) / 2;
}

//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(video.width,video.height);  
 
function takeSnapshot()  
{  
   now.draw(video);  
// 1. capture 'now' 2. Do something 3. copy now to before   
   before=now.clone();  
}  

var oldC = computeAverageColour(before,10,10,20,20);  
var 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;  
}   

No comments:

Post a Comment