Integrate Document Scan SDK for Android

Prerequisites: Make sure you have followed the instructions for installing the SDK.

Scan Documents

Note: To achieve best capture results, ensure you are in a well lit environment, place the document against a contrasting background, and position the capture device parallel with the document.

Document capture sessions are managed by a DSHandler. For every capture session, you must cover 3 steps:

  • Initialize an instance of DSHandler with a DSHandlerListener to respond to capture events. You will also provide the activityResultRegistry and the current Activity instance.

  • Prepare a set of DSOptions to configure the capture session.

  • Start a capture session with the DSHandler instance and the configured options.

Copy
private fun scanIdDocument(options: DSOptions) {
    val dsHandler = DSHandler.getInstance(this)
    
    // Provide the activity that will be used to start the capture session
    dsHandler.init(activityResultRegistry, this@MainActivity, object : DSHandlerListener{
    
        // Respond to one or more capture events
        override fun scanWasCancelled() {
            Log.w(TAG, "Document Scan canceled by user.")
            Toast.makeText(this@MainActivity,"Document Scan canceled by user.", Toast.LENGTH_LONG).show()
        }
    
        override fun captureError(error: DSError) {
            Log.e(TAG, "Document Scan Error: ${error.description}")
    
            when (error.errorType) {
                DSError.ErrorType.LicenseKeyExpiredOrInvalid -> {
                    Toast.makeText(this@MainActivity, "License key invalid.", Toast.LENGTH_LONG).show()
                }
                DSError.ErrorType.CameraPermissionNotGranted -> {
                    Toast.makeText(this@MainActivity, "Camera permission not granted.",Toast.LENGTH_LONG).show()
                }
                DSError.ErrorType.CouldNotInitiateCameraSession -> {
                    Toast.makeText(this@MainActivity, error.message, Toast.LENGTH_LONG).show()
                }
            }
        }
    
        override fun handleScan(result: DSResult) {
            handleResult(result)
        }
    })
    
    dsHandler.start(options)
}

Document capture options are highly configurable, but every capture is broken down into one of two categories:

  • Card Documents (ID1): Card-shaped documents, such as drivers licenses and passport cards, are referred to as ID1 Documents.

    To start an ID1 capture session, provide a DSID1Options to your DSHandler:

    Copy
    private fun getID1LicenseCaptureSettings(mSide: DSSide): DSID1Options {
        return DSID1Options().apply {
            imageCompressionQuality = 0.5 // Default is 0.3
            autoCaptureTimeoutDuration = 10 // Default is 30 seconds
            captureMode = DSCaptureMode.Auto // Default is DSCaptureMode.Manual
            enableFlashCapture = FlashCapture.None // Default is FlashCapture.Front
            
            // For ID1 documents,
            // set the type and side
            type = DSID1Type.License // Default is DSID1Type.License. Can set to DSID1Type.PassportCard
            side = mSide // Default is DSSide.Front
        }
    }
  • Passport Documents (ID3): Passport-shaped documents are referred to as ID3 Documents.

    To start an ID3 capture session, provide a DSPassportOptions to your DSHandler:

    Copy
    private fun getID3PassportCaptureSettings(): DSPassportOptions {
        return DSPassportOptions().apply {
            imageCompressionQuality = 0.5 // Default is 0.3
            autoCaptureTimeoutDuration = 10 // Default is 30 seconds
            captureMode = DSCaptureMode.Auto // Default is DSCaptureMode.Manual
            enableFlashCapture = FlashCapture.None // Default is FlashCapture.Front
        }
    }