Beispiel #1
0
        public CameraForView GetOrCreateCameraForView(CaptureElement view)
        {
            var           viewTag      = view.GetTag();
            var           reactContext = view.GetReactContext();
            CameraForView result;

            if (!_cameras.TryGetValue(viewTag, out result))
            {
                result = new CameraForView(view);
                _cameras.Add(viewTag, result);
                reactContext.AddLifecycleEventListener(result);
            }

            return(result);
        }
Beispiel #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 },
                    });
                }
            }
        }
Beispiel #3
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;
        }