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 BarcodeScanner, first add the Barcode Scanner SDK as a dependency.
Initializing the SDK
Before using the Scanbot SDK, it must be initialized.
Processing the image
Obtaining the image from the gallery
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.
loading...
To run the gallery call and get a Bitmap using ImportImageContract, the following code is used:
loading...
Creating the scanner
loading...
When createBarcodeScanner(configuration: BarcodeScannerConfiguration) 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 the following detection method:
fun run(image: io.scanbot.sdk.image.ImageRef): Result<BarcodeScannerResult>
This method scans barcodes on an image (represented as ImageRef) 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.
loading...
The ImageRef class provides a set of static methods for creating instances from different sources, such as Bitmap, String paths, ByteArray, and others.
Input arguments
image: io.scanbot.sdk.image.ImageRef: the source image represented inImageRefformat
Handling the results
Result is represented by the sealed interface io.scanbot.common.Result, which has two main implementations: Result.Success and Result.Failure (the latter includes several more specific subclasses).
BarcodeScannerResult is returned only when the execution result is Result.Success. You can access it using convenience methods such as result.onSuccess { scannerResult -> }, or by retrieving the value from the result.data field.
Here is a simple example of presenting the result: BarcodeScannerResult data separated by newline characters, displayed in a Toast notification.
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