BStateham
BStateham

Reputation: 1629

Programmatically zooming the AudioVideoCaptureDevice?

Anybody know how to programmatically zoom the AudioVideoCaptureDevice in Windows Phone 8?

I am using AudioVideoCaptureDevice (and yes, I want that specific device so I can control the VideoTorchMode property). I can't for the life of me figure out the zooming though. I am painting a Canvas using a VideoBrush mapped to the AudioVideoCaptureDevice. I'd like to implement Pinch-Zoom or even a simple +/- button to Zoom the camera.

What am I missing?

Upvotes: 1

Views: 1850

Answers (1)

JustinAngel
JustinAngel

Reputation: 16102

I'm not familiar with any API in WP8 that would allow you to programmetically set the zoom on a PhotoCaptureDevice/AudioVideoCaptureDevice. My theory is that you can do it manually by implementing your own Pinch-to-zoom functionality and making sure that region is focused.

For information on how to Focus on a region using WP8 Camera APIs see Nokia's Camera Explorer. The core of what you're looking for can be found on this architectural guide under "tap-to-focus".

private async void videoCanvas_Tap(object sender, GestureEventArgs e)
{
    System.Windows.Point uiTapPoint = e.GetPosition(VideoCanvas);
    if (_focusSemaphore.WaitOne(0))
    {
        // Get tap coordinates as a foundation point
        Windows.Foundation.Point tapPoint = new Windows.Foundation.Point(uiTapPoint.X, uiTapPoint.Y);

        double xRatio = VideoCanvas.ActualWidth / _dataContext.Device.PreviewResolution.Width;
        double yRatio = VideoCanvas.ActualHeight / _dataContext.Device.PreviewResolution.Height;

        // adjust to center focus on the tap point
        Windows.Foundation.Point displayOrigin = new Windows.Foundation.Point(
            tapPoint.X - _focusRegionSize.Width / 2,
            tapPoint.Y - _focusRegionSize.Height / 2);

        // adjust for resolution difference between preview image and the canvas
        Windows.Foundation.Point viewFinderOrigin = new Windows.Foundation.Point(displayOrigin.X / xRatio, displayOrigin.Y / yRatio);
        Windows.Foundation.Rect focusrect = new Windows.Foundation.Rect(viewFinderOrigin, _focusRegionSize);

        // clip to preview resolution
        Windows.Foundation.Rect viewPortRect = new Windows.Foundation.Rect(0, 0, _dataContext.Device.PreviewResolution.Width, _dataContext.Device.PreviewResolution.Height);
        focusrect.Intersect(viewPortRect);

        _dataContext.Device.FocusRegion = focusrect;

        // show a focus indicator
        FocusIndicator.SetValue(Shape.StrokeProperty, _notFocusedBrush);
        FocusIndicator.SetValue(Canvas.LeftProperty, uiTapPoint.X - _focusRegionSize.Width / 2);
        FocusIndicator.SetValue(Canvas.TopProperty, uiTapPoint.Y - _focusRegionSize.Height / 2);
        FocusIndicator.SetValue(Canvas.VisibilityProperty, Visibility.Visible);

        CameraFocusStatus status = await _dataContext.Device.FocusAsync();
        if (status == CameraFocusStatus.Locked)
        {
            FocusIndicator.SetValue(Shape.StrokeProperty, _focusedBrush);
            _manuallyFocused = true;
            _dataContext.Device.SetProperty(KnownCameraPhotoProperties.LockedAutoFocusParameters,
                AutoFocusParameters.Exposure & AutoFocusParameters.Focus & AutoFocusParameters.WhiteBalance);
        }
        else
        {
            _manuallyFocused = false;
            _dataContext.Device.SetProperty(KnownCameraPhotoProperties.LockedAutoFocusParameters, AutoFocusParameters.None);
        }
        _focusSemaphore.Release();
    }
}

Here's how to implement your own pinch-to-zoom functionality in WP8 @ Pinch To Zoom functionality in windows phone 8

One thing I'd add to the pinch-to-zoom code sample in your case is a Clip specification on a parent control to make sure you're not accidentally rendering images tens or hundreds of times bigger then the screen and killing your app's performance.

Upvotes: 2

Related Questions