1: namespace ElBruno.KinectViewer
2: {
3: using System.Windows;
4: using Microsoft.Research.Kinect.Nui;
5:
6: /// <summary>
7: /// Interaction logic for MainWindow.xaml
8: /// </summary>
9: public partial class MainWindow
10: {
11: private Runtime kinect;
12: private InteropBitmapHelper imageHelper;
13: private RuntimeOptions RuntimeOptions { get; set; }
14:
15: public MainWindow()
16: {
17: InitializeComponent();
18: Loaded += MainWindowLoaded;
19: }
20:
21: void MainWindowLoaded(object sender, RoutedEventArgs e)
22: {
23: InitKinect();
24: }
25:
26: void InitKinect()
27: {
28: if (Runtime.Kinects.Count == 0)
29: return;
30: kinect = Runtime.Kinects[0];
31: RuntimeOptions = RuntimeOptions.UseDepthAndPlayerIndex |
32: RuntimeOptions.UseSkeletalTracking |
33: RuntimeOptions.UseColor;
34: kinect.Initialize(RuntimeOptions);
35: kinect.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240,
36: RuntimeOptions.HasFlag(RuntimeOptions.UseDepthAndPlayerIndex) ||
37: RuntimeOptions.HasFlag(RuntimeOptions.UseSkeletalTracking) ?
38: ImageType.DepthAndPlayerIndex : ImageType.Depth);
39: kinect.DepthFrameReady += this.KinectDepthFrameReady;
40: }
41:
42: void KinectDepthFrameReady(object sender, ImageFrameReadyEventArgs e)
43: {
44: var planarImage = e.ImageFrame.Image;
45: var depthImageHelper = new DepthImageHelper();
46: byte[] convertedDepthBits = depthImageHelper.ConvertDepthFrame(planarImage.Bits, RuntimeOptions);
47:
48: if (this.imageHelper == null)
49: {
50: this.imageHelper = new InteropBitmapHelper(planarImage.Width, planarImage.Height, convertedDepthBits);
51: this.DepthSensorViewer.Source = this.imageHelper.InteropBitmap;
52: }
53: else
54: {
55: this.imageHelper.UpdateBits(convertedDepthBits);
56: }
57: }
58: }
59: }