Quick start | Android Barcode Scanner
In this section, you'll learn how to set up the Scanbot Barcode Scanner SDK for Android in your app, with Barcode 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:scanbot-barcode-scanner-sdk:6.1.1")
implementation("io.scanbot:rtu-ui-v2-barcode:6.1.1")
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.Toast
import androidx.activity.result.ActivityResultLauncher
import io.scanbot.sdk.barcode_scanner.ScanbotBarcodeScannerSDKInitializer
import io.scanbot.sdk.ui_v2.barcode.BarcodeScannerActivity
import io.scanbot.sdk.ui_v2.barcode.configuration.BarcodeScannerConfiguration
import io.scanbot.sdk.ui_v2.common.activity.registerForActivityResultOk
To initialize the SDK, simply use the ScanbotBarcodeScannerSDKInitializer
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:
ScanbotBarcodeScannerSDKInitializer()
// optional: uncomment the next line if you have a license key
// .license(this.application, LICENSE_KEY)
.initialize(this.application)
}
You can use the Scanbot Barcode 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 Barcode Scanner and process the result
With just a few lines of code, the Scanbot Barcode Scanner Ready-To-Use UI component (BarcodeScannerActivity) can be integrated into your application's workflow. It's as simple as starting any regular Android Activity
.
First, you need to register the ActivityResultLauncher
in your Activity
class and save it as a variable:
// Add the following variable in your Activity:
private val barcodeScreenLauncher: ActivityResultLauncher<BarcodeScannerConfiguration> =
registerForActivityResultOk(BarcodeScannerActivity.ResultContract()) { resultEntity ->
// Barcode Scanner result callback:
// Get the first scanned barcode from the result object...
val barcodeItem = resultEntity.result?.items?.first()
// ... and process the result as needed, for example, display as a Toast:
Toast.makeText(
this,
"Scanned: ${barcodeItem?.text} (${barcodeItem?.type})",
Toast.LENGTH_LONG
).show()
}
The result of the Barcode Scanner UI will be delivered to the callback you just defined.
Now, to launch the Barcode Scanner UI, you just call the barcodeScreenLauncher
where needed.
For example, in the setOnClickListener
of your button:
// Launch the barcode scanner in your Activity:
barcodeScreenLauncher.launch(BarcodeScannerConfiguration())
🚀 That's it! 🚀 You have successfully integrated a full-featured barcode scanner as an RTU UI Activity
into your app.
💡 Customization: In this Quick Start guide, we use the default configuration for the scanner UI. Feel free to explore the configs and customize the UI and behavior according to your needs via the `BarcodeScannerConfiguration` class. For more details, please refer to the Ready-to-Use UI page.
Want to scan longer than one minute?
Generate a free trial license to test the Scanbot SDK thoroughly.
Get your free Trial LicenseWhat do you think of this documentation?
What can we do to improve it? Please be as detailed as you like.