1818
1919namespace Kinect2Sample
2020{
21+ public enum DisplayFrameType
22+ {
23+ Infrared ,
24+ Color
25+ }
2126
2227 public sealed partial class MainPage : Page , INotifyPropertyChanged
2328 {
29+ private const DisplayFrameType DEFAULT_DISPLAYFRAMETYPE = DisplayFrameType . Infrared ;
30+
2431 /// <summary>
2532 /// The highest value that can be returned in the InfraredFrame.
2633 /// It is cast to a float for readability in the visualization code.
@@ -66,9 +73,10 @@ public sealed partial class MainPage : Page, INotifyPropertyChanged
6673 private string statusText = null ;
6774 private WriteableBitmap bitmap = null ;
6875 private FrameDescription currentFrameDescription ;
76+ private DisplayFrameType currentDisplayFrameType ;
77+ private MultiSourceFrameReader multiSourceFrameReader = null ;
6978
7079 //Infrared Frame
71- private InfraredFrameReader infraredFrameReader = null ;
7280 private ushort [ ] infraredFrameData = null ;
7381 private byte [ ] infraredPixels = null ;
7482
@@ -110,23 +118,11 @@ public MainPage()
110118 // one sensor is currently supported
111119 this . kinectSensor = KinectSensor . GetDefault ( ) ;
112120
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 ) ;
118122
119- // wire handler for frame arrival
120- this . infraredFrameReader . FrameArrived += this . Reader_InfraredFrameArrived ;
123+ this . multiSourceFrameReader = this . kinectSensor . OpenMultiSourceFrameReader ( FrameSourceTypes . Infrared | FrameSourceTypes . Color ) ;
121124
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 ;
130126
131127 // set IsAvailableChanged event notifier
132128 this . kinectSensor . IsAvailableChanged += this . Sensor_IsAvailableChanged ;
@@ -140,35 +136,115 @@ public MainPage()
140136 this . InitializeComponent ( ) ;
141137 }
142138
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+
143165 private void Sensor_IsAvailableChanged ( KinectSensor sender , IsAvailableChangedEventArgs args )
144166 {
145167 this . StatusText = this . kinectSensor . IsAvailable ? "Running" : "Not Available" ;
146168 }
147169
148- private void Reader_InfraredFrameArrived ( object sender ,
149- InfraredFrameArrivedEventArgs e )
170+ private void Reader_MultiSourceFrameArrived ( MultiSourceFrameReader sender , MultiSourceFrameArrivedEventArgs e )
150171 {
151- bool infraredFrameProcessed = false ;
172+ MultiSourceFrame multiSourceFrame = e . FrameReference . AcquireFrame ( ) ;
152173
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 )
155176 {
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 ( ) )
166184 {
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 ;
169202
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 ) ;
171217 }
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 ;
172248 }
173249 }
174250
@@ -196,7 +272,7 @@ private void ConvertInfraredDataToPixels()
196272
197273 // 3. limiting the value to InfraredOutputValueMaximum
198274 intensityRatio = Math . Min ( InfraredOutputValueMaximum , intensityRatio ) ;
199-
275+
200276 // 4. limiting the lower value InfraredOutputValueMinimum
201277 intensityRatio = Math . Max ( InfraredOutputValueMinimum , intensityRatio ) ;
202278
@@ -217,5 +293,15 @@ private void RenderPixelArray(byte[] pixels)
217293 FrameDisplayImage . Source = this . bitmap ;
218294 }
219295
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+
220306 }
221307}
0 commit comments