private async Task InitializeCameraAsync(DeviceInformation preferredDevice) { try { // if (_mediaCapture == null) //{ Debug.WriteLine("open device"); // Get the camera devices try { // Create MediaCapture _mediaCapture = new MediaCapture(); // Initialize MediaCapture and settings await _mediaCapture.InitializeAsync( new MediaCaptureInitializationSettings { VideoDeviceId = preferredDevice.Id }); // Handle camera device location if (preferredDevice.EnclosureLocation == null || preferredDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown) { Debug.WriteLine("null"); _externalCamera = true; } else { _externalCamera = false; _mirroringPreview = (preferredDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front); } // ShowMessage(preferredDevice.EnclosureLocation.ToString()); _rotationHelper = new CameraRotationHelper(preferredDevice.EnclosureLocation); _rotationHelper.OrientationChanged += RotationHelper_OrientationChanged; _mediaCapture.GetPreviewRotation(); // Set the preview source for the CaptureElement PreviewControl.Visibility = Visibility.Visible; PreviewControl.Source = _mediaCapture; PreviewControl.FlowDirection = _mirroringPreview ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; // popUpDisplayText1.Text = "Let's take a picture! Smile~"; // Start viewing through the CaptureElement await _mediaCapture.StartPreviewAsync(); await SetPreviewRotationAsync(); } catch (Exception e) { ShowMessage(e.ToString()); } // } } catch (UnauthorizedAccessException Error) { ShowMessage(Error.ToString()); } }
async void CaptureAndRotate(MediaCapture captureMgrReal) { // <SnippetCaptureRotateAll> // <SnippetCaptureRotateInit> MediaCapture captureMgr = new MediaCapture(); // Set the MediaCapture to a variable in App.xaml.cs to handle suspension. (App.Current as App).MediaCapture = captureMgr; await captureMgr.InitializeAsync(); // </SnippetCaptureRotateInit> // <SnippetCaptureRotateSetRotate> captureMgr.SetPreviewRotation(VideoRotation.Clockwise90Degrees); // </SnippetCaptureRotateSetRotate> // <SnippetCaptureRotateGetRotate> VideoRotation previewRotation = captureMgr.GetPreviewRotation(); // </SnippetCaptureRotateGetRotate> // Start capture preview. // capturePreview is a CaptureElement defined in XAML. capturePreview.Source = captureMgr; // Set the CaptureElement to a variable in App.xaml.cs to handle suspension. (App.Current as App).PreviewElement = capturePreview; // Lock the orientation of the display while previewing. DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape; await captureMgr.StartPreviewAsync(); // Set a tracking variable for preview state in App.xaml.cs (App.Current as App).IsPreviewing = true; // </SnippetCaptureRotateAll> }