Camera

Defined in namespace Scandit.DataCapture.Core.Source

TorchState

Added in version 6.16.0

Possible values for the torch state.

On

Added in version 6.16.0

Value to indicate that the torch is turned on.

Off

Added in version 6.16.0

Value to indicate that the torch is turned off.

Auto

Added in version 6.16.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.ApplySettingsAsync().

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.

ITorchListener
interface ITorchListener

Added in version 6.16.0

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

OnTorchStateChanged()
void OnTorchStateChanged(TorchState state)

Added in version 6.16.0

Called when TorchState changed.

TorchEventArgs
class TorchEventArgs : EventArgs

Added in version 6.16.0

Provides data for Camera.TorchStateChanged event.

TorchEventArgs()
TorchEventArgs(TorchState state)

Added in version 6.16.0

State
TorchState State { get; }

Added in version 6.16.0

Camera
class Camera : IFrameSource

Added in version 6.16.0

Gives access to the built-in camera on .NET for Android. It implements the IFrameSource 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.

protected bool HasCameraPermission()
{
    return Build.VERSION.SdkInt < BuildVersionCodes.M ||
           CheckSelfPermission(CAMERA_PERMISSION) == Permission.Granted;
}

[TargetApi(@Value = (int)BuildVersionCodes.M)]
protected void TurnOnCamera()
{
    // For Android M and onwards the camera permission needs to be requested from the user.
    if (!HasCameraPermission())
    {
        RequestPermissions(new string[] { CAMERA_PERMISSION }, CAMERA_PERMISSION_REQUEST);
    }
    else
    {
        // The permission is already given or not needed.
        camera.SwitchToDesiredStateAsync(FrameSourceState.On);
    }
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
    if (requestCode == CAMERA_PERMISSION_REQUEST)
    {
        if (grantResults.Length > 0 && grantResults[0] == Permission.Granted)
        {
            TurnOnCamera();
        }
        else
        {
            // Show message about denied Camera permission.
        }
    }
    else
    {
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

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 Permission Documentation.

DesiredState
FrameSourceState DesiredState { get; }

Added in version 6.16.0

Implemented from IFrameSource. See IFrameSource.DesiredState.

CurrentState
FrameSourceState CurrentState { get; }

Added in version 6.16.0

Implemented from IFrameSource. See IFrameSource.CurrentState.

Position
CameraPosition Position { get; }

Added in version 6.16.0

The direction that the camera faces.

AddListener()
void AddListener(IFrameSourceListener listener)

Added in version 6.16.0

Implemented from IFrameSource. See IFrameSource.AddListener().

RemoveListener()
void RemoveListener(IFrameSourceListener listener)

Added in version 6.16.0

Implemented from IFrameSource. See IFrameSource.RemoveListener().

GetDefaultCamera()
static Camera GetDefaultCamera()

Added in version 6.16.0

Gets the default camera of the device. This method is identical to calling GetCamera() repeatedly, first with CameraPosition.WorldFacing, then with CameraPosition.UserFacing 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()
static Camera GetDefaultCamera(CameraSettings settings)

Added in version 6.16.0

This method is identical to calling GetDefaultCamera() and ApplySettingsAsync() consecutively.

GetCamera()
static Camera GetCamera(CameraPosition position)

Added in version 6.16.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 IFrameSource.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 IFrameSource.CurrentState will be FrameSourceState.On. The only guarantee about the state and settings is that when instance is initially created, the IFrameSource.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 ApplySettingsAsync() 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.

GetCamera()
static Camera GetCamera(CameraPosition position, CameraSettings settings)

Added in version 6.16.0

This method is identical to calling GetCamera() and then ApplySettingsAsync() consecutively.

SwitchToDesiredStateAsync()
Task<bool> SwitchToDesiredStateAsync(FrameSourceState state)

Added in version 6.16.0

Implemented from IFrameSource. See IFrameSource.SwitchToDesiredStateAsync().

ApplySettingsAsync()
Task ApplySettingsAsync(CameraSettings settings)

Added in version 6.16.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.

TorchAvailable
bool TorchAvailable { get; }

Added in version 6.16.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 DesiredTorchState { get;set; }

Added in version 6.16.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 TorchAvailable), this call has no effect.

AddTorchListener()
void AddTorchListener(ITorchListener listener)

Added in version 6.16.0

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

RemoveTorchListener()
void RemoveTorchListener(ITorchListener listener)

Added in version 6.16.0

Remove a listener for TorchState changes.

FrameOutput
event EventHandler<FrameOutputEventArgs> FrameOutput

Added in version 6.16.0

Occurs when a new frame is available. Consumers of this frame source can subscribe to this event to receive the frames produced by the frame source.

StateChanged
event EventHandler<FrameSourceStateChangedEventArgs> StateChanged

Added in version 6.16.0

Occurs when frame source state changed.

TorchStateChanged
event EventHandler<TorchEventArgs> TorchStateChanged

Added in version 6.16.0

Occurs when torch state changed.