Thursday, July 23, 2009

July 23rd

import flash.display.Bitmap; 
import flash.display.BitmapData; 

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;
}


function ns_onCuePoint(item:Object):void {
    trace("cuePoint");
    trace(item.name + "\t" + item.time);
}
//var snapshot : BitmapData; 

//snapshot = new BitmapData(myVideo.width,myVideo.height);  

//function takeSnapshot()  
//{  
  // snapshot.draw(myVideo);  
//}  
  
//call takeSnapshot every 100ms (0.1s)  
 
  
var now : BitmapData; 
var before: BitmapData;  
 
now = new BitmapData(video.width,video.height);  

//setInterval(this,"takeSnapshot",100);
//setInterval(this,"takeSnapshot",100); 

function takeSnapshot()  
{  
   now.draw(video);  
   // 1. capture 'now' 2. Do something 3. copy now to before   
   before=now.clone();  
 
//var histogram(hRect:Rectangle=null):Vector.> = BitmapData.histogram;  
var oldC = before.histogram();
var newC = now.histogram();
if (oldC != newC) {  
   trace('Activity... in region');  
}
}

function updateHistograms((bmpData:BitmapData, sprites:Vector.):void {

// histogram is a Vector in two dimensions
// notice the interesting type Vector.>
var hst:Vector.> = bmpData.histogram();

var height:int=128; // height for diagrams
var maxValue:Number;
var value:Number;
var i:int;
var clr:int = 16; // value for bitwise shift left, start with red
// channel = 0:red, 1:green, 2:blue

for (var channel:int = 0; channel!=3; channel++) {

// find the max value on the histogram,
// i goes from 1 to 254,
// I discarded the 0 and 0xFF values to get a better balanced chart

maxValue =0.0;
for (i=1; i!=255; i++) {
value = hst[channel][i];
maxValue = value>maxValue ? value : maxValue;
}

with (sprites[channel].graphics) {
// empty and white background
clear();
beginFill(0xFFFFFF,1.0);
drawRect(0,0,255,height);
// draw a vertical line for each value with a corresponding color

i=0;
for each(value in hst[channel]) {
lineStyle(1.0,i<
moveTo(i,height);
lineTo(i++,Math.max(0.0,height-value*height/maxValue) );
}

}
clr -= 8;
}
}


//function histogram(hRect:Rectangle = null):Vector



}

Today, I shall also compress video to a more suitable size.

No comments:

Post a Comment