using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
namespace KinectColorViewer
{
/// 函数
public MainWindow() { InitializeComponent(); } //在Loaded事件的处理函数中添加KinectSensor对象的初始化代码 private void Grid_Loaded(object sender, RoutedEventArgs e) { kinectSensor = (from sensor in KinectSensor.KinectSensors where sensor.Status == KinectStatus.Connected select sensor).FirstOrDefault(); kinectSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); kinectSensor.Start();//启动Kinect设备 kinectSensor.ColorFrameReady += kinectSensor_ColorFrameReady; } //获取视频数据,并将获取到的数据显示出来 void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e) { using (ColorImageFrame imageFrame = e.OpenColorImageFrame()) { if(imageFrame != null) { this.pixelData = new byte[imageFrame.PixelDataLength]; imageFrame.CopyPixelDataTo(this.pixelData); this.ColorImage.Source = BitmapSource.Create(imageFrame.Width, imageFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixelData, imageFrame.Width * imageFrame.BytesPerPixel); } } } private void Grid_Unloaded(object sender, RoutedEventArgs e) { kinectSensor.Stop();//关闭Kinect设备 } }
}this
ColorImageFormat 的枚举项:
// 摘要:
// Image formats for the Color Image Stream. It represents Image Data format,
// Framerate, and Resolution.
public enum ColorImageFormat
{
// 摘要:
// The image format is undefined.
Undefined = 0,
//
// 摘要:
// RGB data (32 bits per pixel, layout corresponding to PixelFormats.Bgr32).
// Resolution of 640 by 480 at 30 Frames per second.
RgbResolution640x480Fps30 = 1,
//
// 摘要:
// RGB data (32 bits per pixel, layout corresponding to PixelFormats.Bgr32).
// Resolution of 1280 by 960 at 12 Frames per second.
RgbResolution1280x960Fps12 = 2,
//
// 摘要:
// The data is collected from the Kinect as YUV, but it is translated to: RGB
// data (32 bits per pixel, layout corresponding to PixelFormats.Bgr32). Resolution
// of 640 by 480 at 15 Frames per second.
YuvResolution640x480Fps15 = 3,
//
// 摘要:
// YUV data (32 bits per pixel, layout corresponding to D3DFMT_LIN_UYVY). Resolution
// of 640 by 480 at 15 Frames per second.
RawYuvResolution640x480Fps15 = 4,
//
// 摘要:
// Infrared data (16 bits per pxel) Resolution of 640 by 480 at 30 Frames per
// second.
InfraredResolution640x480Fps30 = 5,
//
// 摘要:
// Bayer data (8 bits per pixel, layout in alternating pixels of red, green
// and blue). Resolution of 640 by 480 at 30 Frames per second.
RawBayerResolution640x480Fps30 = 6,
//
// 摘要:
// Bayer data (8 bits per pixel, layout in alternating pixels of red, green
// and blue). Resolution of 1280 by 960 at 12 Frames per second.
RawBayerResolution1280x960Fps12 = 7,
}spa
我只想说:操蛋!上次安装的Kinect For Window SDK 的运行环境出错。。。害得我觉得本身的程序出错纠结了两天!!!最后卸载从新下载安装完果真没错。接下来就简单了。。。code