赤ペンせんせー
赤色検出あるだろーと思ってwonderflみたら無かったので書いてみる。
赤いペンを持ってカメラの前で動かしてみてください。右利き専用。
何かwonderflに移して来たらすごく重たい。困ったなー。
@author Takashi Murai(KAYAC)
/**
* Copyright Murai ( http://wonderfl.net/user/Murai )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2ZCy
*/
package {
///////
//赤色検出あるだろーと思ってwonderflみたら無かったので書いてみる。
//赤いペンを持ってカメラの前で動かしてみてください。右利き専用。
//何かwonderflに移して来たらすごく重たい。困ったなー。
///////
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.media.*;
/*
@author Takashi Murai(KAYAC)
*/
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="30")]
public class AkaPencilSenseI extends Sprite {
private var _cam:Camera;
private var _monitor:Video;
private var _src:BitmapData;
private var _result:BitmapData;
private var _chG:BitmapData;
private var _chB:BitmapData;
private var _m:Matrix;
private var _mirror:Matrix;
private var _pt:Point;
private var _rect:Rectangle;
private var _ct:ColorTransform;
private var _Layer:Sprite;
private var _prevPt:Point;
private var STG_W:uint=465;
private var STG_H:uint=465;
private var CAM_W:uint=240;
private var CAM_H:uint=240;
private var RATIO_X:Number =(STG_W/CAM_W);
private var RATIO_Y:Number =(STG_H/CAM_H);
private var _cnt:uint=0
public function AkaPencilSenseI() {
setup();
}
private function setup():void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
_cam = Camera.getCamera();
if (_cam != null) {
_cam.setMode(CAM_W,CAM_H,30);
_monitor = new Video(CAM_W, CAM_H);
_monitor.attachCamera(_cam);
} else {
trace("カメラツケテクダサーイ");
return;
}
_src = new BitmapData(STG_W, STG_H ,false,0xffffff);
_result = new BitmapData(STG_W, STG_H);
_chG = new BitmapData(STG_W, STG_H);
_chB = new BitmapData(STG_W, STG_H);
_mirror = new Matrix();
_mirror.scale(-1, 1);
_mirror.scale(RATIO_X, RATIO_Y);
_mirror.translate(CAM_W*RATIO_X,0);
_pt = new Point(0,0);
_rect = new Rectangle(0,0,STG_W, STG_H);
_ct = new ColorTransform();
_m = new Matrix();
_Layer=new Sprite();
_Layer.blendMode=BlendMode.ADD
_prevPt=new Point();
addChild(new Bitmap(_src));
addChild(_Layer);
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void {
_src.lock();
_result.lock();
_chG.lock();
_chB.lock();
_src.draw(_monitor,_mirror);
_result.draw(_monitor,_mirror);
_chG.copyChannel(_src, _rect, _pt, BitmapDataChannel.GREEN, BitmapDataChannel.RED);
_chB.copyChannel(_src, _rect, _pt, BitmapDataChannel.BLUE, BitmapDataChannel.RED);
_chB.draw(_chG, _m, _ct, BlendMode.LIGHTEN);
_result.draw(_chB, _m,_ct, BlendMode.SUBTRACT);
_result.threshold(_result, _rect, _pt, ">", 0xff500000, 0xffffffff);
_result.threshold(_result, _rect, _pt, "!=", 0xffffffff, 0xff000000);
_src.unlock();
_result.unlock();
_chG.unlock();
_chB.unlock();
var area:Rectangle=_result.getColorBoundsRect(0xffffffff, 0xffffffff,true);
// _Layer.graphics.beginFill(0x00000,0.1)
// _Layer.graphics.drawRect(0,0,STG_W,STG_H)
// _Layer.graphics.endFill()
if (!area.isEmpty()) {
var nPt:Point=new Point(area.x,(area.y+area.height));
if(_cnt>15){
_prevPt=nPt.clone();
_cnt=0
}
_Layer.graphics.lineStyle(4,0xff0000,1);
_Layer.graphics.moveTo(_prevPt.x, _prevPt.y);
_Layer.graphics.lineTo(nPt.x,nPt.y);
_prevPt=nPt.clone();
}
_cnt++
}
}
}