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.

const cameraSettings = ...;
const worldFacingCamera = Scandit.Camera.atPosition(Scandit.CameraPosition.WorldFacing);

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

  // Set the camera as the frame source and turn it on.
  context.setFrameSource(worldFacingCamera);
  worldFacingCamera.switchToDesiredState(Scandit.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.switchToDesiredState(Scandit.FrameSourceState.Off);

const userFacingCamera = Scandit.Camera.atPosition(Scandit.CameraPosition.UserFacing);

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

  // Set the camera as the frame source and turn it on.
  context.setFrameSource(userFacingCamera);
  userFacingCamera.switchToDesiredState(Scandit.FrameSourceState.On);
}

Using the Standby State#

The standby state is an addition in between the states Off and On.

We recommend to use Standby when the user is likely scanning in quick succession and you want to avoid delays when starting the camera.

The standby state is implemented slightly different on Android and iOS:

  • On Android, while in Standby, the camera is not producing any frames just like during Off but certain parts of the camera are already initialized to be able to switch to On much quicker.

  • On iOS, while in Standby, the camera is running (note that on iOS 14 or greater a green indicator will appear in the status bar), but frames are not processed and no camera preview is shown just like during Off.

Because of this, it is important to be selective about the usage of the standby state and avoid keeping the camera in standby throughout the entire app. To minimize the additional battery usage it is better to go into standby only on certain screens where repeated scanning is expected. In rare cases where the entire app does repeated scanning, keeping the camera in standby throughout the entire app might be worth the additional battery usage.

Note

When using Camera API 1 (the default) it is currently not possible to switch the resolution of the preview while the camera is running. This restriction also counts for the standby state but will be addressed for 6.7.0.

App Flow with Standby State#

When using the standby state the typical app flow will be along the following lines:

  • Move to a screen in the app that has scanning capabilities

  • Change the camera’s state to Standby

  • Repeatedly change to On and back to Standby as the user starts and stops scanning

  • Move away from the screen and change the camera state back to Off

For apps that are fully centered around scanning it might be a better option to stay in standby throughout at the cost of the slight battery usage increase:

  • Start with the camera’s state at Standby

  • Repeatedly change to On and back to Standby as the user starts and stops scanning

Setting the Standby State#

Setting the standby state is very straight forward and works just like any other settable FrameSourceState:

camera.switchToDesiredState(Scandit.FrameSourceState.Standby);

// Time passes until the scanner should become active
camera.switchToDesiredState(Scandit.FrameSourceState.On);

// After scanning the camera can go to standby again
camera.switchToDesiredState(Scandit.FrameSourceState.Standby);