1. Vision SDK
  2. Android

Vision SDK

Android

Barcode and QR Code scanner framework for Android. VisionSDK provides a way to detect barcodes and qr codes. It also provides the functionality for information extraction from different kind of logistic labels like shipping labels, inventory labels, bill of ladings, receipts & invoices

Installation

Vision SDK is hosted on JitPack.io

First add JitPack to your root project

        maven { url "https://jitpack.io" }

      

Then add the following dependency to your project's build.gradle file:

        implementation 'com.github.packagexlabs:vision-sdk-android:v1.0'

      

Usage

Initialization

In order to use the OCR API for information extraction, you have to set following parameters

  • Constants.apiKey to your API key.
  • Constants.apiEnvironment you also need to specify the API environment that you have the API key for (sandbox or production).
NOTE

Please note that these have to be set before using the API call. You can generate your own API key at cloud.packagex.io. You can find the instruction guide here.

Initialize the SDK first:

        VisionSDK.getInstance().initialise(
    authentication = //TODO authentication,
    environment = //TODO environment
)

      

Basic Usage

You can find the sample code for using the vision-sdk on our github.

To start scanning for barcodes and QR codes, use the startScanning method and specify the view type:

        private fun startScanning() {

    //setting the scanning window configuration
    binding.customScannerView.startScanning(
        viewType = screenState.scanningWindow,
        scanningMode = screenState.scanningMode,
        detectionMode = screenState.detectionMode,
        scannerCallbacks = this
    )
}

      

Configuration

  • ViewType There are 2 types of views.
    • .WINDOW by default show a square window for QR detection mode and rectangle for Barcode
    • .FULLSCREEN whole screen

To customize the appearance and behavior of the scanning window, you can use the setScanningWindowConfiguration method and provide a configuration object with your desired settings.

For example: As ViewType.RECTANGLE and ViewType.SQUARE have a window, you can configure the scanning window according to yours requirements. There is also option for setting the scanning window radius along with vertical starting point.

        //Setting the Barcode and QR code scanning window sizes
binding.customScannerView.setScanningWindowConfiguration(
  Configuration(
      barcodeWindow = ScanWindow(
          width = ((binding.root.width * 0.9).toFloat()),
          height = ((binding.root.width * 0.4).toFloat()),
          radius = 10f,
          verticalStartingPosition = (binding.root.height / 2) - ((binding.root.width * 0.4).toFloat())
      ), qrCodeWindow = ScanWindow(
          width = ((binding.root.width * 0.7).toFloat()),
          height = ((binding.root.width * 0.7).toFloat()),
          radius = 10f,

          //you can set the vertical position of the scanning window
          verticalStartingPosition = (binding.root.height / 2) - ((binding.root.width * 0.5).toFloat())
      )
  )
)

      
  • scanningMode There are 2 types of scanning mode
    • Auto mode will auto-detect any Barcode or QR code based on the detection mode
    • Manual mode will detect Barcode or QR code upon calling Capture
  • detectionMode Detection mode will tell which codes to detect
    • QrAndBarcode detects Barcode and QR Codes collectively
    • Barcode detects only barcode
    • QR detects only QR codes
    • OCR for OCR detection. This mode will call OCR API

Delegate Methods / Callbacks

There is also Scanner Callback that we need to provide while starting scanning. This is an interface, and it will be giving different callbacks based on the detection mode.

  • onBarcodeDetected whenever a Barcode or QR code is detected in Single model
  • onImageCaptured whenever image is capture in OCR mode
  • onMultipleBarcodesDetected when multiple barcodes are detected in multiple mode
  • onFailure when some exception is thrown or unable to detect any Barcode/QRCode in manual mode

Listening to the Barcode and Text Detection

For the barcode and indicator there are different live data that you can observe

  • customScannerView.barcodeIndicators will post a new value whenever a new barcode or QR code is detected. To distinguish between Barcode and QR Code you can use the format field e.g TWO_DIMENSIONAL_FORMATS.contains(it.format)
  • customScannerView.textIndicator will post a new value whenever a text is detected on the screen

Capture Image

As mentioned above that we have a manual mode. For manual mode trigger, you can call customScannerView.capture(), based on the mode it will be giving different callbacks

if detection mode is

  • DetectionMode.Barcode then it will trigger the onBarcodeDetected callback in case of barcode detection or throw an exception QRCodeNotDetected if barcode not detected with in specific time frame.
  • DetectionMode.QR will return QR code or throw Exception of BarCodeNotDetected
  • DetectionMode.OCR will capture an image along with the current barcode and return in onImageCaptured
WARNING

Make sure that when calling capture, scan mode should be manual

Capturing Image For OCR

You can capture an image when mode is OCR. In OCR mode when capture is called, then in the callback, it will return an image and the barcodes/qrcodes in that particular image.

        customScannerView.captureImage()

      

Callback

In the callback, it will return the image bitmap along with the barcodes list in the current frame.

        fun onImageCaptured(bitmap: Bitmap, value: MutableList<Barcode>?) {
    //Image along with the barcodes
}

      

OCR Method

For the OCR Api call, you need to set the Environment, there are multiple environment(sandbox, production), plus the Api key. You can call makeOCRApiCall for the ocr analysis on a bitmap. Bitmap and barcodes needs to be provided. If there are no barcodes, then provide an empty list.

Below is an example:

        customScannerView.makeOCRApiCall(
    bitmap = bitmap,
    barcodeList = list,
    onScanResult = object : OCRResult {
        override fun onOCRResponse(ocrResponse: OCRResponse?) {
            //Successful result
        }

        override fun onOCRResponseFailed(throwable: Throwable?) {
            //Some issue occurred
        }
    })

      

In the callbacks, success or error response will be returned. It returns with the OCR Response from PackageX Platform API Response.