Skip to main content

Scanning a barcode from an image | Web Barcode Scanner

async detectBarcodes(base64: string, engineMode?: EngineMode, barcodeFormats?: BarcodeFormat[], returnBarcodeImage?: boolean): Promise<BarcodeResult>

ScanbotSDK provides a handy API to detect barcodes on still images. In conjunction with third-party libraries such as PDF.js, it can be used very effectively to also detect barcodes from an imported .pdf file.

To test it out for yourself, simply create a basic file picker:

<input class="file-picker" type="file" accept="image/jpeg" width="48" height="48">

Reference it in your script tag or file and listen to events:

picker.onchange = (e) => {
e.preventDefault();
let reader = new FileReader();
let file = e.target.files[0];
// Be sure to read it as a data url. ScanbotSDK expects a base64 string
reader.readAsDataURL(file);

reader.onload = async (e) => {
const result = await scanbotSDK.detectBarcodes(reader.result);
console.log("barcode detection result", result);
};
};

The result will be a standard BarcodeResult that is also the result of Barcode Scanner UI. BarcodeResult object that contains the following properties:

barcodes: Barcode[];

The list will always contain at least one element. No empty result is returned.

A barcode item contains the following properties:

  • format: BarcodeFormat – The type of barcode. One of the types listed in the previous paragraph.
  • text: string – The data contained in the barcode.
  • rawBytes: Uint8Array – The same data contained in the barcode. Can be disregarded in most scenarios, useful when the result is binary data, not a string.
  • barcodeImage: Uint8Array – The image of the recognized barcode if returnBarcodeImage in BarcodeScannerConfiguration is set to true.
  • parsedText – If the data is structured and parsing is supported for the data type, this structured data will be parsed into document-like data structures.

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?