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;
}
}
}
}