我有一个程序可以检查 Kinect 是否连接到计算机。但是,我真的不知道我是否需要调用一个方法(我会假设如此)以及在哪里?我附上了从介绍 Kinect 书籍中获得的代码。谢谢!
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;
}
}
}
}