Ejemplo n.º 1
0
        private async Task UpdatePreviewOrientationAsync()
        {
            var rotation = _rotationHelper.GetCameraPreviewOrientation();
            var props    = MediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(rotation));
            await MediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null).AsTask().ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        private async Task CapturePhotoAsync(CameraForView cameraForView, JObject options, IPromise promise)
        {
            var mediaCapture = cameraForView.MediaCapture;
            var encoding     = ImageEncodingProperties.CreateJpeg();
            var target       = options.Value <int>("target");

            using (var stream = new InMemoryRandomAccessStream())
            {
                await mediaCapture.CapturePhotoToStreamAsync(encoding, stream).AsTask().ConfigureAwait(false);

                if (target == CameraCaptureTargetMemory)
                {
                    var data = await GetBase64DataAsync(stream).ConfigureAwait(false);

                    promise.Resolve(new JObject
                    {
                        { "data", data },
                    });
                }
                else
                {
                    var storageFile = await GetOutputStorageFileAsync(MediaTypeImage, target).AsTask().ConfigureAwait(false);

                    var orientation = await cameraForView.GetCameraCaptureOrientationAsync().ConfigureAwait(false);

                    var photoOrientation = CameraRotationHelper.ConvertSimpleOrientationToPhotoOrientation(orientation);
                    await ReencodeAndSavePhotoAsync(stream, storageFile, photoOrientation).ConfigureAwait(false);

                    ;                   await UpdateImagePropertiesAsync(storageFile, options).ConfigureAwait(false);

                    promise.Resolve(new JObject
                    {
                        { "path", storageFile.Path },
                    });
                }
            }
        }
Ejemplo n.º 3
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;
            }
        }
Ejemplo n.º 4
0
        private async Task RecordAsync(CameraForView cameraForView, JObject options, IPromise promise, CancellationToken token)
        {
            var mediaCapture         = cameraForView.MediaCapture;
            var taskCompletionSource = new TaskCompletionSource <bool>();

            using (var cancellationTokenSource = new CancellationTokenSource())
                using (token.Register(cancellationTokenSource.Cancel))
                    using (cancellationTokenSource.Token.Register(async() => await StopRecordingAsync(mediaCapture, taskCompletionSource)))
                    {
                        var quality         = (VideoEncodingQuality)options.Value <int>("quality");
                        var encodingProfile = MediaEncodingProfile.CreateMp4(quality);

                        mediaCapture.AudioDeviceController.Muted = options.Value <bool>("audio");

                        var orientation = await cameraForView.GetCameraCaptureOrientationAsync().ConfigureAwait(false);

                        encodingProfile.Video.Properties.Add(RotationKey, CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(orientation));

                        var stream = default(InMemoryRandomAccessStream);

                        try
                        {
                            var target     = options.Value <int>("target");
                            var outputFile = default(StorageFile);
                            if (target == CameraCaptureTargetMemory)
                            {
                                stream = new InMemoryRandomAccessStream();
                                await mediaCapture.StartRecordToStreamAsync(encodingProfile, stream).AsTask().ConfigureAwait(false);
                            }
                            else
                            {
                                outputFile = await GetOutputStorageFileAsync(MediaTypeVideo, target).AsTask().ConfigureAwait(false);

                                await mediaCapture.StartRecordToStorageFileAsync(encodingProfile, outputFile).AsTask().ConfigureAwait(false);
                            }

                            if (options.ContainsKey("totalSeconds"))
                            {
                                var totalSeconds = options.Value <double>("totalSeconds");
                                if (totalSeconds > 0)
                                {
                                    cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(totalSeconds));
                                }
                            }

                            await taskCompletionSource.Task.ConfigureAwait(false);

                            if (target == CameraCaptureTargetMemory)
                            {
                                var data = await GetBase64DataAsync(stream).ConfigureAwait(false);

                                promise.Resolve(new JObject
                                {
                                    { "data", data },
                                });
                            }
                            else
                            {
                                await UpdateVideoPropertiesAsync(outputFile, options).ConfigureAwait(false);

                                promise.Resolve(new JObject
                                {
                                    { "path", outputFile.Path },
                                });
                            }
                        }
                        finally
                        {
                            stream?.Dispose();
                        }
                    }

            _recordingCancellation.Dispose();
            _recordingTask = null;
        }