Skip to main content

Scanning a barcode from an image file with the Android Barcode Scanner SDK

In addition to detecting barcodes from a live camera stream, the Barcode Scanner SDK can scan barcodes from still images imported from the gallery or any other source.

Integration

For an example of a complete integration of the Android Barcode Scanner, please refer to our scanbot-barcode-scanner-sdk-example-android repository on GitHub.

Adding the feature as a dependency

To use ScanbotBarcodeDetector, first add the Barcode Scanner SDK as a dependency.

Initializing the SDK

Before using the Scanbot SDK, it must be initialized.

Processing the image

To select an image from the photo library and run detection on it, a class for an image import contract is created using the modern Android result API.

class ImportImageContract(private val context: Context) : ActivityResultContract<Unit, Bitmap?>() {
override fun createIntent(context: Context, input: Unit): Intent {
// An image is selected from the photo library and document detection is run on it:
val imageIntent = Intent()
imageIntent.type = "image/*"
imageIntent.action = Intent.ACTION_GET_CONTENT
imageIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, false)
imageIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)

return Intent.createChooser(imageIntent, "Select a picture")
}

private fun processGalleryResult(data: Intent): Bitmap? {
val imageUri = data.data
return MediaStore.Images.Media.getBitmap(context.contentResolver, imageUri)
}

override fun parseResult(resultCode: Int, intent: Intent?): Bitmap? {
return if (resultCode == Activity.RESULT_OK && intent != null) {
return processGalleryResult(intent)
} else {
null
}
}
}

To run the gallery call and get a Bitmap using ImportImageContract, the following code is used:

 val galleryImageLauncher =
registerForActivityResult(ImportImageContract(this)) { resultEntity ->
lifecycleScope.launch(Dispatchers.Default) {
val activity = this@MainActivity
val sdk = ScanbotSDK(activity)
if (!sdk.licenseInfo.isValid) {
withContext(Dispatchers.Main) {
Toast.makeText(
activity,
"License has expired!",
Toast.LENGTH_LONG
).show()
}
} else {
resultEntity?.let { bitmap ->
// Image processing is carried out
// processImage()
}
}
}
}
findViewById<View>(R.id.import_image).setOnClickListener {
galleryImageLauncher.launch(Unit)
}

Creating the scanner

Creating the Scanner
loading...
caution

When createBarcodeScanner() is called, a new BarcodeScanner is created and memory is initialized for it. Please avoid creating new scanners for each import action if multiple import operations are expected.

BarcodeScanner offers two detection methods:

  • fun scanFromBitmap(bitmap: Bitmap, frameOrientation: Int): BarcodeScannerResult?
  • fun scanFromJpeg(jpeg: ByteArray, frameOrientation: Int): BarcodeScannerResult?

These methods detect barcodes on an image (JPG file or Bitmap) regardless of the image source (e.g., scanned document image, image picked from photo library, etc.).

Detecting barcodes

Employ the Scanbot SDK to detect the desired element on the imported bitmap.

Scanning from Bitmap
loading...

Input arguments

  • bitmap: Bitmap: Bitmap of the source image
  • jpeg: ByteArray: the byte array of the JPG source image
  • frameOrientation: Int: the frame orientation degrees

Handling the results

Here is a simple example of presenting the result: BarcodeScannerResult data separated by newline characters, displayed in a Toast notification.

Handling the Result
loading...

Full API references for the result class are available here.

Want to scan longer than one minute?

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

Get free trial license