Beispiel #1
0
        private async Task InitializeMediaCaptureAsync()
        {
            // Do not use ConfigureAwait(false), subsequent calls must come from Dispatcher thread
            var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

            var device = _panel.HasValue
                ? devices.FirstOrDefault(d => d.EnclosureLocation?.Panel == _panel)
                : devices.FirstOrDefault();

            // TODO: remove this hack, it defaults the camera to any camera if it cannot find one for a specific panel
            device = device ?? devices.FirstOrDefault();

            if (device == null)
            {
                throw new InvalidOperationException("Could not find camera device.");
            }

            _rotationHelper = new CameraRotationHelper(device.EnclosureLocation);
            _rotationHelper.OrientationChanged += OnOrientationChanged;

            // Initialize for panel
            var settings = new MediaCaptureInitializationSettings
            {
                VideoDeviceId = device.Id,
            };

            // Do not use ConfigureAwait(false), subsequent calls must come from Dispatcher thread
            await MediaCapture.InitializeAsync(settings);

            // Set flash modes
            MediaCapture.SetFlashMode(FlashMode);
            MediaCapture.SetTorchMode(TorchMode);

            // Set to capture element
            CaptureElement.Source = MediaCapture;
            // Mirror for front facing camera
            CaptureElement.FlowDirection = _panel == Windows.Devices.Enumeration.Panel.Front
                ? FlowDirection.RightToLeft
                : FlowDirection.LeftToRight;

            // Start preview
            // Do not `ConfigureAwait(false), orientation must be set on Dispatcher thread
            await MediaCapture.StartPreviewAsync();

            // Set preview rotation
            await UpdatePreviewOrientationAsync().ConfigureAwait(false);

            // Start barcode scanning
            if (BarcodeScanningEnabled)
            {
                _barcodeScanningSubscription.Disposable =
                    GetBarcodeScanningObservable().Subscribe(result =>
                {
                    BarcodeScanned?.Invoke(result);
                });
            }

            IsInitialized = true;

            lock (_initializationGate)
            {
                _initializationTask = null;
            }
        }