18
18
19
19
namespace Kinect2Sample
20
20
{
21
+ public enum DisplayFrameType
22
+ {
23
+ Infrared ,
24
+ Color
25
+ }
21
26
22
27
public sealed partial class MainPage : Page , INotifyPropertyChanged
23
28
{
29
+ private const DisplayFrameType DEFAULT_DISPLAYFRAMETYPE = DisplayFrameType . Infrared ;
30
+
24
31
/// <summary>
25
32
/// The highest value that can be returned in the InfraredFrame.
26
33
/// It is cast to a float for readability in the visualization code.
@@ -66,9 +73,10 @@ public sealed partial class MainPage : Page, INotifyPropertyChanged
66
73
private string statusText = null ;
67
74
private WriteableBitmap bitmap = null ;
68
75
private FrameDescription currentFrameDescription ;
76
+ private DisplayFrameType currentDisplayFrameType ;
77
+ private MultiSourceFrameReader multiSourceFrameReader = null ;
69
78
70
79
//Infrared Frame
71
- private InfraredFrameReader infraredFrameReader = null ;
72
80
private ushort [ ] infraredFrameData = null ;
73
81
private byte [ ] infraredPixels = null ;
74
82
@@ -110,23 +118,11 @@ public MainPage()
110
118
// one sensor is currently supported
111
119
this . kinectSensor = KinectSensor . GetDefault ( ) ;
112
120
113
- // get the infraredFrameDescription from the InfraredFrameSource
114
- FrameDescription infraredFrameDescription = this . kinectSensor . InfraredFrameSource . FrameDescription ;
115
-
116
- // open the reader for the infrared frames
117
- this . infraredFrameReader = this . kinectSensor . InfraredFrameSource . OpenReader ( ) ;
121
+ SetupCurrentDisplay ( DEFAULT_DISPLAYFRAMETYPE ) ;
118
122
119
- // wire handler for frame arrival
120
- this . infraredFrameReader . FrameArrived += this . Reader_InfraredFrameArrived ;
123
+ this . multiSourceFrameReader = this . kinectSensor . OpenMultiSourceFrameReader ( FrameSourceTypes . Infrared | FrameSourceTypes . Color ) ;
121
124
122
- // allocate space to put the pixels being received and converted
123
- this . infraredFrameData = new ushort [ infraredFrameDescription . Width * infraredFrameDescription . Height ] ;
124
- this . infraredPixels = new byte [ infraredFrameDescription . Width * infraredFrameDescription . Height * BytesPerPixel ] ;
125
-
126
- // create the bitmap to display
127
- this . bitmap = new WriteableBitmap ( infraredFrameDescription . Width , infraredFrameDescription . Height ) ;
128
-
129
- this . CurrentFrameDescription = infraredFrameDescription ;
125
+ this . multiSourceFrameReader . MultiSourceFrameArrived += this . Reader_MultiSourceFrameArrived ;
130
126
131
127
// set IsAvailableChanged event notifier
132
128
this . kinectSensor . IsAvailableChanged += this . Sensor_IsAvailableChanged ;
@@ -140,35 +136,115 @@ public MainPage()
140
136
this . InitializeComponent ( ) ;
141
137
}
142
138
139
+ private void SetupCurrentDisplay ( DisplayFrameType newDisplayFrameType )
140
+ {
141
+ currentDisplayFrameType = newDisplayFrameType ;
142
+ switch ( currentDisplayFrameType )
143
+ {
144
+ case DisplayFrameType . Infrared :
145
+ FrameDescription infraredFrameDescription = this . kinectSensor . InfraredFrameSource . FrameDescription ;
146
+ this . CurrentFrameDescription = infraredFrameDescription ;
147
+ // allocate space to put the pixels being received and converted
148
+ this . infraredFrameData = new ushort [ infraredFrameDescription . Width * infraredFrameDescription . Height ] ;
149
+ this . infraredPixels = new byte [ infraredFrameDescription . Width * infraredFrameDescription . Height * BytesPerPixel ] ;
150
+ this . bitmap = new WriteableBitmap ( infraredFrameDescription . Width , infraredFrameDescription . Height ) ;
151
+ break ;
152
+
153
+ case DisplayFrameType . Color :
154
+ FrameDescription colorFrameDescription = this . kinectSensor . ColorFrameSource . FrameDescription ;
155
+ this . CurrentFrameDescription = colorFrameDescription ;
156
+ // create the bitmap to display
157
+ this . bitmap = new WriteableBitmap ( colorFrameDescription . Width , colorFrameDescription . Height ) ;
158
+ break ;
159
+
160
+ default :
161
+ break ;
162
+ }
163
+ }
164
+
143
165
private void Sensor_IsAvailableChanged ( KinectSensor sender , IsAvailableChangedEventArgs args )
144
166
{
145
167
this . StatusText = this . kinectSensor . IsAvailable ? "Running" : "Not Available" ;
146
168
}
147
169
148
- private void Reader_InfraredFrameArrived ( object sender ,
149
- InfraredFrameArrivedEventArgs e )
170
+ private void Reader_MultiSourceFrameArrived ( MultiSourceFrameReader sender , MultiSourceFrameArrivedEventArgs e )
150
171
{
151
- bool infraredFrameProcessed = false ;
172
+ MultiSourceFrame multiSourceFrame = e . FrameReference . AcquireFrame ( ) ;
152
173
153
- // InfraredFrame is IDisposable
154
- using ( InfraredFrame infraredFrame = e . FrameReference . AcquireFrame ( ) )
174
+ // If the Frame has expired by the time we process this event, return.
175
+ if ( multiSourceFrame == null )
155
176
{
156
- if ( infraredFrame != null )
157
- {
158
- FrameDescription infraredFrameDescription =
159
- infraredFrame . FrameDescription ;
160
-
161
- // verify data and write the new infrared frame data to the display bitmap
162
- if ( ( ( infraredFrameDescription . Width * infraredFrameDescription . Height )
163
- == this . infraredFrameData . Length ) &&
164
- ( infraredFrameDescription . Width == this . bitmap . PixelWidth ) &&
165
- ( infraredFrameDescription . Height == this . bitmap . PixelHeight ) )
177
+ return ;
178
+ }
179
+
180
+ switch ( currentDisplayFrameType )
181
+ {
182
+ case DisplayFrameType . Infrared :
183
+ using ( InfraredFrame infraredFrame = multiSourceFrame . InfraredFrameReference . AcquireFrame ( ) )
166
184
{
167
- // Copy the pixel data from the image to a temporary array
168
- infraredFrame . CopyFrameDataToArray ( this . infraredFrameData ) ;
185
+ ShowInfraredFrame ( infraredFrame ) ;
186
+ }
187
+ break ;
188
+ case DisplayFrameType . Color :
189
+ using ( ColorFrame colorFrame = multiSourceFrame . ColorFrameReference . AcquireFrame ( ) )
190
+ {
191
+ ShowColorFrame ( colorFrame ) ;
192
+ }
193
+ break ;
194
+ default :
195
+ break ;
196
+ }
197
+ }
198
+
199
+ private void ShowColorFrame ( ColorFrame colorFrame )
200
+ {
201
+ bool colorFrameProcessed = false ;
169
202
170
- infraredFrameProcessed = true ;
203
+ if ( colorFrame != null )
204
+ {
205
+ FrameDescription colorFrameDescription = colorFrame . FrameDescription ;
206
+
207
+ // verify data and write the new color frame data to the Writeable bitmap
208
+ if ( ( colorFrameDescription . Width == this . bitmap . PixelWidth ) && ( colorFrameDescription . Height == this . bitmap . PixelHeight ) )
209
+ {
210
+ if ( colorFrame . RawColorImageFormat == ColorImageFormat . Bgra )
211
+ {
212
+ colorFrame . CopyRawFrameDataToBuffer ( this . bitmap . PixelBuffer ) ;
213
+ }
214
+ else
215
+ {
216
+ colorFrame . CopyConvertedFrameDataToBuffer ( this . bitmap . PixelBuffer , ColorImageFormat . Bgra ) ;
171
217
}
218
+
219
+ colorFrameProcessed = true ;
220
+ }
221
+ }
222
+
223
+ if ( colorFrameProcessed )
224
+ {
225
+ this . bitmap . Invalidate ( ) ;
226
+ FrameDisplayImage . Source = this . bitmap ;
227
+ }
228
+ }
229
+
230
+ private void ShowInfraredFrame ( InfraredFrame infraredFrame )
231
+ {
232
+ bool infraredFrameProcessed = false ;
233
+
234
+ if ( infraredFrame != null )
235
+ {
236
+ FrameDescription infraredFrameDescription = infraredFrame . FrameDescription ;
237
+
238
+ // verify data and write the new infrared frame data to the display bitmap
239
+ if ( ( ( infraredFrameDescription . Width * infraredFrameDescription . Height )
240
+ == this . infraredFrameData . Length ) &&
241
+ ( infraredFrameDescription . Width == this . bitmap . PixelWidth ) &&
242
+ ( infraredFrameDescription . Height == this . bitmap . PixelHeight ) )
243
+ {
244
+ // Copy the pixel data from the image to a temporary array
245
+ infraredFrame . CopyFrameDataToArray ( this . infraredFrameData ) ;
246
+
247
+ infraredFrameProcessed = true ;
172
248
}
173
249
}
174
250
@@ -196,7 +272,7 @@ private void ConvertInfraredDataToPixels()
196
272
197
273
// 3. limiting the value to InfraredOutputValueMaximum
198
274
intensityRatio = Math . Min ( InfraredOutputValueMaximum , intensityRatio ) ;
199
-
275
+
200
276
// 4. limiting the lower value InfraredOutputValueMinimum
201
277
intensityRatio = Math . Max ( InfraredOutputValueMinimum , intensityRatio ) ;
202
278
@@ -217,5 +293,15 @@ private void RenderPixelArray(byte[] pixels)
217
293
FrameDisplayImage . Source = this . bitmap ;
218
294
}
219
295
296
+ private void InfraredButton_Click ( object sender , RoutedEventArgs e )
297
+ {
298
+ SetupCurrentDisplay ( DisplayFrameType . Infrared ) ;
299
+ }
300
+
301
+ private void ColorButton_Click ( object sender , RoutedEventArgs e )
302
+ {
303
+ SetupCurrentDisplay ( DisplayFrameType . Color ) ;
304
+ }
305
+
220
306
}
221
307
}
0 commit comments