KINECT学习 "kinect for Windows SDK开发入门(二)"

 

//教程地址连接:Kinect for Windows SDK开发入门(二) 

//代码分析,绿色原文注释,红色后添加注释,灰色后添加测试代码

//创建WPF窗口程序,添加Microsoft.Kinect 引用,在主窗口中拖一个Label控件和 Image 控件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 wpfKinect
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //Kinectsensor对象
        private KinectSensor kinect;
        public KinectSensor Kinect
        {       
            get
            {
                return this.kinect;
            }       
            set
            {
                //如果带赋值的传感器和目前的不一样           
                if (this.kinect!=value)
                {
                    //如果当前的传感对象不为null
                    if (this.kinect!=null)
                    { 
                        //uninitailize当前对象

                        UninitializeKinectSensor(this.kinect);
                        this.kinect=null;   
                    }
                    //如果传入的对象不为空,且状态为连接状态
                    if (value!=null&&value.Status==KinectStatus.Connected)
                    {
                        this.kinect=value;

                        InitializeKinectSensor(this.kinect);
                    }
                }      
            }
        } 
        public MainWindow()
        {
            InitializeComponent();

           //窗口加载时,检索KINECT设备
            this.Loaded += (s, e) => DiscoverKinectSensor();   

           //关闭窗口时,把KINECT设备置空

            this.Unloaded += (s, e) => this.kinect = null;

           //上面添加监听事件使用Lambda 表达式 ,Lambda 表达式其实就是匿名函数,

           //详细内容参看这里 http://msdn.microsoft.com/zh-cn/library/bb397687.aspx
        }

        //检索设备
        private void DiscoverKinectSensor()
        {

            //添加监听KINECT设备状态变化事件
            KinectSensor.KinectSensors.StatusChanged += KinectSensors_StatusChanged;

             //返回第一个状态是连接的kinect设备
            this.Kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);

            //在窗口拖一个LABEL控件,显示KINECT连接状态
            if (Kinect != null)
            {

              //窗口启动时显示KINECT 连接状态
                this.label1.Content = "Kinect Connected!";
          
  }
        }
        private void KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case KinectStatus.Connected:      

                    if (this.kinect == null)
                    {
                        this.kinect = e.Sensor;

                    }
                    else
                    {
                        if (this.kinect.Status == KinectStatus.Connected)
                        { 
                            UninitializeKinectSensor(this.kinect);
                            this.kinect = e.Sensor;
                        }
                    }
                    InitializeKinectSensor(this.kinect);
                   //窗口上显示kinect连接状态
                    this.label1.Content = "Kinect Connected!";
                    break;
                case KinectStatus.Disconnected:
                    if (this.kinect == e.Sensor)
                    {
                        this.kinect = null;
                        this.kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
                        if (this.kinect == null)
                        {
                            //TODO:通知用于Kinect已拔出 
                            this.label1.Content = "Kinect Disconnected!!!!";
                        }
                    }
                    break;           
                //TODO:处理其他情况下的状态 
                default: 

                    //窗口上显示kinect当前状态
                   
this.label1.Content = e.Status;
                    break;
            }
        }

        private void InitializeKinectSensor(KinectSensor kinectSensor)
        {
            if (kinectSensor != null)
            {
                kinectSensor.ColorStream.Enable();
                kinectSensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
                kinectSensor.Start();
            }
        }
       
        private void UninitializeKinectSensor(KinectSensor kinectSensor)
        {
            if (kinectSensor != null)
            {
                kinectSensor.Stop();
                kinectSensor.ColorFrameReady -= new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
            }
        }

        void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame != null)
                {
                    byte[] pixelData = new byte[frame.PixelDataLength];
                    frame.CopyPixelDataTo(pixelData);

                    //Image控件
                    ColorImageElement.Source = BitmapImage.Create(frame.Width, frame.Height, 96, 96, PixelFormats.Bgr32, null, pixelData, frame.Width * frame.BytesPerPixel);
                }
            }
        }
    }
}

//执行上面代码后窗口显示"Kinect Connected!" 和彩色画面,

//然后断开KINECT USB连接,再连接USB,再把KINECT电源断开再连上,看看是不是有不同的状态显示。


 

你可能感兴趣的:(windows,object,null,lambda,byte,代码分析)