Skip to main content

Using ID Card Scanner | Android Document Scanner

The Scanbot SDK provides the ability to scan ID Cards of supported types, crop detected card and recognize fields data. As a result of scanning, the user gets the IdScanResult class instance, which contains a cropped card image and a list of fields data. Each field is represented by the IdScanResult.Field class, holding field's type, cropped visual source, recognized text and confidence level value.

Currently types are listed in io.scanbot.sdk.idcardscanner.IdScanResult.DocumentType enum:

  • DeIdBack - German ID card, back side
  • DeIdFront - German ID card, front side
  • DePassport - German passport

Try our ID Card Scanner Example For Live Detection, ID Card Scanner Example For Auto Snapping or check the following step by step integration instructions.

Step 1 - Add ID Card Scanner Feature as a Dependency#

IdCardScanner is available with the SDK Package 3. You have to add the following dependencies for it:

api "io.scanbot:sdk-package-3:$latestSdkVersion"api "io.scanbot:sdk-idcard-assets:$latestSdkVersion"

It can be used both in conjunction with ScanbotCameraView (e.g. live detection for preview) and by itself for detection on a Bitmap or JPEG byte array.

Step 2 - Add desired blobs prefetching to SDK config#

The ID Card Scanner is based on the OCR feature of Scanbot SDK. Please check the Optical Character Recognition docs for more details.

In order to use the ID Card Scanner you need to prepare the German and English OCR language files. Place the deu.traineddata and eng.traineddata files in the assets sub-folder assets/ocr_blobs/ of your app.

Add a call to .prepareOCRLanguagesBlobs(true) methods for ScanbotSDKInitializer in your Application class:

override fun onCreate() {    super.onCreate()
    ScanbotSDKInitializer()            .license(this, licenseKey)            // TODO: other configuration calls            .prepareOCRLanguagesBlobs(true)            .initialize(this)}

Step 3 - Add ScanbotCameraView to layout#

<io.scanbot.sdk.camera.ScanbotCameraView    android:id="@+id/camera_view"    android:layout_width="match_parent"    android:layout_height="match_parent" />

Step 4 - get IdCardScanner instance from ScanbotSDK, set the needed blurriness acceptance score and attach it to ScanbotCameraView#

// Please note that each call to this method will create a new instance of IdCardScanner// It should be used on a "single instance per screen" basisval idCardScanner = scanbotSdk.createIdCardScanner()idCardScanner.acceptedSharpnessScore = 80fval idCardScannerFrameHandler = IdCardScannerFrameHandler.attach(cameraView, idCardScanner)

Step 5 - Add result handler for IdCardScannerFrameHandler:#

Add frame handler which, for example, observes consecutive successful recognition statuses and shows toast whenever two or more of such statuses are received.

idCardScannerFrameHandler.addResultHandler(object : IdCardScannerFrameHandler.ResultHandler {    private var successCounter = 0
    override fun handle(result: FrameHandlerResult<IdScanResult, SdkLicenseError>): Boolean {        val isSuccess = result is FrameHandlerResult.Success        when {            isSuccess && successCounter >= 2 -> {                // NOTE: 'handle' method runs in background thread - don't forget to switch to main before touching any Views                runOnUiThread {                    Toast.makeText(                        this@MainActivity,                        "ID Card found!\nYou can now snap picture.",                        Toast.LENGTH_SHORT                    ).show()                }            }            isSuccess -> successCounter++            else -> successCounter = 0        }        return false    }})

Step 6 - Pass snapped picture to IdCardScanner, process results#

First, decode image ByteArray obtained from camera's callback, taking into account image orientation. For this our ImageProcessor component can be used:

val imageProcessor = ScanbotSDK(this).imageProcessor()val resultBitmap = imageProcessor.processJpeg(image, listOf(RotateOperation(imageOrientation)))

Next, we perform a scan:

val idCardScanResults = idCardScanner.scanBitmap(resultBitmap)

As an example of further application, set obtained scan parameters to a TextView:

val myTextView = findViewById<TextView>(R.id.my_text_view)
val resultsMessage = "ID Card scanning results:\n" +    "Scan status: ${idCardScanResults.status}\n" +    "Card type: ${idCardScanResults.documentType}\n" +    "Number of fields scanned: ${idCardScanResults.fields.size}"
myTextView.text = resultsMessage