/// <summary> /// Lets ISensorStreamHandler know that Kinect Sensor associated with this stream /// handler has changed. /// </summary> /// <param name="newSensor"> /// New KinectSensor. /// </param> public override void OnSensorChanged(KinectSensor newSensor) { if (this.sensor != null) { try { this.backgroundRemovalStream.BackgroundRemovedFrameReady -= this.BackgroundRemovedFrameReadyAsync; this.backgroundRemovalStream.Dispose(); this.backgroundRemovalStream = null; this.sensor.ColorStream.Disable(); } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } } this.sensor = newSensor; if (newSensor != null) { this.backgroundRemovalStream = new BackgroundRemovedColorStream(newSensor); this.backgroundRemovalStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyAsync; // Force enabling the background removal stream because it hasn't been enabled before. this.UpdateBackgroundRemovalFrameFormat(this.colorImageFormat, true); } }
protected override void OnRuntimeConnected() { this.runtime.AllFrameReady += runtime_AllFrameReady; this.backgroundstream = new BackgroundRemovedColorStream(this.runtime.Runtime); this.backgroundstream.Enable(ColorImageFormat.RgbResolution640x480Fps30, DepthImageFormat.Resolution640x480Fps30); this.backgroundstream.BackgroundRemovedFrameReady += backgroundstream_BackgroundRemovedFrameReady; }
/// <summary> /// Notification of changes to available sensors. /// </summary> /// <param name="oldSensor">Previous sensor.</param> /// <param name="newSensor">New sensor.</param> public void OnKinectSensorChanged(KinectSensor oldSensor, KinectSensor newSensor) { if (null != oldSensor) { // Remove sensor frame event handler. oldSensor.AllFramesReady -= this.SensorAllFramesReady; // Tear down the BackgroundRemovedColorStream for this user. this.backgroundRemovedColorStream.BackgroundRemovedFrameReady -= this.BackgroundRemovedFrameReadyHandler; this.backgroundRemovedColorStream.Dispose(); this.backgroundRemovedColorStream = null; this.TrackingId = InvalidTrackingId; } this.sensor = newSensor; if (null != newSensor) { // Setup a new BackgroundRemovedColorStream for this user. this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(newSensor); this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler; this.backgroundRemovedColorStream.Enable( newSensor.ColorStream.Format, newSensor.DepthStream.Format); // Add an event handler to be called when there is new frame data from the sensor. newSensor.AllFramesReady += this.SensorAllFramesReady; } }
protected virtual void Dispose(bool disposing) { if (!this.disposed && false) { if (null != this.backgroundRemovedColorStream) { this.backgroundRemovedColorStream.Dispose(); this.backgroundRemovedColorStream = null; } this.disposed = true; } }
protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (null != this.backgroundRemovedColorStream) { this.backgroundRemovedColorStream.Dispose(); this.backgroundRemovedColorStream = null; } this.disposed = true; } }
public MainWindow() { InitializeComponent(); // initialize the sensor chooser and UI this.sensorChooser = KinectSensor.KinectSensors[0]; //this.sensorChooserUi.KinectSensorChooser = this.sensorChooser; //this.sensorChooser.KinectChanged += this.SensorChooserOnKinectChanged; //sensorChooser.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(SensorAllFramesReady); { sensorChooser.DepthStream.Enable(DepthFormat); sensorChooser.ColorStream.Enable(ColorFormat); sensorChooser.SkeletonStream.Enable(); this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(sensorChooser); this.backgroundRemovedColorStream.Enable(ColorFormat, DepthFormat); // Allocate space to put the depth, color, and skeleton data we'll receive if (null == this.skeletons) { this.skeletons = new Skeleton[sensorChooser.SkeletonStream.FrameSkeletonArrayLength]; } // Add an event handler to be called when the background removed color frame is ready, so that we can // composite the image and output to the app this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler; // Add an event handler to be called whenever there is new depth frame data sensorChooser.AllFramesReady += this.SensorAllFramesReady; try { sensorChooser.DepthStream.Range = DepthRange.Default; sensorChooser.SkeletonStream.EnableTrackingInNearRange = true; } catch (InvalidOperationException) { // Non Kinect for Windows devices do not support Near mode, so reset back to default mode. sensorChooser.DepthStream.Range = DepthRange.Default; sensorChooser.SkeletonStream.EnableTrackingInNearRange = false; } //this.statusBarText.Text = Properties.Resources.ReadyForScreenshot; } this.sensorChooser.Start(); }
public MainWindow() { InitializeComponent(); V3D.Camera.Position = new Point3D(6, -10, 1.5); V3D.Camera.LookDirection = new Vector3D(0, 1, 0); // initialize the sensor chooser and UI this.sensorChooser = KinectSensor.KinectSensors[0]; //this.sensorChooser.KinectChanged += this.SensorChooserOnKinectChanged; //--- Add #region // Sensor 啟動 sensorChooser.DepthStream.Enable(DepthFormat); sensorChooser.ColorStream.Enable(ColorFormat); sensorChooser.SkeletonStream.Enable(); this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(sensorChooser); this.backgroundRemovedColorStream.Enable(ColorFormat, DepthFormat); // Allocate space to put the depth, color, and skeleton data we'll receive if (null == this.skeletons) { this.skeletons = new Skeleton[sensorChooser.SkeletonStream.FrameSkeletonArrayLength]; } // Add an event handler to be called when the background removed color frame is ready, so that we can // composite the image and output to the app this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler; // Add an event handler to be called whenever there is new depth frame data sensorChooser.AllFramesReady += this.SensorAllFramesReady; try { sensorChooser.DepthStream.Range = DepthRange.Default; sensorChooser.SkeletonStream.EnableTrackingInNearRange = true; } catch (InvalidOperationException) { // Non Kinect for Windows devices do not support Near mode, so reset back to default mode. sensorChooser.DepthStream.Range = DepthRange.Default; sensorChooser.SkeletonStream.EnableTrackingInNearRange = false; } #endregion this.sensorChooser.Start(); }
/// <summary> /// Starts the background removal process /// </summary> private void InitiateBackgroundRemoval(KinectSensor newSensor) { //Create BackgroundRemovalLib Obj with active sensor this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(newSensor); backgroundObj = new BackgroundRemovalLib(this.sensorChooser, this.backgroundRemovedColorStream); backgroundObj.BackgroundStart(); //this.backgroundRemovedColorStream = backgroundObj.getBackgroundRemovedColorStream(); //Tie image source to output of object //this.Image.Source = backgroundObj.getBackgroundRemovedImage(); backgroundObj.setImageSource(this.Image.Source); // Add an event handler to be called when the background removed color frame is ready, so that we can // composite the image and output to the app this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += backgroundObj.BackgroundRemovedFrameReadyHandler; // Add an event handler to be called whenever there is new depth frame data newSensor.AllFramesReady += backgroundObj.SensorAllFramesReady; }
public void BackgroundStart() { this.sensor.DepthStream.Enable(DepthFormat); this.sensor.ColorStream.Enable(ColorFormat); this.sensor.SkeletonStream.Enable(); this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(this.sensor); this.backgroundRemovedColorStream.Enable(ColorFormat, DepthFormat); // Allocate space to put the depth, color, and skeleton data we'll receive if (null == this.skeletons) { this.skeletons = new Skeleton[this.sensor.SkeletonStream.FrameSkeletonArrayLength]; } // Add an event handler to be called when the background removed color frame is ready, so that we can // composite the image and output to the app this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler; // Add an event handler to be called whenever there is new depth frame data this.sensor.AllFramesReady += this.SensorAllFramesReady; }
public void BackgroundStop(KinectSensor OldSensor) { try { OldSensor.AllFramesReady -= this.SensorAllFramesReady; OldSensor.DepthStream.Disable(); OldSensor.ColorStream.Disable(); OldSensor.SkeletonStream.Disable(); // Create the background removal stream to process the data and remove background, and initialize it. if (null != this.backgroundRemovedColorStream) { this.backgroundRemovedColorStream.BackgroundRemovedFrameReady -= this.BackgroundRemovedFrameReadyHandler; this.backgroundRemovedColorStream.Dispose(); this.backgroundRemovedColorStream = null; } } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } }
protected override void OnRuntimeConnected() { this.runtime.AllFrameReady += runtime_AllFrameReady; this.backgroundstream = new BackgroundRemovedColorStream(this.runtime.Runtime); this.backgroundstream.BackgroundRemovedFrameReady += backgroundstream_BackgroundRemovedFrameReady; }
/// <summary> /// Enables the chosen sensor /// </summary> private void EnableSensor2(KinectSensor NewSensor) { if (NewSensor != null) { try { NewSensor.DepthStream.Enable(DepthFormat); NewSensor.ColorStream.Enable(ColorFormat); NewSensor.SkeletonStream.Enable(); this.backgroundRemovedColorStream2 = new BackgroundRemovedColorStream(NewSensor); this.backgroundRemovedColorStream2.Enable(ColorFormat, DepthFormat); // Allocate space to put the depth, color, and skeleton data we'll receive if (null == this.skeletons2) { this.skeletons2 = new Skeleton[NewSensor.SkeletonStream.FrameSkeletonArrayLength]; } // Add an event handler to be called when the background removed color frame is ready, so that we can // composite the image and output to the app this.backgroundRemovedColorStream2.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler2; // Add an event handler to be called whenever there is new depth frame data NewSensor.AllFramesReady += this.SensorAllFramesReady2; } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } } }
private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args) { bool error = false; if (args.OldSensor != null) { try { args.OldSensor.AllFramesReady -= this.SensorAllFramesReady; args.OldSensor.DepthStream.Disable(); args.OldSensor.ColorStream.Disable(); args.OldSensor.SkeletonStream.Disable(); // Create the background removal stream to process the data and remove background, and initialize it. if (null != this.backgroundRemovedColorStream) { this.backgroundRemovedColorStream.BackgroundRemovedFrameReady -= this.BackgroundRemovedFrameReadyHandler; this.backgroundRemovedColorStream.Dispose(); this.backgroundRemovedColorStream = null; } } catch (InvalidOperationException) { error = true; } } if (args.NewSensor != null) { try { args.NewSensor.DepthStream.Enable(DepthFormat); args.NewSensor.ColorStream.Enable(ColorFormat); args.NewSensor.SkeletonStream.Enable(); this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(args.NewSensor); this.backgroundRemovedColorStream.Enable(ColorFormat, DepthFormat); // Allocate space to put the depth, color, and skeleton data we'll receive if (null == this.skeletons) { this.skeletons = new Skeleton[args.NewSensor.SkeletonStream.FrameSkeletonArrayLength]; } // Add an event handler to be called when the background removed color frame is ready, so that we can // composite the image and output to the app this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler; // Add an event handler to be called whenever there is new depth frame data args.NewSensor.AllFramesReady += this.SensorAllFramesReady; try { args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true; } catch (InvalidOperationException) { // Non Kinect for Windows devices do not support Near mode, so reset back to default mode. args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false; } try { kinectRegion.KinectSensor = args.NewSensor; } catch (InvalidOperationException) { } } catch (InvalidOperationException) { error = true; } } }
/// <summary> /// Called when the KinectSensorChooser gets a new sensor /// </summary> /// <param name="sender">sender of the event</param> /// <param name="args">event arguments</param> private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args) { if (args.OldSensor != null) { try { args.OldSensor.AllFramesReady -= this.SensorAllFramesReady; args.OldSensor.DepthStream.Disable(); args.OldSensor.ColorStream.Disable(); args.OldSensor.SkeletonStream.Disable(); // Create the background removal stream to process the data and remove background, and initialize it. if (null != this.backgroundRemovedColorStream) { this.backgroundRemovedColorStream.BackgroundRemovedFrameReady -= this.BackgroundRemovedFrameReadyHandler; this.backgroundRemovedColorStream.Dispose(); this.backgroundRemovedColorStream = null; } } catch (InvalidOperationException eInvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. Debug.WriteLine("SensorChooserOnKinectChanged:" + eInvalidOperationException.ToString()); } } if (args.NewSensor != null) { try { args.NewSensor.DepthStream.Enable(DepthFormat); args.NewSensor.ColorStream.Enable(ColorFormat); args.NewSensor.SkeletonStream.Enable(); this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(args.NewSensor); this.backgroundRemovedColorStream.Enable(ColorFormat, DepthFormat); // Allocate space to put the depth, color, and skeleton data we'll receive if (null == this.skeletons) { this.skeletons = new Skeleton[args.NewSensor.SkeletonStream.FrameSkeletonArrayLength]; } // Add an event handler to be called when the background removed color frame is ready, so that we can // *composite the image and output to the app this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler; // *Add an event handler to be called whenever there is new depth frame data args.NewSensor.AllFramesReady += this.SensorAllFramesReady; try { /* args.NewSensor.DepthStream.Range = this.checkBoxNearMode.IsChecked.GetValueOrDefault() ? DepthRange.Near : DepthRange.Default; */ args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true; } catch (InvalidOperationException) { // Non Kinect for Windows devices do not support Near mode, so reset back to default mode. args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false; } this.statusBarText.Text = Properties.Resources.ReadyForScreenshot; } //catch (InvalidOperationException e) catch (Exception e) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. Debug.WriteLine("SensorChooserOnKinectChanged:" + e.ToString()); } } }
private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args) { if (args.OldSensor != null) { try { args.OldSensor.AllFramesReady -= this.SensorAllFramesReady; args.OldSensor.DepthStream.Range = DepthRange.Default; args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false; args.OldSensor.DepthStream.Disable(); args.OldSensor.ColorStream.Disable(); args.OldSensor.SkeletonStream.Disable(); if (colorStream != null) { this.colorStream.BackgroundRemovedFrameReady -= this.BackgroundRemovedFrameReadyHandler; this.colorStream.Dispose(); this.colorStream = null; } if (interactionStream != null) { this.interactionStream.InteractionFrameReady -= this.InteractionFrameReadyHandler; this.interactionStream.Dispose(); this.interactionStream = null; } this.controller.Sensor = null; } catch (InvalidOperationException) { // the sample says something bad might happen while trying to do that stuff } } if (args.NewSensor != null) { try { TransformSmoothParameters smoothingParam = new TransformSmoothParameters(); { smoothingParam.Smoothing = 0.5f; smoothingParam.Correction = 0.5f; smoothingParam.Prediction = 0.5f; smoothingParam.JitterRadius = 0.05f; smoothingParam.MaxDeviationRadius = 0.04f; }; args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30); args.NewSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); args.NewSensor.SkeletonStream.Enable(smoothingParam); args.NewSensor.SkeletonStream.Enable(); args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false; this.skeletons = new Skeleton[args.NewSensor.SkeletonStream.FrameSkeletonArrayLength]; this.colorStream = new BackgroundRemovedColorStream(args.NewSensor); this.colorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30, DepthImageFormat.Resolution640x480Fps30); this.colorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler; this.interactionStream = new InteractionStream(args.NewSensor, new DummyInteractionClient()); this.interactionStream.InteractionFrameReady += this.InteractionFrameReadyHandler; args.NewSensor.AllFramesReady += this.SensorAllFramesReady; this.controller.Sensor = args.NewSensor; } catch (InvalidOperationException) { // I guess something might go wrong } } }
/// <summary> /// Initializes a new instance of the class. /// </summary> public BackgroundRemovalLib(KinectSensorChooser sensorChooserIn, BackgroundRemovedColorStream brColorStreamIn) { this.sensorChooser = sensorChooserIn; this.backgroundRemovedColorStream = brColorStreamIn; }
/// <summary> /// Initializes a new instance of the class. /// </summary> public BackgroundRemovalLib() { this.sensorChooser = null; this.backgroundRemovedColorStream = null; }
/// <summary> /// When a Kinect sensor is plugged or unplugged, enable/disable sensors as necessary /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args) { bool error = false; if (args.OldSensor != null) { try { //Disable all streams from the old sensor args.OldSensor.DepthStream.Range = DepthRange.Default; args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false; args.OldSensor.DepthStream.Disable(); args.OldSensor.SkeletonStream.Disable(); args.OldSensor.ColorStream.Disable(); if (null != backgroundRemovedColorStream) { backgroundRemovedColorStream.BackgroundRemovedFrameReady -= this.BackgroundRemovedFrameReadyHandler; backgroundRemovedColorStream.Dispose(); backgroundRemovedColorStream = null; } } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. error = true; } } if (args.NewSensor != null) { try { //When a new sensor is plugged in, enable all the streams and set the angle to default speechEngine = CreateSpeechRecogniser(); args.NewSensor.DepthStream.Enable(); args.NewSensor.SkeletonStream.Enable(); args.NewSensor.ColorStream.Enable(); args.NewSensor.ElevationAngle = 0; backgroundRemovedColorStream = new BackgroundRemovedColorStream(args.NewSensor); backgroundRemovedColorStream.Enable(args.NewSensor.ColorStream.Format, args.NewSensor.DepthStream.Format); consoleUI.Text = "Raise your hand and grab a sample to start!"; // Allocate space to put the depth, color, and skeleton data we'll receive if (null == skeletons) { skeletons = new Skeleton[args.NewSensor.SkeletonStream.FrameSkeletonArrayLength]; args.NewSensor.AllFramesReady += SensorAllFramesReady; backgroundRemovedColorStream.BackgroundRemovedFrameReady += BackgroundRemovedFrameReadyHandler; } //Try and enable near mode try { args.NewSensor.DepthStream.Range = DepthRange.Near; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true; args.NewSensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated; } catch (InvalidOperationException) { //Kinect for Xbox 360 devices do not support Near mode, so reset back to default mode. args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false; } } catch (InvalidOperationException) { error = true; } if (!error) //If nothing fails assign the newSensor and start listening for audio kRegion.KinectSensor = args.NewSensor; StartAudioListening(); } }
/// <summary> /// Called when the KinectSensorChooser gets a new sensor /// </summary> /// <param name="sender">sender of the event</param> /// <param name="args">event arguments</param> private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args) { if (args.OldSensor != null) { try { if (mode == "Wall") { args.OldSensor.AllFramesReady -= this.SensorAllFramesReady; args.OldSensor.DepthStream.Disable(); args.OldSensor.ColorStream.Disable(); args.OldSensor.SkeletonStream.Disable(); if (null != this.backgroundRemovedColorStream) { this.backgroundRemovedColorStream.BackgroundRemovedFrameReady -= this.BackgroundRemovedFrameReadyHandler; this.backgroundRemovedColorStream.Dispose(); this.backgroundRemovedColorStream = null; } } else if (mode == "Privacy") { Black.Visibility = System.Windows.Visibility.Visible; args.OldSensor.AllFramesReady -= this.KinectSensorOnAllFramesReady; args.OldSensor.DepthStream.Disable(); args.OldSensor.ColorStream.Disable(); args.OldSensor.SkeletonStream.Disable(); } } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } } if (args.NewSensor != null) { try { LoadImage(); ks = args.NewSensor; args.NewSensor.DepthStream.Enable(DepthFormat); args.NewSensor.ColorStream.Enable(ColorFormat); args.NewSensor.SkeletonStream.Enable(); // Add an event handler to be called whenever there is new depth frame data if (mode == "Wall") { this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(args.NewSensor); this.backgroundRemovedColorStream.Enable(ColorFormat, DepthFormat); // Allocate space to put the depth, color, and skeleton data we'll receive if (null == this.skeletons) { this.skeletons = new Skeleton[args.NewSensor.SkeletonStream.FrameSkeletonArrayLength]; } // Add an event handler to be called when the background removed color frame is ready, so that we can // composite the image and output to the app this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler; args.NewSensor.AllFramesReady += this.SensorAllFramesReady; } else if (mode == "Privacy") { Matching.Text = ""; OurTitle.Text = "Kinivacy Privacy Filter"; // Allocate space to put the depth, color, and skeleton data we'll receive if (null == this.skeletons) { this.skeletons = new Skeleton[args.NewSensor.SkeletonStream.FrameSkeletonArrayLength]; } args.NewSensor.AllFramesReady += this.KinectSensorOnAllFramesReady; } try { args.NewSensor.DepthStream.Range = this.checkBoxNearMode.IsChecked.GetValueOrDefault() ? DepthRange.Near : DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true; } catch (InvalidOperationException) { // Non Kinect for Windows devices do not support Near mode, so reset back to default mode. args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false; } this.statusBarText.Text = Properties.Resources.ReadyForScreenshot; } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } } }
private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args) { if (args.OldSensor != null) { try { args.OldSensor.AllFramesReady -= this.SensorAllFramesReady; args.OldSensor.DepthStream.Disable(); args.OldSensor.ColorStream.Disable(); args.OldSensor.SkeletonStream.Disable(); // Create the background removal stream to process the data and remove background, and initialize it. if (null != this.backgroundRemovedColorStream) { this.backgroundRemovedColorStream.BackgroundRemovedFrameReady -= this.BackgroundRemovedFrameReadyHandler; this.backgroundRemovedColorStream.Dispose(); this.backgroundRemovedColorStream = null; } } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } } if (args.NewSensor != null) { try { this.arg = args; args.NewSensor.DepthStream.Enable(DepthFormat); args.NewSensor.ColorStream.Enable(ColorFormat); args.NewSensor.SkeletonStream.Enable(); this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(args.NewSensor); this.backgroundRemovedColorStream.Enable(ColorFormat, DepthFormat); // Allocate space to put the depth pixels we'll receive this.depthPixels = new DepthImagePixel[args.NewSensor.DepthStream.FramePixelDataLength]; // Allocate space to put the depth, color, and skeleton data we'll receive if (null == this.skeletons) { this.skeletons = new Skeleton[args.NewSensor.SkeletonStream.FrameSkeletonArrayLength]; } // Add an event handler to be called when the background removed color frame is ready, so that we can // composite the image and output to the app this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler; // Add an event handler to be called whenever there is new depth frame data args.NewSensor.AllFramesReady += this.SensorAllFramesReady; try { args.NewSensor.DepthStream.Range = this.checkBoxNearMode.IsChecked.GetValueOrDefault() ? DepthRange.Near : DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true; } catch (InvalidOperationException) { // Non Kinect for Windows devices do not support Near mode, so reset back to default mode. args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false; error = true; } this.statusBarText.Text = Properties.Resources.ReadyForScreenshot; } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } } }