forked from: 動体検知
まだまだ途中バージョン
作っている自分も良くわかってません。
// forked from ll_koba_ll's 動体検知
// write as3 code here..
//まだまだ途中バージョン
//作っている自分も良くわかってません。
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.Video;
import flash.text.TextField;
[SWF(width = "465", height = "465", frameRate = "30",backgroundColor="0x000000")]
public class blend extends Sprite
{
private var camera:Camera;
private var video:Video;
private var now:BitmapData;
private var before:BitmapData;
private var bmd:BitmapData;
private var rect:Rectangle;
private var pt:Point;
private var size:int = 465;
public function blend()
{
super();
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
/**
* 初期処理
* @param e
*
*/
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
camera = Camera.getCamera();
//カメラあり
if(camera != null){
setUpCamera();
//カメラ無し
}else{
var txt:TextField = new TextField();
txt.text ='カメラ無し';
addChild(txt);
}
bmd = new BitmapData(size,size/camera.width * camera.height,false,0x000000);
addChild(new Bitmap(bmd));
addEventListener(Event.ENTER_FRAME,loop);
/*
var btn:Sprite = new Sprite();
btn.graphics.beginFill(0xFFFFFF);
btn.graphics.drawRect(0,0,50,30);
btn.graphics.endFill();
btn.buttonMode = true;
btn.addEventListener(MouseEvent.CLICK,loop);
addChild(btn);
*/
now = new BitmapData(size,size/camera.width * camera.height,false,0x000000);
before = new BitmapData(size,size/camera.width * camera.height,false,0x000000);
rect = new Rectangle(0, 0, size,size/camera.width * camera.height);
pt = new Point(0, 0);
}
/**
*カメラのセットアップ
*
*/
private function setUpCamera():void
{
video = new Video(size,size/camera.width * camera.height);
video.attachCamera(camera);
addChild(video);
}
private function loop(e:Event=null):void
{
now.draw(video);
now.draw(before,new Matrix(), new ColorTransform(),BlendMode.DIFFERENCE);
var ret:uint = now.threshold(now,rect,pt,">",0xff111111,0xffffffff);
before.draw(video);
//bmd.draw(now);
//動体検知処理
//参照:http://void.heteml.jp/blog/archives/2007/10/as3_labeling.html
var dst:BitmapData = now.clone();
var lno:int = 0;
var zero:Point = new Point();
var rect:Rectangle = dst.getColorBoundsRect(0xffffff,0xffffff,true);
var drawMap:Sprite = new Sprite();
while( !rect.isEmpty()){
for(var x:int = rect.left; x<rect.right;x++){
if(dst.getPixel(x,rect.top) == 0xffffff){
dst.floodFill(x,rect.top,0xff00ff);
lno++;
rect = dst.getColorBoundsRect(0xffffff,0xffffff,true);
drawMap.graphics.lineStyle(1,0xff0000);
drawMap.graphics.drawRect(rect.x,rect.y,rect.width,rect.height);
}
}
}
//trace(lno);
bmd.draw(video);
bmd.draw(drawMap);
}
}
}