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.
- 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
andCameraReader.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.
- 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.camera.CameraBarcodeScanView
android:id="@+id/camera_scan_view"
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="fillAvailableSpace"
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: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.