gpt4 book ai didi

c# - 检查kinect是否连接

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:51 25 4
gpt4 key购买 nike

我正在创建一个 WPF/C# 应用程序,它使用 kinect 来移动对象,但它也可以使用鼠标运行。目前我注释掉了 kinect 代码,因为它可以使用鼠标工作。我需要识别 kinect 是否已连接的方法,因此我不必注释掉代码以在未连接时使用鼠标(不会像当前那样抛出异常)并在连接时使用 kinect。
我该怎么做?
信息:我正在使用官方的 Microsoft Kinect SDK(大约一周前下载)

编辑 -
我正在使用这些

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;

无法添加引用和使用 Microsoft.Kinect,因为它会与其中一些产生冲突

编辑 -
Device dvc = new Device();
if (dvc.Count.Equals(0))
MessageBox.Show("apoellin");

我尝试了上面的代码,如果我在 Kinect 未连接的情况下使用任何 Kinect 代码,应用程序会崩溃并出现相同的错误

最佳答案

这是《Beginning Kinect Programming with the Microsoft SDK》一书中的代码,它很好地处理了这个问题

// (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();
}
}
}
}
}

关于c# - 检查kinect是否连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13360443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com