Ejemplo n.º 1
0
 private void ButtonClick(object sender, EventArgs e)
 {
     handTracker       = HandTracker.Create();
     btn_start.Enabled = false;
     HandleError(handTracker.StartGestureDetection(GestureData.GestureType.HandRaise));
     handTracker.OnNewData += HandTrackerOnNewData;
 }
Ejemplo n.º 2
0
        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);
        }