Table of contents
Swift
Objective-C

Read from camera

This page is for Foundational APIs only. Refer to Quick Start for how to scan from camera with BarcodeScanner component.

Follow these three steps to read barcodes from the camera:

  1. Set input
  2. Register result receiver
  3. Start capturing

Set input

CameraEnhancer - Dynamsoft Standard Camera Input

  1. Add CameraView to your layout.

    <com.dynamsoft.dce.CameraView
        android:id="@+id/camera_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
  2. Create a CameraEnhancer object and bind it to CameraView.

    • Java
    • Kotlin
    1. CameraEnhancer mCamera;
      CameraView cameraView = findViewById(R.id.camera_view);
      mCamera = new CameraEnhancer(cameraView, this);
      
    2. val cameraView: CameraView = findViewById(R.id.camera_view)
      val mCamera = CameraEnhancer(cameraView, this)
      
  3. Use CaptureVisionRouter.setInput to set the CameraEnhancer object as the input source.

    • Java
    • Kotlin
    1. CaptureVisionRouter mRouter;
      mRouter = new CaptureVisionRouter();
      try {
         mRouter.setInput(mCamera);
      } catch (CaptureVisionRouterException e) {
         throw new RuntimeException(e);
      }
      
    2. 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 DecodeWithCameraX sample for camera input integration.

To use another camera source, complete the following steps so the library can process the input:

  1. Receive the camera input.
  2. Convert the raw camera input to a com.dynamsoft.core.basic_structures.ImageData object.
  3. Use addImageToBuffer to add the ImageData object 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
  1. mRouter.addResultReceiver(new CapturedResultReceiver() {
       @Override
       public void onDecodedBarcodesReceived(@NonNull DecodedBarcodesResult result) {
       }
    });
    
  2. 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
  1. mRouter.startCapturing("ReadBarcodes_Default", new CompletionListener() {
       @Override
       public void onSuccess() {
       }
       @Override
       public void onFailure(int errorCode, String errorString) {
       }
    });
    
  2. mRouter.startCapturing("ReadBarcodes_Default", object : CompletionListener {
       override fun onSuccess() {
       }
       override fun onFailure(errorCode: Int, errorString: String?) {
       }
    })
    

This page is compatible for: