// gets the current frame and is triggered every new frame private void currentFrame(VideoStream vStream) { if (vStream.IsValid && vStream.IsFrameAvailable()) { using (OpenNIWrapper.VideoFrameRef frame = vStream.ReadFrame()) { if (frame.IsValid) { VideoFrameRef.CopyBitmapOptions options = VideoFrameRef.CopyBitmapOptions.None | VideoFrameRef.CopyBitmapOptions.Force24BitRgb; lock (this.m_Bitmap) { try { frame.UpdateBitmap(m_Bitmap, options); Image <Bgra, Byte> colorFrame = new Image <Bgra, Byte>(m_Bitmap); Image <Gray, Int32> xFrame = new Image <Gray, float>(m_Bitmap).Convert <Gray, Int32>(); CameraManager.Instance.SetImages(colorFrame, colorFrame, xFrame, xFrame); } catch (Exception exception) { Console.WriteLine("Exception occurred while updating frame."); Console.WriteLine(exception.Message); } } } } } }
private void OpenNI2_OnNewFrame(VideoStream stream) { if (stream != null && stream.IsValid && stream.IsFrameAvailable()) { using (VideoFrameRef frame = stream.ReadFrame()) { if (frame.IsValid) { VideoFrameRef.CopyBitmapOptions options = VideoFrameRef.CopyBitmapOptions.Force24BitRgb | VideoFrameRef.CopyBitmapOptions.DepthFillShadow; try { frame.UpdateBitmap(this.mBitmap, options); } catch (Exception) { // Happens when Bitmap object is not compatible with returned Frame this.mBitmap = frame.ToBitmap(options); } using (MemoryStream memory = new MemoryStream()) { this.mBitmap.Save(memory, ImageFormat.Bmp); memory.Position = 0; this.mImage = new BitmapImage(); this.mImage.BeginInit(); this.mImage.StreamSource = memory; this.mImage.CacheOption = BitmapCacheOption.OnLoad; this.mImage.EndInit(); this.mImage.Freeze(); } this.Dispatcher.Invoke((Action)(() => { this.ImgDepth.Source = this.mImage; })); } } } }
private void CurrentSensorOnNewFrame(VideoStream videoStream) { if (videoStream.IsValid && videoStream.IsFrameAvailable()) { using (VideoFrameRef frame = videoStream.ReadFrame()) { if (frame.IsValid) { VideoFrameRef.CopyBitmapOptions options = VideoFrameRef.CopyBitmapOptions.Force24BitRgb | VideoFrameRef.CopyBitmapOptions.DepthFillShadow; if (this.cb_invert.Checked) { options |= VideoFrameRef.CopyBitmapOptions.DepthInvert; } if (this.cb_equal.Checked) { options |= VideoFrameRef.CopyBitmapOptions.DepthHistogramEqualize; } if (this.cb_fill.Checked) { options |= videoStream.Mirroring ? VideoFrameRef.CopyBitmapOptions.DepthFillRigthBlack : VideoFrameRef.CopyBitmapOptions.DepthFillLeftBlack; } lock (this.bitmap) { /////////////////////// Instead of creating a bitmap object for each frame, you can simply /////////////////////// update one you have. Please note that you must be very careful /////////////////////// with multi-thread situations. try { frame.UpdateBitmap(this.bitmap, options); } catch (Exception) { // Happens when our Bitmap object is not compatible with returned Frame this.bitmap = frame.ToBitmap(options); } /////////////////////// END NOTE /////////////////////// You can always use .toBitmap() if you dont want to /////////////////////// clone image later and be safe when using it in multi-thread situations /////////////////////// This is little slower, but easier to handle // bitmap = frame.toBitmap(options); /////////////////////// END NOTE if (this.cb_mirrorSoft.Checked) { this.bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); } } ///////////////////// You can simply pass the newly created/updated image to a ///////////////////// PictureBox right here instead of drawing it with Graphic object // this.BeginInvoke(new MethodInvoker(delegate() // { // if (!pb_image.Visible) // pb_image.Visible = true; // if (bitmap == null) // return; // lock (bitmap) // this.BeginInvoke happens on UI Thread so it is better to always keep this lock in place // { // if (pb_image.Image != null) // pb_image.Image.Dispose(); // /////////////////////// If you want to use one bitmap object for all frames, the // /////////////////////// best way to prevent and multi-thread access problems // /////////////////////// is to clone the bitmap each time you want to send it to PictureBox // pb_image.Image = new Bitmap(bitmap, bitmap.Size); // /////////////////////// END NOTE // /////////////////////// If you only use toBitmap() method. you can simply skip the // /////////////////////// cloning process. It is perfectly thread-safe. // //pb_image.Image = bitmap; // /////////////////////// END NOTE // pb_image.Refresh(); // } // })); ///////////////////// END NOTE if (!this.pb_image.Visible) { this.Invalidate(); } } } } }
private bool Start() { RegisterFilter(); if (this.isIdle && this.broadcaster.HasServer()) { MessageBox.Show( @"Only one server is allowed.", @"Multi-Server", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } bool isSameDevice = this.currentDevice != null && this.currentDevice.IsValid && this.currentDevice.DeviceInfo.Uri == Settings.Default.DeviceURI; bool isSameSensor = isSameDevice && this.currentSensor != null && this.currentSensor.IsValid && this.currentSensor.SensorInfo.GetSensorType() == (Device.SensorType)Settings.Default.CameraType; if (!isSameDevice) { if (Settings.Default.DeviceURI == string.Empty) { this.currentDevice = null; MessageBox.Show( @"Please select a device to open and then click Apply.", @"Device Open", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } } if (!isSameSensor) { if (Settings.Default.CameraType == -1) { this.currentDevice = null; MessageBox.Show( @"Please select a sensor to open and then click Apply.", @"Sensor Create", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } } if (!isSameDevice) { try { this.currentDevice = Device.Open(Settings.Default.DeviceURI); } catch (Exception ex) { this.currentDevice = null; MessageBox.Show( string.Format("Can not open selected Device. {0}", ex.Message), @"Device Open", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } if (!isSameSensor) { try { this.currentSensor = this.currentDevice.CreateVideoStream((Device.SensorType)Settings.Default.CameraType); this.currentSensor.OnNewFrame += this.CurrentSensorOnNewFrame; } catch (Exception ex) { this.currentSensor = null; MessageBox.Show( string.Format("Can not open selected Sensor from selected Device. {0}", ex.Message), @"Sensor Create", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } else { this.currentSensor.Stop(); } VideoMode[] vmodes = this.currentSensor.SensorInfo.GetSupportedVideoModes().ToArray(); VideoMode selectedVideoMode = null; switch (this.currentSensor.SensorInfo.GetSensorType()) { case Device.SensorType.Color: this.renderOptions = VideoFrameRef.CopyBitmapOptions.Force24BitRgb; if (Settings.Default.Color_HD) { foreach (VideoMode vm in vmodes) { if (vm.Resolution.Width == 1280 && (vm.Resolution.Height == 960 || vm.Resolution.Height == 1024)) { if ((selectedVideoMode == null || (selectedVideoMode.Fps < vm.Fps && vm.DataPixelFormat < selectedVideoMode.DataPixelFormat)) && vm.DataPixelFormat != VideoMode.PixelFormat.Jpeg && vm.DataPixelFormat != VideoMode.PixelFormat.Yuv422) { selectedVideoMode = vm; } } } this.isHd = selectedVideoMode != null; if (!this.isHd) { MessageBox.Show( @"This device doesn't support ~1.3MP resolution.", @"HD Resolution", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } if (selectedVideoMode == null) { foreach (VideoMode vm in vmodes) { if (vm.Resolution == new Size(640, 480)) { if ((selectedVideoMode == null || (selectedVideoMode.Fps < vm.Fps && vm.DataPixelFormat < selectedVideoMode.DataPixelFormat)) && vm.DataPixelFormat != VideoMode.PixelFormat.Jpeg && vm.DataPixelFormat != VideoMode.PixelFormat.Yuv422) { selectedVideoMode = vm; } } } } break; case Device.SensorType.Depth: this.renderOptions = VideoFrameRef.CopyBitmapOptions.Force24BitRgb | VideoFrameRef.CopyBitmapOptions.DepthFillShadow; if (Settings.Default.Depth_Fill) { if (this.cb_mirror.Enabled && this.cb_mirror.Checked) { this.renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthFillRigthBlack; } else { this.renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthFillLeftBlack; } } if (Settings.Default.Depth_Invert) { this.renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthInvert; } if (Settings.Default.Depth_Histogram) { this.renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthHistogramEqualize; } foreach (VideoMode vm in vmodes) { if (vm.Resolution == new Size(640, 480)) { if ((selectedVideoMode == null || selectedVideoMode.Fps < vm.Fps) && (vm.DataPixelFormat == VideoMode.PixelFormat.Depth1Mm || vm.DataPixelFormat == VideoMode.PixelFormat.Depth100Um)) { selectedVideoMode = vm; } } } break; case Device.SensorType.Ir: this.renderOptions = VideoFrameRef.CopyBitmapOptions.Force24BitRgb; foreach (VideoMode vm in vmodes) { if (vm.Resolution == new Size(640, 480)) { if ((selectedVideoMode == null || (selectedVideoMode.Fps < vm.Fps && vm.DataPixelFormat < selectedVideoMode.DataPixelFormat)) && vm.DataPixelFormat != VideoMode.PixelFormat.Jpeg && vm.DataPixelFormat != VideoMode.PixelFormat.Yuv422) { selectedVideoMode = vm; } } } break; } if (selectedVideoMode != null) { try { if (this.currentSensor.VideoMode.Fps != selectedVideoMode.Fps || this.currentSensor.VideoMode.DataPixelFormat != selectedVideoMode.DataPixelFormat || this.currentSensor.VideoMode.Resolution != selectedVideoMode.Resolution) { this.currentSensor.VideoMode = selectedVideoMode; } } catch (Exception ex) { MessageBox.Show( string.Format("Can not set active video mode to {0}. {1}", selectedVideoMode, ex.Message), @"Sensor Config", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } else { MessageBox.Show( @"No acceptable video mode found.", @"Sensor Config", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } this.softMirror = Settings.Default.Mirroring; if (Settings.Default.SmartCam) { try { if (!isSameDevice || (this.uTracker == null || this.hTracker == null || !this.uTracker.IsValid || !this.hTracker.IsValid)) { this.uTracker = UserTracker.Create(this.currentDevice); this.hTracker = HandTracker.Create(this.currentDevice); this.hTracker.StartGestureDetection(GestureData.GestureType.HandRaise); this.hTracker.OnNewData += this.NiTeOnNewData; } } catch (Exception) { } } if (!HandleError(this.currentSensor.Start())) { this.Stop(false); return false; } this.btn_stopstart.Text = @"Stop Streaming"; this.isIdle = false; this.notify.Visible = true; return true; }
private bool Start() { RegisterFilter(); if (_isIdle && _broadcaster.HasServer()) { MessageBox.Show( @"Only one server is allowed.", @"Multi-Server", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } var isSameDevice = _currentDevice != null && _currentDevice.IsValid && _currentDevice.DeviceInfo.Uri == Settings.Default.DeviceURI; var isSameSensor = isSameDevice && _currentSensor != null && _currentSensor.IsValid && _currentSensor.SensorInfo.GetSensorType() == (Device.SensorType)Settings.Default.CameraType; if (!isSameDevice) { if (Settings.Default.DeviceURI == string.Empty) { _currentDevice = null; MessageBox.Show( @"Please select a device to open and then click Apply.", @"Device Open", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } } if (!isSameSensor) { if (Settings.Default.CameraType == -1) { _currentDevice = null; MessageBox.Show( @"Please select a sensor to open and then click Apply.", @"Sensor Create", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } } if (!isSameDevice) { try { _currentDevice = Device.Open(Settings.Default.DeviceURI); } catch (Exception ex) { _currentDevice = null; MessageBox.Show( string.Format("Can not open selected Device. {0}", ex.Message), @"Device Open", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } if (!isSameSensor) { try { _currentSensor = _currentDevice.CreateVideoStream((Device.SensorType)Settings.Default.CameraType); _currentSensor.OnNewFrame += CurrentSensorOnNewFrame; } catch (Exception ex) { _currentSensor = null; MessageBox.Show( string.Format("Can not open selected Sensor from selected Device. {0}", ex.Message), @"Sensor Create", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } else { _currentSensor.Stop(); } var videoModes = _currentSensor.SensorInfo.GetSupportedVideoModes().ToArray(); VideoMode selectedVideoMode = null; switch (_currentSensor.SensorInfo.GetSensorType()) { case Device.SensorType.Color: _renderOptions = VideoFrameRef.CopyBitmapOptions.Force24BitRgb; if (Settings.Default.Color_HD) { foreach (var vm in videoModes) { if (vm.Resolution.Width == 1280 && (vm.Resolution.Height == 960 || vm.Resolution.Height == 1024)) { if ((selectedVideoMode == null || selectedVideoMode.Fps < vm.Fps && vm.DataPixelFormat < selectedVideoMode.DataPixelFormat) && vm.DataPixelFormat != VideoMode.PixelFormat.Jpeg && vm.DataPixelFormat != VideoMode.PixelFormat.Yuv422) { selectedVideoMode = vm; } } } _isHd = selectedVideoMode != null; if (!_isHd) { MessageBox.Show( @"This device doesn't support ~1.3MP resolution.", @"HD Resolution", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } if (selectedVideoMode == null) { foreach (var vm in videoModes) { if (vm.Resolution.Width == 640 && vm.Resolution.Height == 480) { if ((selectedVideoMode == null || selectedVideoMode.Fps < vm.Fps && vm.DataPixelFormat < selectedVideoMode.DataPixelFormat) && vm.DataPixelFormat != VideoMode.PixelFormat.Jpeg && vm.DataPixelFormat != VideoMode.PixelFormat.Yuv422) { selectedVideoMode = vm; } } } } break; case Device.SensorType.Depth: _renderOptions = VideoFrameRef.CopyBitmapOptions.Force24BitRgb | VideoFrameRef.CopyBitmapOptions.DepthFillShadow; if (Settings.Default.Depth_Fill) { if (cb_mirror.Enabled && cb_mirror.Checked) { _renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthFillRigthBlack; } else { _renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthFillLeftBlack; } } if (Settings.Default.Depth_Invert) { _renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthInvert; } if (Settings.Default.Depth_Histogram) { _renderOptions |= VideoFrameRef.CopyBitmapOptions.DepthHistogramEqualize; } foreach (var vm in videoModes) { if (vm.Resolution.Width == 640 && vm.Resolution.Height == 480) { if ((selectedVideoMode == null || selectedVideoMode.Fps < vm.Fps) && (vm.DataPixelFormat == VideoMode.PixelFormat.Depth1Mm || vm.DataPixelFormat == VideoMode.PixelFormat.Depth100Um)) { selectedVideoMode = vm; } } } break; case Device.SensorType.Ir: _renderOptions = VideoFrameRef.CopyBitmapOptions.Force24BitRgb; foreach (var vm in videoModes) { if (vm.Resolution.Width == 640 && vm.Resolution.Height == 480) { if ((selectedVideoMode == null || selectedVideoMode.Fps < vm.Fps && vm.DataPixelFormat < selectedVideoMode.DataPixelFormat) && vm.DataPixelFormat != VideoMode.PixelFormat.Jpeg && vm.DataPixelFormat != VideoMode.PixelFormat.Yuv422) { selectedVideoMode = vm; } } } break; } if (selectedVideoMode != null) { try { if (_currentSensor.VideoMode.Fps != selectedVideoMode.Fps || _currentSensor.VideoMode.DataPixelFormat != selectedVideoMode.DataPixelFormat || _currentSensor.VideoMode.Resolution.Width != selectedVideoMode.Resolution.Width || _currentSensor.VideoMode.Resolution.Height != selectedVideoMode.Resolution.Height) { _currentSensor.VideoMode = selectedVideoMode; } } catch (Exception ex) { MessageBox.Show( string.Format("Can not set active video mode to {0}. {1}", selectedVideoMode, ex.Message), @"Sensor Config", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } else { MessageBox.Show( @"No acceptable video mode found.", @"Sensor Config", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } _softMirror = Settings.Default.Mirroring; if (Settings.Default.SmartCam) { try { if (!isSameDevice || _userTracker == null || _handTracker == null || !_userTracker.IsValid || !_handTracker.IsValid) { _userTracker = UserTracker.Create(_currentDevice); _handTracker = HandTracker.Create(_currentDevice); _handTracker.StartGestureDetection(GestureData.GestureType.HandRaise); _handTracker.OnNewData += NiTeOnNewData; } } catch { // ignored } } if (!HandleError(_currentSensor.Start())) { Stop(false); return(false); } btn_stopstart.Text = @"Stop Streaming"; _isIdle = false; notify.Visible = true; return(true); }