async Task StartPreview() { if (String.Equals(Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily, "Windows.Mobile")) { mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees); mediaCapture.SetPreviewMirroring(true); } var focusControl = mediaCapture.VideoDeviceController.FocusControl; if (!focusControl.FocusChangedSupported) { if (focusControl.Supported) { _cleanedUp = false; _processScan = true; VideoCaptureElement.Source = mediaCapture; VideoCaptureElement.Stretch = Stretch.UniformToFill; await mediaCapture.StartPreviewAsync(); await focusControl.UnlockAsync(); focusControl.Configure(new FocusSettings { Mode = FocusMode.Auto }); timerFocus.Tick += timerFocus_Tick; timerFocus.Interval = new TimeSpan(0, 0, 3); timerFocus.Start(); } else { OnErrorAsync(new Exception("AutoFocus control is not supported on this device")); } } else { _cleanedUp = false; _processScan = true; mediaCapture.FocusChanged += mediaCaptureManager_FocusChanged; VideoCaptureElement.Source = mediaCapture; VideoCaptureElement.Stretch = Stretch.UniformToFill; await mediaCapture.StartPreviewAsync(); await focusControl.UnlockAsync(); var settings = new FocusSettings { Mode = FocusMode.Continuous, AutoFocusRange = AutoFocusRange.FullRange }; focusControl.Configure(settings); await focusControl.FocusAsync(); } }
/// <summary> /// Initialises the camera and resolves the resolution for both the /// full resolution and preview images. /// </summary> private async void InitializeCameraAsync() { if (CameraState != CameraStates.NotInitialized) { Debug.WriteLine(DebugTag + "InitializeCameraAsync(): Invalid state"); return; } Debug.WriteLine(DebugTag + "InitializeCameraAsync() ->"); CameraState = CameraStates.Initializing; ProgressIndicator.IsActive = true; MessageDialog messageDialog = null; #if WINDOWS_PHONE_APP DeviceInformationCollection devices; devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); if (devices.Count == 0) { ProgressIndicator.IsActive = false; CameraState = CameraStates.NotInitialized; messageDialog = new MessageDialog( _resourceLoader.GetString("FailedToFindCameraDevice/Text"), _resourceLoader.GetString("CameraInitializationFailed/Text")); await messageDialog.ShowAsync(); return; } // Use the back camera DeviceInformation info = devices[0]; _cam = true; foreach (var devInfo in devices) { if (devInfo.Name.ToLowerInvariant().Contains("back")) { info = devInfo; _cam = false; // Set this to true if you use front camera break; } } MyCaptureElement.FlowDirection = _cam ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; #endif _mediaCapture = new MediaCapture(); try { #if WINDOWS_PHONE_APP await _mediaCapture.InitializeAsync( new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Video, PhotoCaptureSource = PhotoCaptureSource.VideoPreview, AudioDeviceId = string.Empty, VideoDeviceId = info.Id }); #else await _mediaCapture.InitializeAsync(); #endif } catch (Exception ex) { messageDialog = new MessageDialog(ex.ToString(), _resourceLoader.GetString("CameraInitializationFailed/Text")); } MyCaptureElement.Source = _mediaCapture; if (messageDialog != null) { /* Add commands and set their callbacks; both buttons use the * same callback function instead of inline event handlers */ if (messageDialog.Commands != null) { messageDialog.Commands.Add(new UICommand(_resourceLoader.GetString("Retry/Text"), CommandInvokedHandler)); messageDialog.Commands.Add(new UICommand(_resourceLoader.GetString("Cancel/Text"), CommandInvokedHandler)); } // Set the command that will be invoked by default messageDialog.DefaultCommandIndex = 0; // Set the command to be invoked when escape is pressed messageDialog.CancelCommandIndex = 1; await messageDialog.ShowAsync(); } else { // Get the resolution uint width = 0; uint height = 0; IMediaEncodingProperties propertiesToSet = null; AppUtils.GetBestResolution(_mediaCapture, ref width, ref height, ref propertiesToSet); if (width > 0 && height > 0) { _dataContext.SetFullResolution((int)width, (int)height); int previewWidth = (int)FilterEffects.DataContext.DefaultPreviewResolutionWidth; int previewHeight = 0; AppUtils.CalculatePreviewResolution((int)width, (int)height, ref previewWidth, ref previewHeight); _dataContext.SetPreviewResolution(previewWidth, previewHeight); } if (propertiesToSet != null) { await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync( MediaStreamType.Photo, propertiesToSet); Debug.WriteLine(DebugTag + "Capture resolution set to " + width + "x" + height + "!"); } else { Debug.WriteLine(DebugTag + "Failed to set capture resolution! Using fallback resolution."); var fallbackResolution = new Size( FilterEffects.DataContext.DefaultPreviewResolutionWidth, FilterEffects.DataContext.DefaultPreviewResolutionHeight); _dataContext.PreviewResolution = fallbackResolution; _dataContext.FullResolution = fallbackResolution; } _mediaCapture.SetPreviewMirroring(false); await _mediaCapture.StartPreviewAsync(); } #if WINDOWS_PHONE_APP // In case front camera is used, we need to handle the rotations DisplayInformation displayInfo = DisplayInformation.GetForCurrentView(); displayInfo.OrientationChanged += DisplayInfo_OrientationChanged; DisplayInfo_OrientationChanged(displayInfo, null); #endif CameraState = CameraStates.Initialized; ProgressIndicator.IsActive = false; CaptureButton.IsEnabled = true; Debug.WriteLine(DebugTag + "InitializeCameraAsync() <-"); }
public void SetPreviewMirroring(Boolean isPreviewMirrored) { _captureManager.SetPreviewMirroring(isPreviewMirrored); }