2012-12-04 66 views
0

我有一個程序檢查Kinect是否連接到計算機。但是,我真的不知道是否需要調用某個方法(我會這麼認爲)以及在哪裏?我附上了從Kinect入門書中獲得的代碼。謝謝!如何在Kinect SDK中調用方法?

using System; 
using System.Windows; 
using System.Windows.Controls; 
using Microsoft.Kinect; 

namespace test 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

     } 

    KinectSensor kinectSensor; 
    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      KinectSensor.KinectSensors.StatusChanged += Kinects_StatusChanged; 
      foreach (KinectSensor kinect in KinectSensor.KinectSensors) 
      { 
       if (kinect.Status == KinectStatus.Connected) 
       { 
        kinectSensor = kinect; 
        MessageBox.Show("Connected"); 
        break; 
       } 
      } 
      if (KinectSensor.KinectSensors.Count == 0) 
       MessageBox.Show("No Kinect Found"); 
      else 
       Initialize(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

    void Kinects_StatusChanged(object sender, StatusChangedEventArgs e) 
    { 
     switch (e.Status) 
     { 
      case KinectStatus.Connected: 
       if (kinectSensor == null) 
       { 
        kinectSensor = e.Sensor; 
        Initialize(); 
       } 
       break; 
      case KinectStatus.Disconnected: 
       if (kinectSensor == e.Sensor) 
       { 
        Clean(); 
        MessageBox.Show("Kinect was disconnected"); 
       } 
       break; 
      case KinectStatus.NotReady: 
       break; 
      case KinectStatus.NotPowered: 
       if (kinectSensor == e.Sensor) 
       { 
        Clean(); 
        MessageBox.Show("Kinect is not powered anymore."); 
       } 
       break; 
      default: 
       MessageBox.Show("Unhandled Status: " + e.Status); 
       break; 
     } 
    } 

    private void Initialize() 
    { 
     if (kinectSensor == null) 
      return; 
     kinectSensor.Start(); 
    } 

    private void Clean() 
    { 
     if (kinectSensor != null) 
     { 
      kinectSensor.Stop(); 
      kinectSensor = null; 
     } 
    } 

} 

}

回答

1

下載Kinect for Windows Developer Toolkit。在這裏有很多關於如何做多件事的例子,這會讓你開始並幫助你理解如何與Kinect對話。

一旦你連接到Kinect,你需要設置它,然後訂閱事件回調。你將得到一個如下所示的函數:

private void InitializeKinectServices(KinectSensorManager kinectSensorManager, KinectSensor sensor) 
{ 
    // configure the color stream 
    kinectSensorManager.ColorFormat = ColorImageFormat.RgbResolution640x480Fps30; 
    kinectSensorManager.ColorStreamEnabled = true; 

    // configure the depth stream 
    kinectSensorManager.DepthStreamEnabled = true; 

    kinectSensorManager.TransformSmoothParameters = 
     new TransformSmoothParameters 
     { 
      // as the smoothing value is increased responsiveness to the raw data 
      // decreases; therefore, increased smoothing leads to increased latency. 
      Smoothing = 0.5f, 
      // higher value corrects toward the raw data more quickly, 
      // a lower value corrects more slowly and appears smoother. 
      Correction = 0.5f, 
      // number of frames to predict into the future. 
      Prediction = 0.5f, 
      // determines how aggressively to remove jitter from the raw data. 
      JitterRadius = 0.05f, 
      // maximum radius (in meters) that filtered positions can deviate from raw data. 
      MaxDeviationRadius = 0.04f 
     }; 

    // configure the skeleton stream 
    sensor.SkeletonFrameReady += OnSkeletonFrameReady; 
    kinectSensorManager.SkeletonStreamEnabled = true; 

    // initialize the gesture recognizer 
    _gestureController = new GestureController(); 
    _gestureController.GestureRecognized += OnGestureRecognized; 

    kinectSensorManager.KinectSensorEnabled = true; 

    if (!kinectSensorManager.KinectSensorAppConflict) 
    { 
     // additional init 
    } 
} 

這是我的通用設置函數,它基於Developer Toolkit的示例。您將無法將其插入代碼中,並且可以正常工作。查看工具包中的示例可以幫助您瞭解發生的情況以及如何最好地管理它。

KinectExplorer示例是一個很好的整體項目。它也會讓你清楚地瞭解上述功能是如何工作的(它具有相同的功能)。