Skip to main content

Scanning a barcode from an image

The Scanbot Barcode Scanner SDK is capable of detecting check data in live camera streams, as well as extracting barcodes from still images imported from the gallery or any other source.

Integration

To see the complete integration and the Barcode scanner in action, Example Apps are available:

  • Ready-To-Use UI: demo
  • Classic UI Components: demo

Adding the Feature as a Dependency

ScanbotBarcodeDetector is available with the Scanbot Standalone Barcode SDK. Firstly, you need to add dependencies.

Initializing the SDK

Before using the Scanbot SDK, it must be initialized by following these instructions.

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

  val sdk = ScanbotBarcodeScannerSDK(activity)
val barcodeDetector = sdk.createBarcodeDetector()
caution

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

ScanbotBarcodeDetector offers two detection methods:

  • fun detectFromBitmap(bitmap: Bitmap, frameOrientation: Int): BarcodeScanningResult?
  • fun detectFromJpeg(jpeg: ByteArray, frameOrientation: Int): BarcodeScanningResult?

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.).

Detection

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

    private fun processImage(
barcodeDetector: ScanbotBarcodeDetector,
bitmap: Bitmap
) {
if (!sdk.licenseInfo.isValid) { return; }
barcodeDetector.modifyConfig { //specify list of barcode formats to be expected in document if needs.}
val result = barcodeDetector.detectFromBitmap(bitmap, 0)
// handle the detected barcode(s) from result
}

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: BarcodeScanningResult data separated by newline characters, displayed in a Toast notification.

val barcodeData = result!!
val barcodesTextResult = StringBuilder()
for (item in barcodeData.barcodeItems) {
barcodesTextResult.append(item.barcodeFormat.name + "\n" + item.text)
.append("\n")
.append("-------------------")
.append("\n")
}
Toast.makeText(activity, barcodesTextResult.toString(), Toast.LENGTH_LONG).show()

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 your free Trial License

What do you think of this documentation?