//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//------------------------------------------------------------------------------
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Resources;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using WindowsPreview.Kinect;
namespace Microsoft.Samples.Kinect.ColorBasics
{
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
///
/// Resource loader for string resources
///
private ResourceLoader resourceLoader = new ResourceLoader("Resources");
///
/// Size of the RGB pixel in the bitmap
///
private readonly uint bytesPerPixel;
///
/// Active Kinect sensor
///
private KinectSensor kinectSensor = null;
///
/// Reader for color frames
///
private ColorFrameReader colorFrameReader = null;
///
/// Bitmap to display
///
private WriteableBitmap bitmap = null;
///
/// Intermediate storage for receiving frame data from the sensor
///
private byte[] colorPixels = null;
///
/// Current status text to display
///
private string statusText = null;
///
/// Initializes a new instance of the MainPage class.
///
public MainPage()
{
// get the kinectSensor object
this.kinectSensor = KinectSensor.GetDefault();
// open the reader for the color frames
this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader();
// wire handler for frame arrival
this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived;
// create the colorFrameDescription from the ColorFrameSource using rgba format
FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Rgba);
// rgba is 4 bytes per pixel
this.bytesPerPixel = colorFrameDescription.BytesPerPixel;
// allocate space to put the pixels to be rendered
this.colorPixels = new byte[colorFrameDescription.Width * colorFrameDescription.Height * this.bytesPerPixel];
// create the bitmap to display
this.bitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height);
// set IsAvailableChanged event notifier
this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged;
// open the sensor
this.kinectSensor.Open();
// set the status text
this.StatusText = this.kinectSensor.IsAvailable ? resourceLoader.GetString("RunningStatusText")
: resourceLoader.GetString("NoSensorStatusText");
// use the window object as the view model in this simple example
this.DataContext = this;
// initialize the components (controls) of the window
this.InitializeComponent();
theImage.Source = this.bitmap;
}
///
/// INotifyPropertyChangedPropertyChanged event to allow window controls to bind to changeable data.
///
public event PropertyChangedEventHandler PropertyChanged;
///
/// Gets or sets the current status text to display
///
public string StatusText
{
get
{
return this.statusText;
}
set
{
if (this.statusText != value)
{
this.statusText = value;
// notify any bound elements that the text has changed
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("StatusText"));
}
}
}
}
///
/// Execute shutdown tasks.
///
/// object sending the event
/// event arguments
private void MainPage_Unloaded(object sender, RoutedEventArgs e)
{
if (this.colorFrameReader != null)
{
// ColorFrameReder is IDisposable
this.colorFrameReader.Dispose();
this.colorFrameReader = null;
}
if (this.kinectSensor != null)
{
this.kinectSensor.Close();
this.kinectSensor = null;
}
}
///
/// Handles the color frame data arriving from the sensor.
///
/// object sending the event
/// event arguments
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
bool colorFrameProcessed = false;
// ColorFrame is IDisposable
using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
{
if (colorFrame != null)
{
FrameDescription colorFrameDescription = colorFrame.FrameDescription;
// verify data and write the new color frame data to the Writeable bitmap
if ((colorFrameDescription.Width == this.bitmap.PixelWidth) && (colorFrameDescription.Height == this.bitmap.PixelHeight))
{
if (colorFrame.RawColorImageFormat == ColorImageFormat.Bgra)
{
colorFrame.CopyRawFrameDataToBuffer(this.bitmap.PixelBuffer);
}
else
{
colorFrame.CopyConvertedFrameDataToBuffer(this.bitmap.PixelBuffer, ColorImageFormat.Bgra);
}
colorFrameProcessed = true;
}
}
}
// we got a frame, render
if (colorFrameProcessed)
{
this.bitmap.Invalidate();
}
}
///
/// Handles the event which the sensor becomes unavailable (E.g. paused, closed, unplugged).
///
/// object sending the event
/// event arguments
private void Sensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs e)
{
// on failure, set the status text
this.StatusText = this.kinectSensor.IsAvailable ? resourceLoader.GetString("RunningStatusText")
: resourceLoader.GetString("SensorNotAvailableStatusText");
}
}
}