4.5. The camera scanner

enioka Scan can still be used to manage barcodes scanned using the Android camera, through com.enioka.scanner.sdk.camera, (check library dependencies and compatibility matrix). For that, it exposes the CameraBarcodeScanView and a series of helper classes to handle compatiblity with both Camera 1 and Camera 2 hardware APIs.

This page regroups all information needed to control this special scanner at a deeper level.

4.5.1. The CameraBarcodeScanView class

This is the base Android FrameLayout used by the activity to display the camera. This view wraps the API-specific CameraBarcodeScanViewBase implementation and handles the API selection based on the device’s properties.

For most users, this is the class they will interact with if they need to interact with the Camera hardware and not just the Scanner instance.

setPreviewRatioMode(AspectRatioMode mode) void

Change the preview aspect ratio mode of the camera. This change will not take effect until the next view refresh. You can force a refresh by pausing and resuming the camera.

Parameters:

mode (AspectRatioMode) – The mode to use for filling the camera preview. Can be one of the following values:

  • 0: fillWithCrop: The preview will be scaled to fit the picture, cropping the sides if needed. The aspect ratio of the preview will be kept.

  • 1: fillWithBlackBars: The preview will be scaled to fit the picture, with black bars on the sides if needed. The aspect ratio of the preview will be kept.

  • 2: fillWithStretch: The preview will fill the available space, by squashing or stretching the preview if needed. The aspect ratio of the preview can be altered.

Example of a preview with the fillWithCrop mode:

Example of a preview with the `fillWithCrop` mode.

Example of a preview with the fillWithBlackBars mode:

Example of a preview with the `fillWithBlackBars` mode.

Example of a preview with the fillWithStretch mode:

Example of a preview with the `fillWithStretch` mode.
setReaderMode(CameraReader readerMode) void

Change the library used to read barcodes from the camera feed.

Parameters:

readerMode (CameraReader) – The library to use, between CameraReader.ZBAR and CameraReader.ZXing

addSymbology(BarcodeType barcodeType) void

Add a symbology to detect. By default, only CODE_128 is used.

Parameters:

barcodeType (BarcodeType) – The symbology to add.

setResultHandler(ResultHandler handler) void

Change the callback used when a barcode is read.

Used by CameraBarcodeScanViewScanner to register itself with the view and correctly propagate barcodes read by the camera as any regular scanner reads.

Parameters:

handler (ResultHandler) – the ResultHandler implementation

setTorch(boolean value) void

Switch the camera’s torch on or off.

Parameters:

value (boolean) – Indicate if the torch should be turned on (true) or off (false)

getSupportTorch() boolean
Returns:

true if the camera supports torch activation. false otherwise.

getTorchOn() boolean
Returns:

true if the camera’s torch is on, false otherwise.

cleanUp() void

Unhooks all camera callbacks and closes it. After this method is called, the camera becomes unusable and the view needs to be reinitialized.

pauseCamera() void

Pauses the camera’s capture.

resumeCamera() void

Resumes the camera’s capture.

resetTargetPosition() void

Reset the vertical position of the target. Also resets the associated preferences.

orientationChanged() void

Notifies the view that the orientation of the device has changed by calling setDisplayOrientation() with the correct clockwise rotation of the camera preview, depending on the device’s orientation.

setTargetPosition(float y) void

Set the top target’s vertical position on the preview (y coordinate).

setTargetDimension(float width, float height) void

Set the target’s dimensions on the preview.

getLatestSuccessfulScanJpeg() byte[]
Returns:

The JPEG data of the image used in the latest successful scan, or null if there is no previous scan data.

4.5.2. Use CameraBarcodeScanView in your custom layout

To use this view in your own layout, you can add the following block to its XML definition and adapt the attributes to your needs:

<com.enioka.scanner.sdk.camera.CameraBarcodeScanView
    android:id="@+id/cameraScanView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:forceCameraApiVersion="Auto"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:maxDistortionRatio="0.3"
    app:minResolutionY="720"
    app:maxResolutionY="1080"
    app:previewRatioMode="fillWithCrop"
    app:readerMode="Auto"
    app:storePreferredResolution="false"
    app:targetColorActive="@color/colorRed"
    app:targetColorPaused="@color/defaultItemColor"
    app:targetIsFixed="false"
    app:targetStrokeWidth="5"
    app:useAdaptiveResolution="true" />

The main XML attributes are as follow:

app:forceCameraApiVersion

“1” for the old camera API, “2” for the Camera2 API, any other value to let the library detect the appropriate camera API for this device.

app:useAdaptiveResolution

If true, the resolution of the camera preview may be decreased if performances are deemed too low by the frame analyzer.

app:minResolutionY

The minimum vertical resolution of the camera preview, after which resolution can no longer be decreased by adaptive resolution when trying to improve performance.

app:maxResolutionY

The maximum vertical resolution of the camera preview, useful to limit performance costs.

app:previewRatioMode

The mode to use for filling the camera preview. Can be one of the following values:

  • 0: fillWithCrop: The preview will be scaled to fit the picture, cropping the sides if needed. The aspect ratio of the preview will be kept.

  • 1: fillWithBlackBars: The preview will be scaled to fit the picture, with black bars on the sides if needed. The aspect ratio of the preview will be kept.

  • 2: fillWithStretch: The preview will fill the available space, by squashing or stretching the preview if needed. The aspect ratio of the preview can be altered.

If the value of this attribute is not recognized or defined, the preview ratio mode will be set by default to fillWithCrop.

app:readerMode

“1” for ZXING, any other value for ZBAR.

app:storePreferredResolution

If true, the app will persists the most used preview resolution to the application’s preference.

app:targetColorActive

The default color of the “target” indicating where barcodes are expected to be.

app:targetColorPaused

The color of the “target” indicating where barcodes are expected to be, used whenever the scanning is paused.

app:targetIsFixed

If true, the target cannot be moved by the user. If false, the user can drag the target up and down on the preview.

app:targetStrokeWidth

The thickness of the target’s lines.

4.5.3. The CameraScannerProvider API

The CameraScannerProvider is the entry point to the camera scanner API. It is used to create and control the camera scanner service, and to access the camera scanner view. As the camera scanner is part of the com.enioka.scanner.sdk.camera package, it is not directly accessible from the main library.

4.5.3.1. The CameraScannerProvider interface

getIdResource() HashMap<String, Integer>
Returns:

A map containing the IDs of the views used by the camera scanner. May be replaced with your own.

It contains the following values:

  • layout_id_camera: The ID of the layout containing the camera view. Set to R.layout.activity_main_alt.

  • camera_view_id: The ID of the CameraBarcodeScanView inside the layout_id_camera layout. Set to R.id.cameraScanView.

  • scanner_toggle_view_id: The ID of the optional ImageButton on which to press to toggle the zxing/zbar camera scan library. Set to R.id.scannerSwitchZxing.

  • scanner_toggle_pause_id: The ID of the optional toggle button on which to press to pause/unpause the scanner. Set to R.id.scannerSwitchPause.

  • card_last_scan_id: The ID of the card view that displays the last scan. Set to R.id.cardCameraLastScan.

  • constraint_layout_id: The ID of the constraint layout inside the camera layout. Set to R.id.constraintLayoutMainActivity.

  • scanner_flashlight_id: The ID of the optional ImageButton on which to press to toggle the flashlight/illumination. Set to R.id.scannerFlashlight.

  • scanner_bt_provider_logs: The ID of the optional ImageButton on which to press to manually access available providers logs. Set to R.id.scannerBtProviderLogs.

getCameraScanner(Context ctx, ScannerConnectionHandler handler, ScannerSearchOptions options) void

Called to initialize the camera scanner with the given view and callbacks.

isCameraScannerInitialized() boolean
Returns:

true if the camera scanner is initialized, false otherwise.

reset() void

Resets the camera scanner.

toggleIllumination() void

Toggles the camera’s illumination.

disconnect() void

Disconnects the camera scanner.

pauseCamera() void

Pauses the camera scanner.

resumeCamera() void

Resumes the camera scanner.

isIlluminationOn() boolean
Returns:

true if the camera’s illumination is on, false otherwise.

setPreviewRatioMode(View cameraView, int previewRatioMode) void

See setPreviewRatioMode for more information.

setReaderMode(View cameraView, boolean readerMode) void
Parameters:
  • cameraView (View) – The camera view to change the reader mode of.

  • readerMode (boolean) – false for ZBar, true for ZXing.

Change the library used to read barcodes from the camera feed.

orientationChanged(View cameraView) void
Parameters:

cameraView (View) – The camera view to notify of the orientation change.

Notifies the view that the orientation of the device has changed by calling setDisplayOrientation() with the correct clockwise rotation of the camera preview, depending on the device’s orientation.

setTargetPosition(View cameraView, float y) void
Parameters:
  • cameraView (View) – The camera view to change the target position of.

  • y (float) – The top target’s vertical position on the preview (y coordinate).

Set the top target’s vertical position on the preview.

setTargetDimension(View cameraView, float width, float height) void
Parameters:
  • cameraView (View) – The camera view to change the target dimensions of.

  • width (float) – The target’s width.

  • height (float) – The target’s height.

Set the target’s dimensions on the preview.