Did you visit Droidcon 2024 in Berlin? If so, you may have noticed our Integration Challenge, for which we asked developers to integrate our Barcode Scanner SDK for Android in less than 10 minutes.
40 people tried to do so. All of them managed it in less than 6 minutes, with the best time clocking in at incredible 1:02! 🤯
Can you top that? Let’s find out – we’ve added the instructions to our Quick Start Guide!
⏱️ If you want to see how fast you can do it, start your clock when you scroll down.
Let’s go through the steps one by one.
[toc]Step 1: Clone the template app (or create a project from scratch)
We’ve prepared a template project so you won’t waste any time on building a UI. You can clone or download it from our GitHub repository.
Open the project in Android Studio and you’re good to go.
Step 2: Add the 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:5.2.0")
implementation("io.scanbot:rtu-ui-v2-barcode:5.2.0")
To check out the latest version of the Scanbot SDK, please always refer to the SDK’s changelog.
For more details on the dependencies, please refer to our detailed installation guide.
Step 3: Add the camera permission
The Scanbot Barcode Scanner SDK needs access to the device camera so it can scan from a live camera stream. Therefore, you have to define the camera permission 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 the Android documentation).
Our Ready-to-Use UI Components handle the runtime permissions automatically, so there is no need to add anything else in the code.
Step 4: 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’ll do it in an Activity
class.
First, make sure to add the following imports to the top of the file (e.g., MainActivity.kt
):
// 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 your Activity's onCreate method as follows:
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 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.
Step 5: Start the Barcode Scanner and process the result
You only need a few lines of code to integrate the Scanbot Barcode Scanner UI (BarcodeScannerActivity
) into your application’s workflow. It’s as simple as starting a 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. If you’d like, let us know what your time was: hello@scanbot.io
💡 Customization: In this Quick Start guide, we have used the default configuration for the scanner UI. Feel free to explore the configs and customize the UI and the behavior according to your needs via the BarcodeScannerConfiguration
class. For more details, please refer to the Ready-to-Use UI page.