0
votes

I am displaying a viewfinder for a Dell Venue 8 Pro tablet camera using a MediaCapture instance. The tablet is running Windows 8.1 Pro. The issue I am facing is that the camera seems to be slow at focusing and adjusting according to the lighting in the room. The preview can flash between too dark and too bright very quickly and seems to struggle to get the optimal lighting setting. Using the stock camera app has no trouble focusing. Here is the code I am using to initialize the camera.

// Attempt to get the back camera if one is available, but use any camera device if not
var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
if (cameraDevice == null)
{
    Debug.WriteLine("No camera device found!");
    return;
}
var settings = new MediaCaptureInitializationSettings { 
    VideoDeviceId = cameraDevice.Id, 
    AudioDeviceId = "",
    StreamingCaptureMode = StreamingCaptureMode.Video, 
    PhotoCaptureSource = PhotoCaptureSource.VideoPreview
};
//setup media capture
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(settings);
mediaCapture.VideoDeviceController.PrimaryUse = CaptureUse.Photo;
ViewFinder.Source = mediaCapture;
mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
await mediaCapture.StartPreviewAsync();

How can I optimize the MediaCapture to be quick at focusing? (The app needs the MediaCapture for decoding a QR code)

1

1 Answers

0
votes

It sounds like you want to use continuous autofocus. This snippet should get you started:

var focusControl = _mediaCapture.VideoDeviceController.FocusControl;

await focusControl.UnlockAsync();

// You may want to use AutoFocusRange.Macro for QR codes. Make sure it's within the supported modes though
var settings = new FocusSettings { Mode = FocusMode.Continuous, AutoFocusRange = AutoFocusRange.FullRange };

focusControl.Configure(settings);
await focusControl.FocusAsync();

Also, I noticed you're using SetPreviewRotation. The official CameraStarterKit sample recommends rotating the preview as follows instead, for efficiency:

// Rotation metadata to apply to the preview stream and recorded videos (MF_MT_VIDEO_ROTATION)
// Reference: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868174.aspx
readonly Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1");

// 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);

You can learn a little more about preview rotation at the camera session from the //build/ presentation.