1. Kotlin
  2. Capturing an Image for OCR

Kotlin

Capturing an Image for OCR

Vision SDK provides a simple way to capture images for further processing like OCR or just for saving in storage. All you need to do is follow the setup steps below.

Capture Image

Using the following code, you can get image as a bitmap in onImageCaptured callback, along with a list of ScannedCodeResult. The list will contain any barcodes that were available in that bitmap. The list could be empty, if there were no barcodes found.

        private fun startCamera() {

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

  visionCameraView.setScannerCallback(object : ScannerCallbackAdapter() {

    override fun onImageCaptured(bitmap: Bitmap, scannedCodeResults: List<ScannedCodeResult>) {
        processCaptureImage(bitmap, scannedCodeResults)
    }
  })

  visionCameraView.setCameraLifecycleCallback(object : CameraLifecycleCallback {

      override fun onCameraStarted() {
        shutterButton.setOnClickListener {
          visionCameraView.capture()
        }
      }
      
      override fun onCameraStopped() {}
  })

  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()