-
Notifications
You must be signed in to change notification settings - Fork 0
/
KinectDiagnosticViewer.xaml.cs
154 lines (134 loc) · 11.4 KB
/
KinectDiagnosticViewer.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//------------------------------------------------------------------------------
// <copyright file="KinectDiagnosticViewer.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.WpfViewers
{
using System.Collections.Generic;
using System.IO;
using System.Windows.Controls;
using Microsoft.Kinect;
/// <summary>
/// Interaction logic for KinectDiagnosticViewer.xaml
/// </summary>
public partial class KinectDiagnosticViewer : UserControl
{
private readonly KinectSettings kinectSettings;
private readonly Dictionary<KinectSensor, bool> sensorIsInitialized = new Dictionary<KinectSensor, bool>();
private KinectSensor kinect;
private bool kinectAppConflict;
public KinectDiagnosticViewer()
{
InitializeComponent();
this.kinectSettings = new KinectSettings(this);
this.kinectSettings.PopulateComboBoxesWithFormatChoices();
Settings.Content = this.kinectSettings;
this.KinectColorViewer = this.colorViewer;
this.StatusChanged();
}
public ImageViewer KinectColorViewer { get; set; }
public KinectSensor Kinect
{
get
{
return this.kinect;
}
set
{
if (this.kinect != null)
{
bool wasInitialized;
this.sensorIsInitialized.TryGetValue(this.kinect, out wasInitialized);
if (wasInitialized)
{
this.UninitializeKinectServices(this.kinect);
this.sensorIsInitialized[this.kinect] = false;
}
}
this.kinect = value;
this.kinectSettings.Kinect = value;
if (this.kinect != null)
{
if (this.kinect.Status == KinectStatus.Connected)
{
this.kinect = this.InitializeKinectServices(this.kinect);
if (this.kinect != null)
{
this.sensorIsInitialized[this.kinect] = true;
}
}
}
this.StatusChanged(); // update the UI about this sensor
}
}
public void StatusChanged()
{
if (this.kinectAppConflict)
{
status.Text = "KinectAppConflict";
}
else if (this.Kinect == null)
{
status.Text = "Kinect initialize failed";
}
else
{
this.status.Text = this.Kinect.Status.ToString();
if (this.Kinect.Status == KinectStatus.Connected)
{
// Update comboboxes' selected value based on stream isenabled/format.
this.kinectSettings.colorFormats.SelectedValue = this.Kinect.ColorStream.Format;
this.kinectSettings.depthFormats.SelectedValue = this.Kinect.DepthStream.Format;
this.kinectSettings.trackingModes.SelectedValue = KinectSkeletonViewerOnDepth.TrackingMode;
this.kinectSettings.UpdateUiElevationAngleFromSensor();
}
}
}
// Kinect enabled apps should customize which Kinect services it initializes here.
private KinectSensor InitializeKinectServices(KinectSensor sensor)
{
// Centralized control of the formats for Color/Depth and enabling skeletalViewer
sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
sensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);
this.kinectSettings.SkeletonStreamEnable.IsChecked = true; // will enable SkeletonStream if available
// Inform the viewers of the Kinect KinectSensor.
this.KinectColorViewer.Kinect = sensor;
KinectDepthViewer.Kinect = sensor;
KinectSkeletonViewerOnColor.Kinect = sensor;
KinectSkeletonViewerOnDepth.Kinect = sensor;
kinectAudioViewer.Kinect = sensor;
// Start streaming
try
{
sensor.Start();
this.kinectAppConflict = false;
}
catch (IOException)
{
this.kinectAppConflict = true;
return null;
}
sensor.AudioSource.Start();
return sensor;
}
// Kinect enabled apps should uninitialize all Kinect services that were initialized in InitializeKinectServices() here.
private void UninitializeKinectServices(KinectSensor sensor)
{
sensor.AudioSource.Stop();
// Stop streaming
sensor.Stop();
// Inform the viewers that they no longer have a Kinect KinectSensor.
this.KinectColorViewer.Kinect = null;
KinectDepthViewer.Kinect = null;
KinectSkeletonViewerOnColor.Kinect = null;
KinectSkeletonViewerOnDepth.Kinect = null;
kinectAudioViewer.Kinect = null;
// Disable skeletonengine, as only one Kinect can have it enabled at a time.
if (sensor.SkeletonStream != null)
{
sensor.SkeletonStream.Disable();
}
}
}
}