Camera

Defined in package com.scandit.datacapture.core.source

TorchState

Added in version 6.0.0

Possible values for the torch state.

ON

Added in version 6.0.0

Value to indicate that the torch is turned on.

OFF

Added in version 6.0.0

Value to indicate that the torch is turned off.

AUTO

Added in version 6.3.0

Value to indicate that the torch is managed automatically. The torch is turned on or off based on the available illumination.

Requires camera API 2. To make use of the automatic switch to API 2, make sure that you instantiate the camera directly with the settings instead of using Camera.applySettings().

Note

This is an experimental feature. This functionality may or may not work for your use case or may be removed in future version of this software.

TorchListener
interface TorchListener

Added in version 6.3.0

Interface definition for a callback to be invoked when the TorchState of a Camera changes.

onTorchStateChanged(state)
void onTorchStateChanged(TorchState state)

Added in version 6.3.0

Called when TorchState changed.

Camera
class Camera : FrameSource

Added in version 6.0.0

Gives access to the built-in camera on Android. It implements the FrameSource interface, and, as such can be set as the frame source for the DataCaptureContext.

Instances of this class are created through one of the factory methods getDefaultCamera(), or getCamera().

The camera is started by changing the desired state to FrameSourceState.ON.

By default, the resolution of captured frames as well as auto-focus and exposure settings are chosen such that they work best for a variety of use cases. To fine-tune recognition, the camera settings can be changed through applying new camera settings.

Runtime permissions

Starting from Android 6.0 (API level 23), the camera permissions can no longer be requested during app installation. Instead, permissions are requested at runtime. If your application targets API level 23 and higher, you will need to add logic to handle camera permissions before turning it on.

To achieve this, you need to check system version and if camera permission is already granted. If it’s not, you have to ask for the Manifest.permission.CAMERA permission from your Fragment or Activity. If permission has already been granted, you can enable the camera by calling desired state with FrameSourceState.ON. When the answer was negative, invoke requestPermissions and try again on onRequestPermissionsResult callback.

private void turnOnCamera() {
    if (checkSelfPermission(Manifest.permission.CAMERA) != PERMISSION_GRANTED) {
        requestPermissions(
            new String[]{Manifest.permission.CAMERA},
            CAMERA_PERMISSION_REQUEST
        );
    } else {
        camera.switchToDesiredState(FrameSourceState.ON, null);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == CAMERA_PERMISSION_REQUEST
        && grantResults.length > 0
        && grantResults[0] == PERMISSION_GRANTED) {
        turnOnCamera();
    } else {
        // Show message about denied Camera permission.
    }
}

In your application, you will most likely want to rely on the functionality implemented by the app compat library for handling the diffences between API versions. For more information on the permissions system introduced in Android 23, take a look at the Android Permission Documentation.

desiredState
FrameSourceState getDesiredState()

Added in version 6.0.0

Implemented from FrameSource. See FrameSource.desiredState.

currentState
FrameSourceState getCurrentState()

Added in version 6.0.0

Implemented from FrameSource. See FrameSource.currentState.

position
CameraPosition getPosition()

Added in version 6.0.0

The direction that the camera faces.

addListener(listener)
void addListener(@Nullable FrameSourceListener listener)

Added in version 6.0.0

Implemented from FrameSource. See FrameSource.addListener().

removeListener(listener)
void removeListener(@Nullable FrameSourceListener listener)

Added in version 6.0.0

Implemented from FrameSource. See FrameSource.removeListener().

getDefaultCamera()
static @Nullable Camera getDefaultCamera()

Added in version 6.0.0

Gets the default camera of the device. This method is identical to calling getCamera() repeatedly, first with CameraPosition.WORLD_FACING, then with CameraPosition.USER_FACING followed by CameraPosition.UNSPECIFIED, stopping after the first of these calls returns a non-null instance.

See getCamera() for a more detailed description of the method behavior.

getDefaultCamera(settings)
static @Nullable Camera getDefaultCamera(@Nullable CameraSettings settings)

Added in version 6.0.0

This method is identical to calling getDefaultCamera() and applySettings() consecutively.

getCamera(position)
static @Nullable Camera getCamera(CameraPosition position)

Added in version 6.0.0

Retrieves the camera instance of the first camera at the provided position. In case the system does not have a camera at the provided position, null is returned.

When this method is called multiple times with the same argument, the same Camera instance is returned. The FrameSource.currentState of the camera as well as the settings depend on previous invocations. For example, if the camera is currently in use and is active, the camera’s FrameSource.currentState will be FrameSourceState.ON. The only guarantee about the state and settings is that when instance is initially created, the FrameSource.currentState is FrameSourceState.OFF and has the default CameraSettings.

Important

In case parts of your app use custom camera settings and others use the default settings, make sure to reset the camera to use the default settings when you need them by passing the default camera settings to applySettings() to ensure that you don’t have any other settings when you’d expect the defaults to be active.

The camera object is returned if present, regardless whether the application has permissions to use it or not.

The camera will use application context and the same camera object can be reused with different activities.

Starting from Android 6.0, permission to use the camera will have to be requested at runtime. This will have to be implemented in the activity itself. Consult the runtime permission documentation for details.

getCamera(position, settings)
static @Nullable Camera getCamera(CameraPosition position,
        @NonNull CameraSettings settings)

Added in version 6.0.0

This method is identical to calling getCamera() and then applySettings() consecutively.

fromJson(jsonData)
static @Nullable Camera fromJson(@NonNull String jsonData)

Added in version 6.0.0

Constructs a new camera with the provided JSON serialization. See Serialization for details.

switchToDesiredState(desiredState, whenDone)
void switchToDesiredState(FrameSourceState desiredState,
        Callback<@NonNull Boolean> whenDone)

Added in version 6.0.0

Implemented from FrameSource. See FrameSource.switchToDesiredState().

switchToDesiredState(desiredState)
void switchToDesiredState(FrameSourceState desiredState)

Added in version 6.0.0

Convenience method for FrameSource.switchToDesiredState(): it is the same as calling switchToDesiredState() with the second argument set to null.

applySettings(settings, whenDone)
void applySettings(@NonNull CameraSettings settings, @Nullable Runnable whenDone)

Added in version 6.0.0

Applies the camera settings to the camera. The task will complete when the settings have been applied and the camera has switched to use the new settings. If the camera is currently in FrameSourceState.OFF state, the task will complete immediately. If, on the other hand, the camera is currently in FrameSourceState.ON state, the settings will be modified on the fly.

applySettings(settings)
void applySettings(@NonNull CameraSettings settings)

Added in version 6.0.0

Same as applySettings() with the second argument set to null.

isTorchAvailable
boolean isTorchAvailable()

Added in version 6.0.0

Whether the torch is available for the given camera. When the camera is off, this method always returns false. When the camera is on, it returns true for cameras that support the torch, false otherwise.

desiredTorchState
TorchState getDesiredTorchState()
void setDesiredTorchState(TorchState value)

Added in version 6.0.0

The desired torch state for this camera. By default, the torch state is TorchState.OFF. When setting the desired torch state to TorchState.ON, the torch will be on as long as the camera is running (the camera’s state is FrameSourceState.ON) and off otherwise.

When setting the desired torch state for a camera that does not have a torch (see isTorchAvailable), this call has no effect.

addTorchListener(listener)
void addTorchListener(@NonNull TorchListener listener)

Added in version 6.3.0

Add a listener that will be called when TorchState of the camera changes.

removeTorchListener(listener)
void removeTorchListener(@NonNull TorchListener listener)

Added in version 6.3.0

Remove a listener for TorchState changes.

updateFromJson(jsonData)
void updateFromJson(@NonNull String jsonData)

Added in version 6.0.0

Updates the camera according to a JSON serialization. See Serialization for details.