在Kinect for windows上,有三个摄像头,一个红外发射摄像头,一个红外摄像头 ,一个普通摄像头,这篇无博文就是说明关于普通摄像头采集视频数据的。另外还有深度数据采集(Depth Data)和骨骼数据(Skeleton Data)采集在后面几篇博文中进行说明。
新建一个 WPF项目,在窗体上放一个Image控件,Name为img。
-
- KinectSensor kinectsensor = null;
- private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- if (kinectsensor.Status == KinectStatus.Connected)
- {
- kinectsensor.Stop();
- }
- }
-
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- foreach (KinectSensor ks in KinectSensor.KinectSensors)
- {
- if (ks.Status == KinectStatus.Connected)
- {
- kinectsensor = ks;
-
- kinectsensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
-
- kinectsensor.ColorFrameReady += kinectsensor_ColorFrameReady;
- kinectsensor.Start();
- 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;
- }
- }
- }
结果如下:
本文转自桂素伟51CTO博客,原文链接: http://blog.51cto.com/axzxs/1184448,如需转载请自行联系原作者