Seth Kitchen
Seth Kitchen

Reputation: 1566

How do I Focus a Camera in Windows Universal Apps?

I am working with the Windows Universal Sample for OCR located here:

https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/OCR/cs

Specifically the OcrCapturedImage.xaml.cs

It seems that the camera often becomes unfocused, blurry, and nowhere near as good quality as the native camera app. How can I set up autofocusing and/or tap to fix exposure?

What I have tried so far is looking at the other camera samples which help set resolution, but I cannot find anything about focus/exposure.

Update:

I think

await mediaCapture.VideoDeviceController.FocusControl.FocusAsync();

and

await mediaCapture.VideoDeviceController.ExposureControl.SetAutoAsync(true);

But this isn't working (does nothing-still blurry etc.) and could be built upon if someone knows how to tap a certain area and apply focus/exposure accordingly.

Native Camera:

enter image description here

App Camera:

enter image description here

Update based on answer:

I must have been putting my focus methods in the wrong spot because my original update code works. Sergi's also works. I want to used the tapped event in combination with it, something like this:

Point tapped=e.GetPosition(null); //Where e is TappedRoutedEventArgs from a tapped event method on my preview screen
await mediaCapture.VideoDeviceController.RegionsOfInterestControl.ClearRegionsAsync();
await mediaCapture.VideoDeviceController.RegionsOfInterestControl.SetRegionsAsync(new[] { new RegionOfInterest() { Bounds = new Rect(tapped.X, tapped.Y, 0.02, 0.02) } }); //Throws Parameter Incorrect

But it throws parameter incorrect. Also, How would I show the overlay a Rectangle on the preview screen, so the user knows how big the region of interest is?

This is a great link https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/CameraManualControls/cs/MainPage.Focus.xaml.cs

Upvotes: 9

Views: 4243

Answers (1)

Sergii Zhevzhyk
Sergii Zhevzhyk

Reputation: 4202

Configuration of the auto focus using the Configure method of the FocusControl class.

mediaCapture.VideoDeviceController.FocusControl.Configure(
    new FocusSettings { Mode = FocusMode.Auto });
await mediaCapture.VideoDeviceController.FocusControl.FocusAsync();

In order to focus on a certain area, the RegionOfInterestControl propery can be used. It has the SetRegionsAsync method to add a collection of RegionOfInterest instances. RegionOfInterest has the Bounds property which defines the region of focus. This example shows how to set the focus in the center:

// clear previous regions of interest
await mediaCapture.VideoDeviceController.RegionOfInterestControl.ClearRegionsAsync();
// focus in the center of the screen
await mediaCapture.VideoDeviceController.RegionOfInterestControl.SetRegionsAsync(
    new [] 
         { 
             new RegionOfInterest() {Bounds = new Rect(0.49,0.49,0.02,0.02) } 
         });

Upvotes: 8

Related Questions