我想检测使用C#实现的Kinect SDK v1.7抓住或释放的手状态,但是我厌倦了这个例外:
抛出异常:' System.InvalidOperationException'在 Microsoft.Kinect.Toolkit.Interaction.dll
代码:
private void InteractionStreamOnInteractionFrameReady(object sender,InteractionFrameReadyEventArgs e)
{
using (InteractionFrame frame = e.OpenInteractionFrame())
{
if (frame != null)
{
if (this.userInfos == null)
{
this.userInfos = new UserInfo[InteractionFrame.UserInfoArrayLength];
}
frame.CopyInteractionDataTo(this.userInfos);
}
else
{ return;}
}
foreach (UserInfo userInfo in this.userInfos)
{
foreach (InteractionHandPointer handPointer in userInfo.HandPointers)
{
string action = null;
switch (handPointer.HandEventType)
{
case InteractionHandEventType.Grip:
action = "gripped";
break;
case InteractionHandEventType.GripRelease:
action = "released";
break;
}
if (action != null)
{
string handSide = "unknown";
switch (handPointer.HandType)
{
case InteractionHandType.Left:
handSide = "left";
break;
case InteractionHandType.Right:
handSide = "right";
break;
}
if (handSide == "left")
{
if (action == "released")
{
// left hand released code here
//MessageBox.Show("left hand released");
System.Console.WriteLine("left hand released");
}
else
{
// left hand gripped code here
System.Console.WriteLine("left hand gripped");
}
}
else
{
if (action == "released")
{
// right hand released code here
System.Console.WriteLine("right hand released");
}
else
{
// right hand gripped code here
System.Console.WriteLine("right hand gripped");
}
}
}
}
}
}
private void WindowLoaded(object sender, RoutedEventArgs e)
{
// Create the drawing group we'll use for drawing
this.drawingGroup = new DrawingGroup();
// Create an image source that we can use in our image control
this.imageSource = new DrawingImage(this.drawingGroup);
// Display the drawing using our image control
Image.Source = this.imageSource;
sensor = KinectSensor.KinectSensors.FirstOrDefault();
if (null != this.sensor )
{
System.Console.WriteLine("Start streaming");
skeletons = new
Skeleton[sensor.SkeletonStream.FrameSkeletonArrayLength];
userInfos = new
UserInfo[InteractionFrame.UserInfoArrayLength];
this.sensor.SkeletonStream.Enable();
this.interactionStream = new InteractionStream(this.sensor,
new
DummyInteractionClient());
this.interactionStream.InteractionFrameReady +=
InteractionStreamOnInteractionFrameReady;
sensor.SkeletonFrameReady += SensorSkeletonFrameReady;
// Start the sensor !
try
{
this.sensor.Start();
}
catch (IOException)
{
//////// this.sensor = null;
}
}
if (null == this.sensor)
{
System.Console.WriteLine("NoKinectReady");
}
}