Advanced Camera Functionality#

In this guide, you will learn about advanced camera functionality not explained in the “Getting Started” guides.

Before you start…

To get the most out of this guide, we recommend that you first take a look at the following guides:

The data capture context supports using different frame sources to perform recognition on. Most applications will use the built-in camera of the device, e.g. the world-facing camera of a device. The remainder of this guide will assume that you use the built-in camera.

Switching between World-Facing and User-Facing#

In your app you might want to switch between using the world-facing and user-facing camera for different use cases. As these are different cameras entirely switching between them is done by instantiating an entirely new camera and setting it as the data capture context’s new frame source.

Let’s start with the world facing camera. What we do is very similar to the camera section in the “Getting Started” guides, but here we want to specifically use the world-facing camera. Remember to use the recommended camera settings for whichever data capture mode you are using.

CameraSettings cameraSettings = ...;
Camera worldFacingCamera = Camera.GetCamera(CameraPosition.WorldFacing);

if (worldFacingCamera != null)
{
    worldFacingCamera.ApplySettingsAsync(cameraSettings);

    // Set the camera as the frame source and turn it on.
    dataCaptureContext.SetFrameSourceAsync(worldFacingCamera);
    worldFacingCamera.SwitchToDesiredStateAsync(FrameSourceState.On);
}

At this point we have a running world-facing camera that is used by the context. Whenever we want to switch to a user-facing camera, we simply turn off the world-facing camera and create a new user-facing camera that we set as the new frame source.

worldFacingCamera.SwitchToDesiredStateAsync(FrameSourceState.Off);

Camera userFacingCamera = Camera.GetCamera(CameraPosition.UserFacing);

if (userFacingCamera != null)
{
    userFacingCamera.ApplySettingsAsync(cameraSettings);

    // Set the new camera as the frame source and turn it on.
    dataCaptureContext.SetFrameSourceAsync(userFacingCamera);
    userFacingCamera.SwitchToDesiredStateAsync(FrameSourceState.On);
}