I use MediapCapture to scan QrCode. Here is my configuration:
var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
int cameraIndex = cameras.Count > 1 ? 1 : 0;
MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[cameraIndex].Id };
settings.StreamingCaptureMode = StreamingCaptureMode.Video;
await _mediaCapture.InitializeAsync();
SetResolution();
_mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
_mediaCapture.VideoDeviceController.FocusControl.Configure(new Windows.Media.Devices.FocusSettings() { Mode = Windows.Media.Devices.FocusMode.Auto });
videoCapture.Source = _mediaCapture;
await _mediaCapture.StartPreviewAsync();
And I have also a Tapped event on my CaptureElement which call the FocusAsync method:
await _mediaCapture.VideoDeviceController.FocusControl.FocusAsync();
My issue is that I have sometimes (not always) an error when I try to focus (even if it's the first time I call it). This error is:
The op is invalid
EDIT:
Here is my stack trace: (there is no inner exception)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at ProjectName.Views.QrCodeScannerPage.d__8.MoveNext()
Finally I choose to set auto focus from the following examples: https://msdn.microsoft.com/en-us/windows/uwp/audio-video-camera/capture-device-controls-for-photo-and-video-capture