代码注释之kinect数据流对象操作

代码来源介绍

作者:   yangecnu(yangecnu's Blog on 博客园)
出处:http://www.cnblogs.com/yangecnu/
作品由yangecnu创作,采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 欢迎转载,但任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问或者授权方面的协商,请给我留言。

  private KinectSensor kinect;

        public KinectSensor Kinect
        {
            get { return this.kinect; }
            set
            {
                //如果带赋值的传感器和目前的不一样
                if (this.kinect != value)
                {
                    //如果当前的传感对象不为null
                    if (this.kinect != null)
                    {
                        //反初始化传感器数据流对象
                        UninitializeKinectSensor(this.kinect);
                        //uninitailize当前对象
                        this.kinect = null;
                    }
                    //如果传入的对象不为空,且状态为连接状态
                    if (value != null && value.Status == KinectStatus.Connected)
                    {
                        this.kinect = value;
                        //初始化传感器数据流对象
                        InitializeKinectSensor(this.kinect);
                    }
                }
            }
        }

 

//传感器数据流对象初始化函数
        private void InitializeKinectSensor(KinectSensor kinectSensor)
        {
            if (kinectSensor != null)
            {
                //让ColorStream对象可用
                kinectSensor.ColorStream.Enable();
                //当有新的数据帧可用时,触发kinectSensor_ColorFrameReady函数对数据进行处理输出
                kinectSensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
                //打开传感器,新数据帧达到时,就会触发frameready事件,该事件每秒触发30次
                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)
                {
                    //数据流对象的PixelDataLength属性返回数据和序列的具体大小
                    byte[] pixelData = new byte[frame.PixelDataLength];
                    //填充像素数据到byte数组pixelData
                    frame.CopyPixelDataTo(pixelData);
                    ColorImageElement.Source = BitmapImage.Create(frame.Width, frame.Height, 96, 96,
                                                                 PixelFormats.Bgr32, null, pixelData,
                                                                 frame.Width * frame.BytesPerPixel);

                }
            }
        }

你可能感兴趣的:(代码注释之kinect数据流对象操作)