在Kinect for windows上,有三个摄像头,一个红外发射摄像头,一个红外摄像头 ,一个普通摄像头,这篇无博文就是说明关于普通摄像头采集视频数据的。另外还有深度数据采集(Depth Data)和骨骼数据(Skeleton Data)采集在后面几篇博文中进行说明。windows
- //Kinect对象
- KinectSensor kinectsensor = null;
- private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- if (kinectsensor.Status == KinectStatus.Connected)
- {
- kinectsensor.Stop();//中止Kinect
- }
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- foreach (KinectSensor ks in KinectSensor.KinectSensors)
- {
- if (ks.Status == KinectStatus.Connected)
- {
- kinectsensor = ks;
- //开启彩色流,参数为采集方式,分辨率640x480和采集频率每秒30帧
- kinectsensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
- //订阅采集事件
- kinectsensor.ColorFrameReady += kinectsensor_ColorFrameReady;
- kinectsensor.Start();//启动Kinect
- this.Title = "Kinect开始工做……";
- return;
- }
- }
- }
- byte[] colorPixels;//采集数据的字节数组
- WriteableBitmap colorBitmap;//位图对象
- void kinectsensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
- {
- using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
- {
- if (colorFrame != null)//判断彩色数据框架是否有数据
- {
- //初始化字节数据长度
- this.colorPixels = new byte[kinectsensor.ColorStream.FramePixelDataLength];
- //把数据复制到字节数组中
- colorFrame.CopyPixelDataTo(colorPixels);
- //实例化彩色位图
- colorBitmap = new WriteableBitmap(kinectsensor.ColorStream.FrameWidth, kinectsensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
- //加载字节数据数据到位图中
- this.colorBitmap.WritePixels(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight), colorPixels,colorBitmap.PixelWidth * sizeof(int),0);
- //把位图赋给图片控件,并显示出来
- img.Source = colorBitmap;
- }
- }
- }
结果以下:数组