Skip to main content

Scan from Image | Capacitor Barcode Scanner

To scan barcodes from an image (PNG or JPG file) use ScanbotBarcodeSDK.detectBarcodesOnImage function.

// Always make sure you have a valid license on runtime via ScanbotBarcodeSDK.getLicenseInfo()
if (!(await this.isLicenseValid())) {
return;
}

try {
// Select image from library
const imageFileUri = await this.imageUtils.selectImageFromLibrary();

const args: DetectBarcodesOnImageArguments = {
imageFileUri: imageFileUri,
stripCheckDigits: true,
barcodeFormats: await this.scanbotUtils.getAcceptedBarcodeFormats(), // optional filter for specific barcode types
acceptedDocumentFormats: await this.scanbotUtils.getAcceptedBarcodeDocumentFormats() // optional filter for specific document types
// see further args ...
};
await this.utils.showLoader();

const result = await ScanbotBarcodeSDK.detectBarcodesOnImage(args);

await this.utils.dismissLoader();

if (result.data?.barcodes && result.data.barcodes.length > 0) {
// Handle the detected barcode(s) from result
this.utils.showResultInfo(JSON.stringify(result.data.barcodes, null, 2));
} else {
this.utils.showInfoAlert('No barcodes detected');
}
} catch (error: any) {
await this.utils.dismissLoader();

this.utils.showErrorAlert(error);
}

Handing the Result

The scanning result object is the same as the result that you receive with Barcode Scanner UI.

Arguments

Send the imageFileUri as a part of the input arguments and in addition you can choose which barcode formats to accept, min/max length or any other property that is part of the DetectBarcodesOnImageArguments.

export interface DetectBarcodesOnImageArguments {
/**
* The input image file URI
*/
imageFileUri: string;
/**
* Accepted barcode formats
*/
barcodeFormats?: BarcodeFormat[];
/**
* An optional array of barcode document formats that act as a detection filter.
* By default all supported document formats will be detected.
*/
acceptedDocumentFormats?: BarcodeDocumentFormat[];
/**
* Barcode scanner engine mode. Default is NEXT_GEN
*/
engineMode?: EngineMode;
/**
* Optional minimum required text length of the detected barcode.
* The default is 0 (setting is turned off).
* NOTE: This feature works on ITF barcodes only.
*/
minimumTextLength?: number;
/**
* Optional maximum required text length of the detected barcode.
* The default is 0 (setting is turned off).
* NOTE: This feature works on ITF barcodes only.
*/
maximumTextLength?: number;
/**
* Optional minimum required quiet zone on the barcode.
* Measured in modules (the size of minimal bar on the barcode).
* The default is 10.
* NOTE: This feature works on ITF barcodes only.
*/
minimum1DBarcodesQuietZone?: number;
/** The GS1 handling mode. The default value is PARSE. */
gs1HandlingMode?: Gs1HandlingMode;
/**
* With this option enabled, the scanner removes checks digits for UPC, EAN and MSI Plessey codes.
* Has no effect if both single and double digit MSI Plessey checksums are enabled.
* The default is `false`
*/
stripCheckDigits?: boolean;
/**
* The checksum algorithm for MSI Plessey barcodes.
* The default value is Mod10.
*/
msiPlesseyChecksumAlgorithm?: MSIPlesseyChecksumAlgorithm;
/**
* If `true`, enabled the mode which slightly decreases the scanning quality and the energy consumption, and increases the scanning speed. If `false` - mode is disabled. The default is `false`. Android only.
* */
lowPowerMode?: boolean;
/**
* The expected density of QR codes in an image.
*/
codeDensity?: CodeDensity;
}

Want to scan longer than one minute?

Generate your free "no-strings-attached" Trial License and properly test the Scanbot SDK.

Get your free Trial License

What do you think of this documentation?


On this page

Scroll to top