问题
I am creating a WPF/C# app that uses the kinect for moving objects but it also runs with using the mouse. Currently I comment out the kinect code for it work using the mouse. I need way to recognize whether the kinect is connected or not so I don't have to comment out code for it to use the mouse when it's not (without throwing an exception as it currently does) and use kinect when it does. How may I do that?? info: I am using official Microsoft Kinect SDKs (Downloaded about a week ago)
Edit-- I am using these
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using GridAnimationDemo;
using System.Windows.Threading;
using HtmlAgilityPack;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Net;
using Microsoft.Research.Kinect.Nui;
using Microsoft.Research.Kinect.Audio;
using Microsoft.Research.Kinect;
using Microsoft.Office.Interop.PowerPoint;
using System.Windows.Data;
using Microsoft.Research.Kinect.Samples.CursorControl;
using Coding4Fun.Kinect.Wpf;
using Coding4Fun;
using System.Speech.Synthesis;
Cannot add reference and use Microsoft.Kinect as it creates conflicts with some of these
EDIT--
Device dvc = new Device();
if (dvc.Count.Equals(0))
MessageBox.Show("apoellin");
I tried the above code and the app crashes with the same error it crashes if I use any Kinect code with Kinect not connected
回答1:
You are using a very out of date version of the Kinect for Windows SDK. The Microsoft.Research.Kinect
namespace is from the beta.
The latest SDK can be downloaded here:
http://www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx
After you do that, download the Developer Toolkit, also available from the link above. It contains multiple examples of how to do many tasks.
I strongly suggest looking at the Kinect Explorer example. This will show you how to use a data container called KinectSensorManager. This class is a data wrapper and not part of the SDK -- it helps manage the Kinect sensor. It is included in several of the Toolkit examples.
The class, among other things, fires events when the Kinect sensor state changes. So you can set your program up to initialize and uninitialized the Kinect in the appropriate event handlers.
回答2:
Here is code from the book "Beginning Kinect Programming with the Microsoft SDK", which handles this nicely
// (in your page/window constructor):
this.KinectDevice = KinectSensor.KinectSensors
.FirstOrDefault(x => x.Status == KinectStatus.Connected);
// (and create a property like this:)
public KinectSensor KinectDevice
{
get { return this._KinectDevice; }
set
{
if (this._KinectDevice != value)
{
//Uninitialize
if (this._KinectDevice != null)
{
this._KinectDevice.Stop();
this._KinectDevice.SkeletonFrameReady -= KinectDevice_SkeletonFrameReady;
this._KinectDevice.SkeletonStream.Disable();
this._FrameSkeletons = null;
}
this._KinectDevice = value;
//Initialize
if (this._KinectDevice != null)
{
if (this._KinectDevice.Status == KinectStatus.Connected)
{
this._KinectDevice.SkeletonStream.Enable();
this._FrameSkeletons = new
Skeleton[this._KinectDevice.SkeletonStream.FrameSkeletonArrayLength];
this.KinectDevice.SkeletonFrameReady +=
KinectDevice_SkeletonFrameReady;
ColorImageStream colorStream = this._KinectDevice.ColorStream;
colorStream.Enable();
this._ColorImageBitmap = new WriteableBitmap(colorStream.FrameWidth,
colorStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null);
this._ColorImageBitmapRect = new Int32Rect(0, 0, colorStream.FrameWidth,
colorStream.FrameHeight);
this._ColorImageStride = colorStream.FrameWidth * colorStream.FrameBytesPerPixel;
ColorImageElement.Source = this._ColorImageBitmap;
this._KinectDevice.ColorFrameReady += Kinect_ColorFrameReady;
this.ColorImageElement.Dispatcher.BeginInvoke(new Action(() =>
{
this._ColorImageBitmap = new WriteableBitmap(colorStream.FrameWidth,
colorStream.FrameHeight,
96, 96, PixelFormats.Bgr32,
null);
this._ColorImageBitmapRect = new Int32Rect(0, 0, colorStream.FrameWidth,
colorStream.FrameHeight);
this._ColorImageStride = colorStream.FrameWidth *
colorStream.FrameBytesPerPixel;
this._ColorImagePixelData = new byte[colorStream.FramePixelDataLength];
this.ColorImageElement.Source = this._ColorImageBitmap;
}));
this._KinectDevice.Start();
}
}
}
}
}
回答3:
If you are using the latest Windows SDK, you can check for value of Runtime.Kinects.Count
.
If value is 0 then no Kinects are connected -
if (Runtime.Kinects.Count == 0)
{
// No Kinects are connected
}
else
{
// Kinect is connecetd
}
回答4:
I'd just like to add an answer for the Kinect 2.0 SDK. Unfortunately the SDK no longer has the Runtime
namespace or any other ways for listing devices. However you can use WMI to determine if a Kinect 2.0 is connected.
For this to work you need to add a references to the System.Management library.
public static bool IsConnected()
{
// Use WMI to find devices with the proper hardware id for the Kinect2
// note that one Kinect2 is listed as three harwdare devices
string query = String.Format(WmiQuery, HardwareId);
using (var searcher = new ManagementObjectSearcher(query))
{
using (var collection = searcher.Get())
{
return collection.Count > 0;
}
}
}
private const string HardwareId = @"VID_045E&PID_02C4";
private const string WmiQuery = @"SELECT * FROM Win32_PnPEntity WHERE DeviceID LIKE '%{0}%'";
Update, since Microsoft discontinued its Kinect 2 for Windows, and now uses the Xbox One's Kinect for Windows, we have discovered not all Kinect 2's use the same ID. At the moment we use these Ids. This seems to work.
<!-- Kinect 2 For Xbox -->
USB\VID_045E&PID_02C4
<!-- Kinect 2 For Windows -->
USB\VID_045E&PID_02D9
来源:https://stackoverflow.com/questions/13360443/checking-if-kinect-is-connected-or-not