[AIR3.0] Xtionを体験するのだ! (6)

そうだ! Xtionを体験してみよう! :bouzu:

AIRKinect (2.2) と OpenNI を使って、HandMagic をやってみるよ。 :boy:

Xtion_HandMagic31

:caution: 要 Flash Player 11 以上

This movie requires Flash Player 11.0.0

:bear: AIRKinect 2.x で、Xtion も動いちゃうよ!
:banana: [AIR3.0] Kinectを体験するのだ! (2) 参照。

Main.as
package {

  import flash.display.Sprite;
  import flash.display.BitmapData;
  import flash.display.Bitmap;
  import flash.events.Event;
  import flash.geom.Vector3D;
  import flash.geom.Rectangle;

  import com.as3nui.nativeExtensions.air.kinect.Kinect;
  import com.as3nui.nativeExtensions.air.kinect.KinectSettings;
  import com.as3nui.nativeExtensions.air.kinect.constants.CameraResolution;
  import com.as3nui.nativeExtensions.air.kinect.events.DeviceEvent;
  import com.as3nui.nativeExtensions.air.kinect.events.CameraImageEvent;
  import com.as3nui.nativeExtensions.air.kinect.events.UserEvent;
  import com.as3nui.nativeExtensions.air.kinect.data.User;
  import com.as3nui.nativeExtensions.air.kinect.data.SkeletonJoint;

  import effects.MagicLight;

  [SWF(backgroundColor="#000000", width="640", height="480", frameRate="30")]

  public class Main extends Sprite {
    // プロパティ
    private var device:Kinect;
    private var rgb:Bitmap;
    private var light:MagicLight;

    // コンストラクタ
    public function Main() {
      init();
    }

    // メソッド
    private function init():void {
      setup();
      if (Kinect.isSupported()) {
        initialize();
        trace("initialized");
      } else {
        trace("can’t connect");
      }
    }
    private function setup():void {
      rgb = new Bitmap(new BitmapData(640, 480, false, 0xFF000000));
      addChild(rgb);
      light = new MagicLight(new Rectangle(0, 0, 640, 480));
      addChild(light);
      light.start();
      light.setup({x: 320, y: 240});
    }
    private function initialize():void {
      device = Kinect.getDevice();
      device.addEventListener(DeviceEvent.STARTED, connected);
      device.addEventListener(DeviceEvent.STOPPED, disconnected);

      device.addEventListener(CameraImageEvent.RGB_IMAGE_UPDATE, rgbImage);
      device.addEventListener(UserEvent.USERS_ADDED, added);
      device.addEventListener(UserEvent.USERS_REMOVED, removed);

      var settings:KinectSettings = new KinectSettings();
      settings.rgbEnabled = true;
      settings.rgbResolution = CameraResolution.RESOLUTION_640_480;

      settings.userEnabled = true;
      settings.skeletonEnabled = true;

      device.start(settings);
      addEventListener(Event.ENTER_FRAME, update, false, 0, true);
    }
    private function connected(evt:DeviceEvent):void {
      trace("connected");
    }
    private function disconnected(evt:DeviceEvent):void {
      trace("disconnected");
    }
    private function rgbCamera(evt:CameraFrameEvent):void {
      var bitmapData:BitmapData = evt.imageData;
      rgb.bitmapData = bitmapData;
    }
    private function added(evt:UserEvent):void {
      trace("users: " + evt.users.length);
    }
    private function removed(evt:UserEvent):void {
      trace("removed");
    }
    private function update(evt:Event):void {
      container.removeChildren();
      for each (var user:User in device.users) {
        if (user.hasSkeleton) {
          var vector:Vector3D = kinectVector(user.rightHand);
          light.setup({x: vector.x, y: vector.y});
        }
      }
    }
    private function kinectVector(joint:SkeletonJoint):Vector3D {
      var px:Number = joint.position.rgb.x;
      var py:Number = joint.position.rgb.y;
      var pz:Number = joint.position.world.z;
      var vector:Vector3D = new Vector3D(px, py, pz);
      return vector;
    }

  }

}

MagicLight.as (effects.MagicLight.as) 指定した位置に光を生成・表示するクラス

libsフォルダに、airkinect-2-core-openni.ane を用意。ライブラリパスを通す。

CS6なら、パブリッシュするだけ。


光を生成・表示するクラスを変えると、違った効果も。
This movie requires Flash Player 11.0.0


:sun: 「forum.as3nui.com • View topic - Xtion + AIRKinect + Flash」に投稿しといた!