Skip to main content

Quick start | Android Document Scanner

In this section, you'll learn how to set up the Scanbot Document Scanner SDK for Android in your app, with Document Scanning enabled, all within 10 minutes – thanks to our Ready-to-Use UI!

Add Scanbot SDK dependencies

The Scanbot SDK for Android is distributed through our private Maven repository server (nexus.scanbot.io), which needs to be specified in the settings.gradle.kts file in the root folder of your project:

// settings.gradle.kts in the root of the project:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()

// Add Scanbot SDK maven repositories here:
maven(url = "https://nexus.scanbot.io/nexus/content/repositories/releases/")
maven(url = "https://nexus.scanbot.io/nexus/content/repositories/snapshots/")
}
}

Afterward, the dependencies can be added in the dependencies section of your Android application project configuration, usually in the app/build.gradle.kts file:

// app/build.gradle.kts (dependencies section):
implementation("io.scanbot:sdk-package-1:$scanbotSdkVersion")
implementation("io.scanbot:rtu-ui-v2-bundle:$scanbotSdkVersion")
implementation("io.scanbot:sdk-multitasktext-assets:$scanbotSdkVersion")

To get the latest version of the Scanbot SDK, please always refer to the SDK's changelog.

For more details about the dependencies, please refer to our detailed installation guide.

Add camera permission

The Scanbot SDK needs access to the device camera so it can scan from a live camera stream. Therefore, the camera permission must be defined in the AndroidManifest.xml file:

<!-- AndroidManifest.xml: -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>

<!-- Add Camera permission here: -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

<application ...>

Note how we also added the uses-feature tag for better recognition of your app on the Google Play Store (see more).

Our Ready-to-Use UI Components handle the runtime permissions automatically, so there is no need to add anything else in the code.

Initialize the SDK

Usually, we recommend initializing the SDK in the Application class of your app (see our detailed setup page). However, in this quick start guide, we are going to implement the initialization in an Activity class.

Make sure to add the following imports to the top of the file:

// Add the following imports in your Activity (for example, MainActivity.kt):
import android.widget.Button
import android.widget.ImageView
import androidx.activity.result.ActivityResultLauncher
import io.scanbot.sdk.ScanbotSDKInitializer
import io.scanbot.sdk.docprocessing.Document
import io.scanbot.sdk.docprocessing.Page
import io.scanbot.sdk.ui_v2.common.activity.registerForActivityResultOk
import io.scanbot.sdk.ui_v2.document.DocumentScannerActivity
import io.scanbot.sdk.ui_v2.document.configuration.DocumentScanningFlow

To initialize the SDK, simply use the ScanbotSDKInitializer class in the onCreate method of your Activity:

// Adapt the 'onCreate' method in your Activity (for example, MainActivity.kt):
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
// Initialize the SDK here:
ScanbotSDKInitializer()
// optional: uncomment the next line if you have a license key
// .license(this.application, LICENSE_KEY)
.initialize(this.application)
}
info

You can use the Scanbot Document Scanner SDK for quick testing or evaluation purposes even without a license key. However, the SDK will only work for 60 seconds per app session and may not be used for production purposes. Want to scan longer than 60 seconds? Get your free trial license key here.

Start the Document Scanner and process the result

With just a few lines of code, the Scanbot Document Scanner Ready-To-Use UI component (DocumentScannerActivity) can be integrated into your application's workflow. It's as simple as starting any regular Android Activity.

First, you need to add the following variable to your Activity:

// Add the following variable in your Activity:
private lateinit var documentScreenLauncher: ActivityResultLauncher<DocumentScanningFlow>

In the onCreate method of your Activity we need to initialize the ActivityResultLauncher and define the callback that will be called when the Document Scanner UI is closed:

override fun onCreate(savedInstanceState: Bundle?) {
// ...
// After the super.onCreate(...) and ScanbotSDKInitializer call:

// We will use this image view to display the first page preview image, it should exist in your layout xml:
val previewImageView = findViewById<ImageView>(R.id.first_page_image_preview)

documentScreenLauncher =
registerForActivityResultOk(DocumentScannerActivity.ResultContract(this)) {
resultEntity: DocumentScannerActivity.Result ->
val result: Document? = resultEntity.result
val pages: List<Page>? = result?.pages

// Display the first page preview image:
pages?.get(0)?.let {
val previewImage = it.documentPreviewImage
previewImageView.setImageBitmap(previewImage)
}
}

// Set up a button to open the document scanner, it should exist in your layout xml:
findViewById<Button>(R.id.open_document_scanner).setOnClickListener {
openDocumentScannerRtuV2()
}
}

The result of the Document Scanner UI will be delivered to the callback you just defined.

Now, to launch the Document Scanner UI, you just call the documentScreenLauncher where needed. For example, in the setOnClickListener of your button:

// Launch the Document Scanner in your Activity:
private fun openDocumentScannerRtuV2() {
val configuration = DocumentScanningFlow().apply {
// Customize text resources, behavior and UI:
// ...
}

documentScreenLauncher.launch(configuration)
}

🚀 That's it! 🚀 You have successfully integrated a full-featured document scanner as an RTU UI Activity into your app.

Want to scan longer than one minute?

Generate a free trial license to test the Scanbot SDK thoroughly.

Get your free Trial License

What do you think of this documentation?