在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;
- }
- }
- }
結果以下:數組