Esempio n. 1
0
        /// <summary>
        /// Gets the current orientation of the UI in relation to the device and applies a corrective rotation to the preview
        /// </summary>
        private async Task SetPreviewRotationAsync()
        {
            // Only need to update the orientation if the camera is mounted on the device.
            if (mediaCapture == null || externalCamera)
            {
                return;
            }

            // Calculate which way and how far to rotate the preview.
            int           rotationDegrees;
            VideoRotation sourceRotation;

            CalculatePreviewRotation(out sourceRotation, out rotationDegrees);

            // Set preview rotation in the preview source.
            mediaCapture.SetPreviewRotation(sourceRotation);

            // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
            var props = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationDegrees);
            await mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);

            var currentPreviewResolution = GetPreviewResolution();

            // Setup a frame to use as the input settings
            videoFrame = new VideoFrame(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, (int)currentPreviewResolution.Width, (int)currentPreviewResolution.Height);
        }
        private async Task SetPreviewRotationAsync()
        {
            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, 270);
            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
        private async Task SetPreviewRotationAsync(int degree)
        {
            var videoPreviewProperties = MediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            videoPreviewProperties.Properties.Add(VideoRotationGuid, degree);
            await MediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, videoPreviewProperties, null);
        }
Esempio n. 4
0
        private async Task SetPreviewRotationAsync()
        {
            // Only need to update the orientation if the camera is mounted on the device
            if (_externalCamera)
            {
                return;
            }

            // Populate orientation variables with the current state
            _displayOrientation = _displayInformation.CurrentOrientation;

            // Calculate which way and how far to rotate the preview
            int rotationDegrees = ConvertDisplayOrientationToDegrees((DisplayOrientations)_displayOrientation);

            // The rotation direction needs to be inverted if the preview is being mirrored
            if (_mirroringPreview)
            {
                rotationDegrees = (360 - rotationDegrees) % 360;
            }

            // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationDegrees);
            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
Esempio n. 5
0
        /// <summary>
        /// Gets the current orientation of the UI in relation to the device (when AutoRotationPreferences cannot be honored) and applies a corrective rotation to the preview
        /// </summary>
        private async Task SetPreviewRotationAsync()
        {
            if (_mediaCapture == null || _mediaCapture.CameraStreamState != Windows.Media.Devices.CameraStreamState.Streaming)
            {
                return;
            }

            // Only need to update the orientation if the camera is mounted on the device
            if (_cameraDevice.EnclosureLocation == null || _cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown)
            {
                return;
            }

            // Calculate which way and how far to rotate the preview
            int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);

            // The rotation direction needs to be inverted if the preview is being mirrored
            if (!App.IsXbox() && (_cameraDevice.EnclosureLocation == null || _cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front))
            {
                rotationDegrees = (360 - rotationDegrees) % 360;
            }

            // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationDegrees);
            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        /// <summary>
        /// Set preview rotation and mirroring state to adjust for the orientation of the camera, and for embedded cameras, the rotation of the device.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="type"></param>
        private async Task SetPreviewRotationAsync(DisplayOrientations displayOrientation)
        {
            bool isExternalCamera;
            bool isPreviewMirrored;

            // Figure out where the camera is located to account for mirroring and later adjust rotation accordingly.
            DeviceInformation cameraInformation = await DeviceInformation.CreateFromIdAsync(selectedScanner.VideoDeviceId);

            if ((cameraInformation.EnclosureLocation == null) || (cameraInformation.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown))
            {
                isExternalCamera  = true;
                isPreviewMirrored = false;
            }
            else
            {
                isExternalCamera  = false;
                isPreviewMirrored = (cameraInformation.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
            }

            PreviewControl.FlowDirection = isPreviewMirrored ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;

            if (!isExternalCamera)
            {
                // Calculate which way and how far to rotate the preview.
                int rotationDegrees = 0;
                switch (displayOrientation)
                {
                case DisplayOrientations.Portrait:
                    rotationDegrees = 90;
                    break;

                case DisplayOrientations.LandscapeFlipped:
                    rotationDegrees = 180;
                    break;

                case DisplayOrientations.PortraitFlipped:
                    rotationDegrees = 270;
                    break;

                case DisplayOrientations.Landscape:
                default:
                    rotationDegrees = 0;
                    break;
                }

                // The rotation direction needs to be inverted if the preview is being mirrored.
                if (isPreviewMirrored)
                {
                    rotationDegrees = (360 - rotationDegrees) % 360;
                }

                // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames.
                var streamProperties = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
                streamProperties.Properties[rotationGuid] = rotationDegrees;
                await mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, streamProperties, null);
            }
        }
Esempio n. 8
0
        private async void InitVideoCapture()
        {
            ///摄像头的检测
            var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);

            // var cameraDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);

            if (cameraDevice == null)
            {
                Debug.WriteLine("No camera device found!");
                return;
            }
            var settings = new MediaCaptureInitializationSettings
            {
                StreamingCaptureMode = StreamingCaptureMode.Video,
                //必须,否则截图的时候会很卡很慢
                PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
                MediaCategory      = MediaCategory.Other,
                AudioProcessing    = AudioProcessing.Default,
                VideoDeviceId      = cameraDevice.Id
            };

            _mediaCapture = new MediaCapture();


            //初始化
            await _mediaCapture.InitializeAsync(settings);

            VideoCapture.Source = _mediaCapture;
            await _mediaCapture.StartPreviewAsync();

            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, 90);

            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);

            var focusControl = _mediaCapture.VideoDeviceController.FocusControl;

            if (focusControl.Supported)
            {
                await focusControl.UnlockAsync();

                var setting = new FocusSettings {
                    Mode = FocusMode.Continuous, AutoFocusRange = AutoFocusRange.FullRange
                };
                focusControl.Configure(setting);
                await focusControl.FocusAsync();
            }

            _isPreviewing = true;
            _isInitVideo  = true;
            InitVideoTimer();
        }
 /// <summary>
 /// control the camera frame invert , flip to the mirror image
 /// </summary>
 private async Task SetPreviewRotationAsync()
 {
     if (!_externalCamera)
     {
         // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
         var  rotation    = _rotationHelper.GetCameraPreviewOrientation();
         var  props       = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
         Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1");
         props.Properties.Add(RotationKey, CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(rotation));
         await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Gets the current orientation of the UI in relation to the device and applies a corrective rotation to the preview
        /// </summary>
        private async Task SetPreviewRotationAsync()
        {
            // Only need to update the orientation if the camera is mounted on the device
            if (_externalCamera)
            {
                return;
            }

            // Calculate which way and how far to rotate the preview
            int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);

            // The rotation direction needs to be inverted if the preview is being mirrored
            if (_mirroringPreview)
            {
                rotationDegrees = (360 - rotationDegrees) % 360;
            }

            // Query all properties of the device
            // Order them by resolution then frame rate
            allPreviewProperties = allPreviewProperties.OrderByDescending(x => x.Height * x.Width).OrderByDescending(x => x.FrameRate);


            List <StreamResolution> r = allPreviewProperties.ToList();


            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, r.First().EncodingProperties, null);

            await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, r.First().EncodingProperties);


            // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationDegrees);



            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
Esempio n. 11
0
        private async Task SetPreviewRotationAsync()
        {
            _displayOrientation = _displayInformation.CurrentOrientation;
            int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);

            if (_mirroringPreview)
            {
                rotationDegrees = (360 - rotationDegrees) % 360;
            }

            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(_rotationKey, rotationDegrees);
            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
Esempio n. 12
0
        private async Task SetPreviewRotationAsync()
        {
            // Calculate which way and how far to rotate the preview
            int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayInformation.CurrentOrientation);

            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationDegrees);

            // On Desktop, this setting may stop some of webcams.
            if (AnalyticsInfo.VersionInfo.DeviceFamily != DEVCIE_FAMILY_DESKTOP)
            {
                await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Gets the current orientation of the UI in relation to the device (when AutoRotationPreferences cannot be honored) and applies a corrective rotation to the preview
        /// </summary>
        private async Task SetPreviewRotationAsync()
        {
            // Only need to update the orientation if the camera is mounted on the device
            if (_externalCamera)
            {
                return;
            }

            // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
            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);
        }
Esempio n. 14
0
                public async Task SetPreviewRotationAsync()
                {
                    // Only need to update the orientation if the camera is mounted on the device
                    if (_externalCamera || _rotationHelper == null || m_mediaCapture == null)
                    {
                        return;
                    }

                    // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
                    var rotation = _rotationHelper.GetCameraPreviewOrientation();
                    var props    = m_mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

                    props.Properties.Add(new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1"), CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(rotation));
                    await m_mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
                }
Esempio n. 15
0
        private async Task SetPreviewRotationAsync()
        {
            if (_capture.CameraStreamState != CameraStreamState.Streaming)
            {
                return;
            }

            // Calculate which way and how far to rotate the preview
            int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayInformation.CurrentOrientation);

            // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
            var props = _capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationDegrees);
            await _capture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
        private async Task SetPreviewRotation()
        {
            if (mediaCapture == null)
            {
                return;
            }

            var orientation = DisplayInformation.GetForCurrentView().CurrentOrientation;

            var rotationDegrees = ConvertDisplayOrientationToDegrees(orientation);

            var props = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationDegrees);
            await mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
        public async Task Initialize(int rotation_offset)
        {
            // it collects informations about avaible devices that can capture video
            Devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

            // if there is no current preview running in program or and if there is at least 1 camera avaible
            // then execute operations below - otherwise set error code
            if (Capture == null && Devices.Count > 0)
            {
                // it chooses first avaible device to capture video
                var preferredDevice = Devices.FirstOrDefault();

                // it creates new object from MediaCapture class to capture image
                Capture = new MediaCapture();

                // it prevents display from going to sleep when is inactive
                RequestDisplay.RequestActive();

                // initialization of camera preview
                await Capture.InitializeAsync(
                    new MediaCaptureInitializationSettings
                {
                    VideoDeviceId = preferredDevice.Id
                });

                // it saves all supported resolutions of the camera to 1D array
                var resolutions = Capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).ToList();

                // it sets the type of saved data as picture with choosen resolution
                await Capture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, resolutions[47]); // 5 - 1280x720; 47 - 1280x800;

                // it sets the source of UI element as data stream from camera module
                Preview.Source = Capture;

                // it starts previewing the screen in UI
                await Capture.StartPreviewAsync();

                // it sets rotation (variable is sent to method) of the preview
                var rotation_settings = Capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
                rotation_settings.Properties.Add(RotationKey, rotation_offset);
                await Capture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, rotation_settings, null);
            }
            else
            {
                ErrorCode = "Camera not found";
            }
        }
        private async Task SetPreviewRotationAsync()
        {
            // Calculate which way and how far to rotate the preview
            int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);

            // The rotation direction needs to be inverted if the preview is being mirrored
            if (_mirroringPreview)
            {
                rotationDegrees = (360 - rotationDegrees) % 360;
            }

            // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationDegrees);
            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
Esempio n. 19
0
		private async Task SetPreviewRotationAsync()
		{
			if (_mediaCapture == null) { return; }

			// Populate orientation variables with the current state
			_displayOrientation = _displayInformation.CurrentOrientation;

			// Calculate which way and how far to rotate the preview
			int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);

			// On Desktop, this setting may stop some of webcams.
			if (AnalyticsInfo.VersionInfo.DeviceFamily != WINDOWS_DESKTOP)
			{
				var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
				props.Properties.Add(RotationKey, rotationDegrees);
				await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
			}
		}
Esempio n. 20
0
        private async Task SetPreviewRotationAsync()
        {
            // Populate orientation variables with the current state
            _displayOrientation = _displayInformation.CurrentOrientation;

            // Calculate which way and how far to rotate the preview
            int rotationDegrees = RotationHelper.ConvertDisplayOrientationToDegrees(_displayOrientation);

            // The rotation direction needs to be inverted if the preview is being mirrored
            if (_mirroringPreview)
            {
                rotationDegrees = (360 - rotationDegrees) % 360;
            }

            // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1"), rotationDegrees);
            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
        /// <summary>
        /// Refreshes the preview rotation.
        /// </summary>
        private async Task RefreshPreviewRotation()
        {
            if (AppEnvironment.Instance.IsMobileDeviceFamily)
            {
                // Calculate which way and how far to rotate the preview
                var rotationDegrees = DisplayOrientations.Portrait.ToDegrees();

                // On Phone, we need to mirror the preview picture for the
                // front camera
                if (_device?.EnclosureLocation?.Panel == Panel.Front)
                {
                    rotationDegrees = (360 - rotationDegrees) % 360;
                }

                var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
                props.Properties.Add(RotationKey, rotationDegrees);

                await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
            }
        }
Esempio n. 22
0
        private async Task SetPreviewRotationPropertiesAsync()
        {
            // Only need to update the orientation if the camera is mounted on the device
            if (_externalCamera)
            {
                return;
            }

            // Calculate which way and how far to rotate the preview
            int rotation = ConvertDisplayOrientationToDegrees(DisplayInformation.GetForCurrentView().CurrentOrientation);

            // Get the property meta data about the video.
            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            // Change the meta data to rotate the preview to fill the screen with the preview.
            props.Properties.Add(RotationKey, rotation);

            // Now set the updated meta data into the video preview.
            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
Esempio n. 23
0
        /// <summary>
        /// 设置视频预览方向
        /// </summary>
        /// <returns></returns>
        private async Task SetPreviewRotationAsync()
        {
            //如果外部设备存在
            if (_externalCamera)
            {
                return;
            }
            //获取手机当前方向
            int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);

            //如果是前置摄像头
            if (_mirroringPreview)
            {
                //如果镜像预览,则对称处理
                rotationDegrees = (rotationDegrees + 270) % 360;
            }
            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationDegrees);
            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
Esempio n. 24
0
        /// <summary>
        /// Gets the current orientation of the UI in relation to the device and applies a corrective rotation to the preview.
        /// </summary>
        private async Task SetPreviewRotationAsync()
        {
            // Only need to update the orientation if the camera is mounted on the device.
            if (externalCamera)
            {
                return;
            }

            // Calculate which way and how far to rotate the preview.
            int           rotationDegrees;
            VideoRotation sourceRotation;

            CalculatePreviewRotation(out sourceRotation, out rotationDegrees);

            // Set preview rotation in the preview source.
            mediaCapture.SetPreviewRotation(sourceRotation);

            // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
            var props = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationDegrees);
            await mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
        async Task SetPreviewRotationAsync()
        {
            // Only update the orientation if the camera is mounted on the device
            if (_externalCamera)
            {
                return;
            }

            // Derive the preview rotation
            int rotation = ConvertDisplayOrientationToDegrees(_displayOrientation);

            // Invert if mirroring
            if (_mirroringPreview)
            {
                rotation = (360 - rotation) % 360;
            }

            // Add rotation metadata to preview stream
            var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotation);
            await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
        /// <summary>
        /// A version of MediaCapture.SetPreviewRotation() which does not add
        /// unnecessary black bars.
        /// </summary>
        public static async Task SetPreviewRotationAsync(this MediaCapture capture, VideoRotation rotation)
        {
            // Convert VideoRotation to MFVideoRotationFormat
            uint rotationValue;

            switch (rotation)
            {
            case VideoRotation.None: rotationValue = 0; break;

            case VideoRotation.Clockwise90Degrees: rotationValue = 90; break;

            case VideoRotation.Clockwise180Degrees: rotationValue = 180; break;

            case VideoRotation.Clockwise270Degrees: rotationValue = 270; break;

            default: throw new ArgumentException("rotation");
            }

            // Rotate preview
            var props = capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);

            props.Properties.Add(RotationKey, rotationValue);
            await capture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
        }
        private async Task <bool> InitVideoCapture()
        {
            mediaCapture = new MediaCapture();
            var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);

            if (cameraDevice == null)
            {
                await UiUtils.NotifyUser("No camera device found!");

                return(false);
            }

            var settings = new MediaCaptureInitializationSettings
            {
                StreamingCaptureMode = StreamingCaptureMode.Video,
                MediaCategory        = MediaCategory.Other,
                AudioProcessing      = Windows.Media.AudioProcessing.Default,
                PhotoCaptureSource   = PhotoCaptureSource.VideoPreview,
                VideoDeviceId        = cameraDevice.Id
            };

            try
            {
                await mediaCapture.InitializeAsync(settings);
            }
            catch (UnauthorizedAccessException)
            {
                await UiUtils.NotifyUser("Please turn on the camera permission of the app to ensure scan QR code normaly.");

                return(false);
            }

            var focusControl = mediaCapture.VideoDeviceController.FocusControl;

            if (focusControl.Supported)
            {
                var focusSettings = new FocusSettings()
                {
                    Mode = focusControl.SupportedFocusModes.FirstOrDefault(f => f == FocusMode.Continuous),
                    DisableDriverFallback = true,
                    AutoFocusRange        = focusControl.SupportedFocusRanges.FirstOrDefault(f => f == AutoFocusRange.FullRange),
                    Distance = focusControl.SupportedFocusDistances.FirstOrDefault(f => f == ManualFocusDistance.Nearest)
                };
                focusControl.Configure(focusSettings);
            }
            VideoCapture.Source        = mediaCapture;
            VideoCapture.FlowDirection = FlowDirection.LeftToRight;
            // Fix preview mirroring for front webcam
            if (cameraDevice.EnclosureLocation?.Panel == Windows.Devices.Enumeration.Panel.Front)
            {
                VideoCapture.FlowDirection = FlowDirection.RightToLeft;
            }
            await mediaCapture.StartPreviewAsync();

            // Fix preview orientation for portrait-first devices (such as Windows Phone)
            if (DisplayInformation.GetForCurrentView().NativeOrientation == DisplayOrientations.Portrait)
            {
                var  props       = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
                Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1");
                props.Properties.Add(RotationKey, 90);
                await mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
            }
            if (mediaCapture.VideoDeviceController.FlashControl.Supported)
            {
                mediaCapture.VideoDeviceController.FlashControl.Enabled = false;
            }

            if (focusControl.Supported)
            {
                await focusControl.FocusAsync();
            }
            return(true);
        }