1. Kotlin
  2. Single Barcode Scanning

Kotlin

Single Barcode Scanning

Single barcode scanning allows you to detect and process one barcode at a time within the scanning window. This ensures precise detection by limiting the scan area, improving accuracy and performance.

⚙️ Configure the SDK

To enable single barcode scanning, you need to configure the Vision SDK using the configure method:

        visionCameraView.configure(
    detectionMode: DetectionMode,
    scanningMode: ScanningMode,
    isMultipleScanEnabled = false
)

      

Select the camera lens you want to use for barcode scanning:

        visionCameraView.setCameraSettings(
  CameraSettings(cameraLensFace: CameraLensFace.Back)
)

      

🤖 Auto Mode Barcode Scanning

For automatic barcode capture, set the scanningMode to ScanningMode.Auto:

        scanningMode = ScanningMode.Auto

      

Configuration example:

        visionCameraView.configure(
    detectionMode: DetectionMode,
    scanningMode = ScanningMode.Auto,
    isMultipleScanEnabled = false
)

      

✋ Manual Mode Barcode Scanning

For manual barcode capture, set the scanningMode to ScanningMode.Manual:

        scanningMode = ScanningMode.Manual

      

Configuration example:

        visionCameraView.configure(
    detectionMode: DetectionMode,
    scanningMode = ScanningMode.Manual,
    isMultipleScanEnabled = false
)

      

Afterwards, when you want to capture, call the following method:

        visionCameraView.capture()

      

📦 Callback Method for Barcode Values

When a barcode is successfully scanned, the following callback method is called:

        private fun startSingleScanning() {

    visionCameraView.setScannerCallback(object : ScannerCallbackAdapter() {

      override fun onScanResult(barcodeList: List<ScannedCodeResult>) {

      }
    })

    visionCameraView.startCamera()
}

      

Note that, once onScanResult or onImageCaptured callbacks are called, VisionSDK will stop analyzing camera feed for text or barcodes/QR codes. This is to prevent extra processing and battery consumption. When client wants to start analyzing camera feed again, after consuming the results of previous scan, client needs to call the following function:

        visionCameraView.rescan()

      

❗️Error Handling Callback

If an error occurs during the barcode scanning process, the following callback method will be triggered. You can implement this to handle any failures during scanning, gracefully.

        private fun startSingleScanning() {

    visionCameraView.setScannerCallback(object : ScannerCallbackAdapter() {

      override fun onFailure(exception: VisionSDKException) {

      }
    })

    visionCameraView.startCamera()
}

      

Use this method to log errors, display messages to the user, or perform recovery actions when scanning fails.

🧾 Barcode Object Structure

The ScannedCodeResult object returned in the callback contains the following properties:

        data class ScannedCodeResult(
    val scannedCode: String,
    val symbology: BarcodeSymbology, // UPC, Code 128 etc
    val gs1ExtractedInfo: Map<String, String>? = null
)

      

🧭 Detection Indicators

To receive detection indicators (without actual values), implement the following callback method:

        private fun startScanning() {

    visionCameraView.setScannerCallback(object : ScannerCallbackAdapter() {

      override fun onIndications(barcodeDetected: Boolean, qrCodeDetected: Boolean, textDetected: Boolean, documentDetected: Boolean) {

      }
    })

    visionCameraView.startCamera()
}

      
  • textDetected: Indicates presence of text
  • barcodeDetected: Indicates presence of a barcode
  • qrCodeDetected: Indicates presence of a QR code
  • documentDetected: Indicates presence of a document

🔘 Enable Detection Indicators

You can toggle detection indicators as follows:

        visionCameraView.setObjectDetectionConfiguration(
   ObjectDetectionConfiguration(
      isTextIndicationOn = true,
      isBarcodeOrQRCodeIndicationOn = true,
      isDocumentIndicationOn = true,
      secondsToWaitBeforeDocumentCapture = 3
   )
)

      

🎯 Focus Area Configuration

In single barcode scanning, you can also define the scan region. A scan region is a rectangular area inside VisionCameraView. If you enable focus area, only the barcodes inside that rectangular area will be sent in onScanResult. To define focus area, use the object of FocusRegionManager that you can get after camera is started:

        private fun startScanning() {

    visionCameraView.setCameraLifecycleCallback(object : CameraLifecycleCallback {

      override fun onCameraStarted() {
          // If you want to apply Focus Settings, you can do that here, after camera is started.
          visionCameraView
            .getFocusRegionManager()
            .setFocusSettings(
                focusSettings = FocusSettings(
                    context: Context,
                    focusImage: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.default_focus_frame),
                    focusImageRect: RectF = RectF(0.0F, 0.0F, 0.0F, 0.0F),
                    shouldDisplayFocusImage: Boolean = false,
                    shouldScanInFocusImageRect: Boolean = false,
                    focusImageTintColor: Int = Color.WHITE,
                    focusImageHighlightedColor: Int = Color.WHITE,
                    showCodeBoundariesInMultipleScan: Boolean = true,
                    validCodeBoundaryBorderColor: Int = Color.GREEN,
                    validCodeBoundaryBorderWidth: Int = 1,
                    validCodeBoundaryFillColor: Int = Color.argb(76, 0, 255, 0), // Green color with 30% alpha value
                    invalidCodeBoundaryBorderColor: Int = Color.RED,
                    invalidCodeBoundaryBorderWidth: Int = 1,
                    invalidCodeBoundaryFillColor: Int = Color.argb(76, 255, 0, 0), // Red color with 30% alpha value
                    showDocumentBoundaries: Boolean = true,
                    documentBoundaryBorderColor: Int = Color.YELLOW,
                    documentBoundaryFillColor: Int = Color.argb(76, 255, 255, 0),
                )
            )
      }
      override fun onCameraStopped() = Unit
    })

    visionCameraView.startCamera()
}

      

For assistance of scanning region visually during development or debugging:

shouldDisplayFocusImage = true

🧪 Sample Code

To start scanning for barcodes, QR codes, text or documents, use the startCamera method. See the code below for example:

        private fun startSingleScanning() {

    visionCameraView.configure(
      detectionMode = DetectionMode.BarcodeOrQRCode,
      scanningMode = ScanningMode.Auto,
      isMultipleScanEnabled = false
    )

    visionCameraView.setScannerCallback(object : ScannerCallbackAdapter() {

      fun onIndications(barcodeDetected: Boolean, qrCodeDetected: Boolean, textDetected: Boolean, documentDetected: Boolean) {

      }

      fun onCodesScanned(barcodeList: List<ScannedCodeResult>) {

      }

      fun onFailure(exception: VisionSDKException) {

      }
    })

    visionCameraView.startCamera()
}