close

 

    public partial class TakePhotoPage
    {
        private MediaCapture _mediaCapture;
        private bool _isPreviewing;
        private DisplayRequest _displayRequest;

        private async Task StartPreviewAsync()
        {
            try
            {
                _mediaCapture = new MediaCapture();
                await _mediaCapture.InitializeAsync();

                PreviewControl.Source = _mediaCapture;
                await _mediaCapture.StartPreviewAsync();
                _isPreviewing = true;

                _displayRequest = new DisplayRequest();
                _displayRequest.RequestActive();
                DisplayInformation.AutoRotationPreferences = DisplayOrientations.PortraitFlipped;

                // 解決手機上的旋轉問題
                SimpleOrientationSensor sensor = SimpleOrientationSensor.GetDefault();
                if (sensor == null)
                {
                    return;
                }

                sensor.OrientationChanged += (s, arg) =>
                {
                    switch (arg.Orientation)
                    {
                        case SimpleOrientation.Rotated90DegreesCounterclockwise:
                            _mediaCapture.SetPreviewRotation(VideoRotation.None);
                            break;
                        case SimpleOrientation.Rotated180DegreesCounterclockwise:
                        case SimpleOrientation.Rotated270DegreesCounterclockwise:
                            _mediaCapture.SetPreviewRotation(VideoRotation.Clockwise180Degrees);
                            break;
                        default:
                            _mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
                            break;
                    }
                };

            }
            catch (UnauthorizedAccessException)
            {
                // This will be thrown if the user denied access to the camera in privacy settings
                var messageDialog = new MessageDialog("The app was denied access to the camera");
                await messageDialog.ShowAsync();
            }
            catch (Exception ex)
            {
                var messageDialog = new MessageDialog("MediaCapture initialization failed. {0}", ex.Message);
                await messageDialog.ShowAsync();
            }
        }

        private async Task CleanupCameraAsync()
        {
            if (_mediaCapture == null)
            {
                return;
            }

            if (_isPreviewing)
            {
                await _mediaCapture.StopPreviewAsync();
            }

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                PreviewControl.Source = null;

                if (_displayRequest == null)
                {
                    return;
                }

                _displayRequest.RequestRelease();
            });

            _mediaCapture.Dispose();
            _mediaCapture = null;

        }

    }

 

 

參考資料:

https://msdn.microsoft.com/en-us/windows/uwp/audio-video-camera/simple-camera-preview-access

http://stackoverflow.com/questions/27109817/how-to-rotate-camera-view-in-captureelement

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 小賢 的頭像
    小賢

    小賢的部落格

    小賢 發表在 痞客邦 留言(0) 人氣()