Skip to main content

SDK initialization | Android Barcode Scanner

The Scanbot Barcode Scanner SDK must be initialized before usage.

For Android applications, we recommend initializing the SDK in your Application implementation. This ensures the SDK is correctly initialized even if the app's process is restored by the operating system after being terminated in the background.

To initialize the SDK, add the following code snippets to your Application class:

import io.scanbot.sdk.barcode_scanner.ScanbotBarcodeScannerSDKInitializer

class ExampleApplication : Application() {

override fun onCreate() {
super.onCreate()

ScanbotBarcodeScannerSDKInitializer()
// optional: uncomment the next line if you have a license key
// .license(this, LICENSE_KEY)
.initialize(this)
}
}

If your app does not have an Application class yet, create one and do not forget to register it in the AndroidManifest.xml file:

<application
android:name=".ExampleApplication"
...
>

Add Scanbot SDK license key

info

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.

Use the method license(application, LICENSE_KEY) on initialization of the Scanbot SDK to set your license key:

const val LICENSE_KEY = "YOUR_SCANBOT_SDK_LICENSE_KEY"

ScanbotSDKInitializer()
.license(this, LICENSE_KEY)
.initialize(this)

The Scanbot SDK license key is bound to the applicationId, which can be found in the app-level build.gradle file of your app. Please note that the applicationId is the actual and relevant ID of an app. The "package" name in the AndroidManifest.xml or the Java package are NOT relevant (also see the Android documentation).

Please make sure that you have inserted the exact key as it was provided by us, including all encoded line breaks \n.

License handling

caution

If your Scanbot SDK license has expired, any call to the Scanbot SDK API will return empty results. To prevent this you should always check for license expiration during the runtime via licenseInfo.isValid. If this property returns false, you should disable any usage of the Scanbot SDK (functions, UI components, etc).

See license handling section to learn how to handle the different license statuses in your app.

Control detection acceleration and optimizations

There are a few options developers can use to manage the SDK's detection performance.

  • Xnnpack acceleration - setting Xnnpack acceleration for ML models provides highly-optimized implementations of floating-point neural network operators and can improve performance up to 4x. It is enabled by default and only disabled for models that have shown problems during testing. If you encounter any problems with the detection you could try to disable this option by setting:
ScanbotBarcodeScannerSDKInitializer()
.allowXnnpackAcceleration(false)
...
.initialize(this)
  • GpuAcceleration - allows accelerating ML models using the GPU of the mobile device. By default it is enabled. To disable it set:
ScanbotBarcodeScannerSDKInitializer()
.allowGpuAcceleration(false)
...
.initialize(this)
caution

Unfortunately, we have noticed that all devices using a Cortex A53 processor DO NOT SUPPORT GPU acceleration. If you encounter any problems, please disable GPU acceleration for these devices.

  • Precompilation of the GPU ML models - allows to precompile ML model data thus speeding up the second and subsequent scanner initializations, but as a downfall the initial start of the scanner will be quite slow. It is disabled by default but can be enabled by setting:
ScanbotBarcodeScannerSDKInitializer()
.precompileGpuMlModels(precompilingCallback)
...
.initialize(this)

Where:

  • precompilingCallback is a lambda that will be triggered when GPU ML models compilation has completed.

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?