code on 2008-12-26
package {
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.*;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.MouseEvent;
import flash.events.EventDispatcher;
import flash.net.*;
import flash.utils.*;
import fl.transitions.*;
import fl.transitions.easing.*;
import flash.media.Camera;
import flash.media.Video;
import flash.events.ActivityEvent;
import flash.geom.*;
import flash.utils.ByteArray;
[SWF(width=600, height=600, backgroundColor=0x000000, frameRate=60)]
public class Main extends Sprite {
public static var _stage:Stage;
private const CAMERA_W:int = stage.stageWidth;
private const CAMERA_H:int = stage.stageHeight;
private var camera:Camera;
private var video:Video;
private var button:Sprite;
private var bmd:BitmapData;
private var pic:BitmapData;
private var now:BitmapData;
private var before:BitmapData;
private var rect:Rectangle;
private var pt:Point;
private var urlRequest:URLRequest;
private var urlLoader:URLLoader;
private var cnt:int=0
public function Main() {
Init()
}
private function Init():void {
camera = Camera.getCamera();
if (camera != null) {
setupCamera();
}
bmd = new BitmapData(CAMERA_W, CAMERA_H ,false,0xffffff);
var bmp:DisplayObject = addChild(new Bitmap(bmd));
//bmp.x = CAMERA_W + 10;
addEventListener(Event.ENTER_FRAME, loop);
//
pic = new BitmapData(CAMERA_W, CAMERA_H);
now = new BitmapData(CAMERA_W, CAMERA_H);
before = new BitmapData(CAMERA_W, CAMERA_H);
rect = new Rectangle(0, 0, CAMERA_W, CAMERA_H);
pt = new Point(0, 0);
}
private function setupCamera():void {
video = new Video(CAMERA_W, CAMERA_H);
video.attachCamera(camera);
addChild(video);
}
//差分抽出
private function loop(e:Event):void {
cnt++
pic.draw(video);
now.draw(video);
now.draw(before, new Matrix(), new ColorTransform(), BlendMode.DIFFERENCE);
now.threshold(now, rect, pt, ">", 0xff111111, 0xffffffff);
before.draw(video);
bmd.draw(now);
//60フレームごとに
if(cnt==60){
var hantei:int=getBitBlackArray(before)
cnt = 0
if(hantei>400){
//dispatchEvent(new MouseEvent(MouseEvent.CLICK));
}
}
}
//ビットマップデータを解析
function getBitBlackArray(bmd:BitmapData):int {
var bmd_cnt:int=0
for (var i:Number=0; i<bmd.height; i+=5) {
for (var j:Number=0; j<bmd.width; j+=5) {
var pixelColor:String = bmd.getPixel(j, i).toString(16);
if (pixelColor == "0") {
bmd_cnt++
}
}
}
return bmd_cnt;
}
}
}