-
package motioncap {
-
import flash.media.Video;
-
import flash.events.StatusEvent;
-
import flash.media.Camera;
-
import flash.display.MovieClip;
-
import flash.display.BitmapData;
-
import flash.geom.Matrix;
-
import flash.utils.Timer;
-
import flash.events.TimerEvent;
-
import flash.geom.Point;
-
-
/**
-
* @author dongkang 2008-02-25
-
*/
-
public class MotionCapture extends MovieClip {
-
-
private var w:uint;
-
private var h:uint;
-
private var tolerance : Number;
-
private var pixel:int;
-
private var startbool:Boolean;
-
private var cam : Camera;
-
private var vid : Video;
-
private var now:BitmapData;
-
private var before:BitmapData;
-
private var snaptimer:Timer;
-
private var motiontimer:Timer;
-
private var snapmat : Matrix;
-
private var mainStage:Object;
-
private var hit_arr:Array;
-
private var fl_arr:Array;// 동작인식된 점의 저장 장소
-
private var bbo:Boolean;
-
-
-
-
public function MotionCapture(_root:Object,_hit_arr:Array) {
-
bbo = false;
-
mainStage = _root;
-
hit_arr = _hit_arr;
-
w = 480;
-
h = 320;
-
tolerance = 50;
-
pixel = 10;
-
-
startbool = false;
-
-
cam = Camera.getCamera();
-
cam.addEventListener(StatusEvent.STATUS, statushandler);
-
-
vid = new Video();
-
vid.attachCamera(cam);
-
vid.visible = false;
-
fl_arr = new Array();
-
snaptimer = new Timer(100);
-
motiontimer = new Timer(3000);
-
-
}
-
private function statushandler(e:StatusEvent):void
-
{
-
-
-
if(cam.muted){return;}
-
-
cam.setMode(320,240,26,false);
-
mainStage.addChild(vid);
-
initialize();
-
-
cam.removeEventListener(StatusEvent.STATUS, statushandler);
-
}
-
private function initialize():void
-
{
-
snaptimer.addEventListener("timer", timerhandler);
-
snaptimer.start();
-
-
-
motiontimer.addEventListener(TimerEvent.TIMER, onMotionCapture);
-
motiontimer.start();
-
-
-
snapmat = new Matrix();
-
snapmat.scale(1.5,1.5);
-
now = new BitmapData(w,h);
-
-
var tim:Timer = new Timer(3000,10);
-
tim.start();
-
tim.addEventListener("timer",timhandler);
-
tim.addEventListener(TimerEvent.TIMER_COMPLETE, timCompletehandler);
-
-
-
}
-
private function timCompletehandler(e:TimerEvent):void{
-
//bbo = false;
-
-
}
-
-
private function timhandler(e:TimerEvent):void
-
{
-
bbo = true;
-
-
-
}
-
-
-
private function timerhandler(evt:TimerEvent):void
-
{
-
try{
-
-
-
before = now.clone();
-
now.draw(vid,snapmat);
-
-
-
// 현재 snapshot 에 대한 픽셀을 저장한다.
-
var nc:uint = new uint();
-
-
// 색깔 채널값을 설정한다.
-
var nr:Number = new Number();
-
var ng:Number = new Number();
-
var nb:Number = new Number();
-
var nl:Number = new Number();
-
// 각 채널에 대한 똑같은 값 지정
-
var bc:uint = new uint();
-
-
var br:Number = new Number();
-
var bg:Number = new Number();
-
var bb:Number = new Number();
-
var bl:Number = new Number();
-
-
// 두 밝기에 대한 차이를 저장한다.
-
var d:Number = new Number();
-
-
for(var xR:int=w; xR > 0;xR-=pixel)
-
{
-
for(var yR:int=1;yR<h;yR+=pixel)
-
{
-
-
-
nc=now.getPixel(xR,yR);
-
//trace(nc.toString(16));
-
nr=nc>>16&0xff;
-
ng=nc>>8&0xff;
-
nb=nc&0xff;
-
nl=Math.sqrt(nr*nr + ng*ng + nb*nb);
-
bc=before.getPixel(xR,yR);
-
br=bc>>16&0xff;
-
bg=bc>>8&0xff;
-
bb=bc&0xff;
-
bl=Math.sqrt(br*br + bg*bg + bb*bb);
-
d=Math.round(Math.abs(bl-nl));
-
-
if(d > tolerance)
-
{
-
-
-
if(bbo)
-
{
-
if(fl_arr.length < 2000)
-
{
-
var te:Ctext = new Ctext();
-
te.x = xR;
-
te.y = yR;
-
addChild(te);
-
fl_arr.push(te);
-
}
-
}
-
-
-
-
-
-
}
-
-
}
-
-
-
}
-
evt.updateAfterEvent();
-
before = now.clone();
-
}
-
catch(e:TypeError)
-
{
-
-
-
}
-
}
-
private function onMotionCapture(e:TimerEvent):void {
-
trace("전 점의 갯수 : "+fl_arr.length);
-
var len:int = fl_arr.length *2;
-
for (var q:int = 0; q < fl_arr.length; q++) {
-
try{
-
removeChild(fl_arr.pop());
-
}
-
catch(e:Error)
-
{
-
-
}
-
}
-
trace("후 점의 갯수 : "+fl_arr.length);
-
-
}
-
-
}
-
}