Read from camera
This page is for Foundational APIs only. Refer to Quick Start for how to scan from camera with
BarcodeScannercomponent.
Follow these three steps to read barcodes from the camera:
- Set input
- Register result receiver
- Start capturing
Set input
CameraEnhancer - Dynamsoft Standard Camera Input
-
Add
CameraViewto your layout.<com.dynamsoft.dce.CameraView android:id="@+id/camera_view" android:layout_width="match_parent" android:layout_height="match_parent"/> -
Create a
CameraEnhancerobject and bind it toCameraView.- Java
- Kotlin
-
CameraEnhancer mCamera; CameraView cameraView = findViewById(R.id.camera_view); mCamera = new CameraEnhancer(cameraView, this); -
val cameraView: CameraView = findViewById(R.id.camera_view) val mCamera = CameraEnhancer(cameraView, this)
-
Use
CaptureVisionRouter.setInputto set theCameraEnhancerobject as the input source.- Java
- Kotlin
-
CaptureVisionRouter mRouter; mRouter = new CaptureVisionRouter(); try { mRouter.setInput(mCamera); } catch (CaptureVisionRouterException e) { throw new RuntimeException(e); } -
val mRouter = CaptureVisionRouter() try { mRouter.setInput(mCamera) } catch (e: CaptureVisionRouterException) { throw RuntimeException(e) }
Other Camera Input
Note: If you are using CameraX, refer to the
DecodeWithCameraXsample for camera input integration.
To use another camera source, complete the following steps so the library can process the input:
- Receive the camera input.
- Convert the raw camera input to a
com.dynamsoft.core.basic_structures.ImageDataobject. - Use
addImageToBufferto add theImageDataobject to the buffer.
Result receiver
Use CapturedResultReceiver to receive capture results. The callback is triggered each time an image is processed, regardless of whether a barcode is decoded.
- Java
- Kotlin
mRouter.addResultReceiver(new CapturedResultReceiver() { @Override public void onDecodedBarcodesReceived(@NonNull DecodedBarcodesResult result) { } });mRouter.addResultReceiver(object : CapturedResultReceiver { override fun onDecodedBarcodesReceived(result: DecodedBarcodesResult) { } })
Start capturing
Use startCapturing and stopCapturing to control when barcode decoding starts and stops.
- Java
- Kotlin
mRouter.startCapturing("ReadBarcodes_Default", new CompletionListener() { @Override public void onSuccess() { } @Override public void onFailure(int errorCode, String errorString) { } });mRouter.startCapturing("ReadBarcodes_Default", object : CompletionListener { override fun onSuccess() { } override fun onFailure(errorCode: Int, errorString: String?) { } })