Dev Center
Table of contents

BarcodeReader for Images

A low-level barcode reader that processes still images and return barcode results. The following code snippet shows its basic usage.

let reader = await Dynamsoft.DBR.BarcodeReader.createInstance();
let results = await reader.decode(imageSource);
for(let result of results){
  console.log(result.barcodeText);
}

API Index

Create and Destroy Instances

API Name Description
createInstance Creates a BarcodeReader instance.
destroy Destroies the BarcodeReader instance.
bDestroyed Indicates whether the instance has been destroyed.

Decode Barcodes

API Name Description
decode Decodes barcodes from an image.
decodeBase64String Decodes barcodes from a base64-encoded image (with or without MIME).
decodeUrl Decodes barcodes from an image specified by its URL.
decodeBuffer Decodes barcodes from raw image data.

Change Settings

API Name Description
getRuntimeSettings Returns the current runtime settings.
updateRuntimeSettings Updates runtime settings with a given struct or a preset template.
resetRuntimeSettings Resets all parameters to default values.
getModeArgument Returns the argument value for the specified mode parameter.
setModeArgument Sets the argument value for the specified mode parameter.

Auxiliary

API Name Description
bSaveOriCanvas Whether to save the original image into a <canvas> element.
oriCanvas An HTMLCanvasElement that holds the original image.

createInstance

Creates a BarcodeReader instance.

static createInstance(): Promise<BarcodeReader>

Parameters

None.

Return value

A promise resolving to the created BarcodeReader object.

Code snippet

let reader = await Dynamsoft.DBR.BarcodeReader.createInstance();

destroy

Destroies the BarcodeReader instance. If your page needs to create new instances from time to time, don’t forget to destroy unused old instances.

destroy(): Promise<void>

Parameters

None.

Return value

A promise that resolves when the operation succeeds.

Code snippet

let reader = await Dynamsoft.DBR.BarcodeReader.createInstance();
// ... decode ...
reader.destroy();

bDestroyed

Indicates whether the instance has been destroyed.

readonly bDestroyed: boolean

decode

Decodes barcodes from an image.

decode(source: Blob | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | string): Promise<TextResult[]>

Parameters

source: specifies the image to decode. The supported image formats include png, jpeg, bmp, gif and a few others (some browsers support webp, tif). Also note that the image can be specified in a lot of ways including binary data, base64 string (with MIME), URL, etc.

Return value

A promise resolving to a TextResult object that contains all the barcode results found in this image.

Code snippet

let results1 = await reader.decode(blob);
let results2 = await reader.decode(htmlImageElement);
let results3 = await reader.decode(url);
let results4 = await reader.decode(strBase64WithMime); // like `data:image/png;base64,iV************`

You can even use an HTMLVideoElement as the source. If the video is playing, the current frame will be decoded.

let results;
try{
  // The current frame will be decoded.
  results = await reader.decode(htmlVideoElement);
}catch(ex){
  // If no frame in the video, throws an exception.   
}

See also

decodeBase64String

Decodes barcodes from a base64-encoded image (with or without MIME).

decodeBase64String(base64Str: string): Promise<TextResult[]>

Parameters

base64Str: specifies the image represented by a string.

Return value

A promise resolving to a TextResult object that contains all the barcode results found in this image.

Code snippet

let results = await reader.decodeBase64String(strBase64); //e.g. `data:image/jpg;base64,Xfjshekk....` or `Xfjshekk...`.
for(let result of results){
  console.log(result.barcodeText);
}

See also

decodeUrl

Decodes barcodes from an image specified by its URL. Note that the image should either be from the same domain or has the ‘Access-Control-Allow-Origin’ header set to allow access from your current domain.

decodeUrl(url: string): Promise<TextResult[]>

Parameters

url: specifies the image with its URL.

Return value

A promise resolving to a TextResult object that contains all the barcode results found in this image.

Code snippet

let results = await reader.decodeUrl("https://www.yourdomain.com/imageWithBarcodes.png");
for(let result of results){
    console.log(result.barcodeText);
}

See also

decodeBuffer

Decodes barcodes from raw image data.

decodeBuffer(buffer: Blob | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray, width: number, height: number, stride: number, format: EnumImagePixelFormat): Promise<TextResult[]>

Parameters

buffer: specifies the image represented by a Uint8Array, Uint8ClampedArray, ArrayBuffer, Blob or Buffer object.

Return value

A promise resolving to a TextResult object that contains all the barcode results found in this image.

See also

getRuntimeSettings

Returns the current runtime settings.

getRuntimeSettings(): Promise<RuntimeSettings>

Parameters

None.

Return value

A promise resolving to a RuntimeSettings object that contains the settings for barcode reading.

Code snippet

let settings = await reader.getRuntimeSettings();
settings.barcodeFormatIds = Dynamsoft.DBR.EnumBarcodeFormat.BF_QR_CODE; // Decodes only QR code.
await reader.updateRuntimeSettings(settings);

See also

updateRuntimeSettings

Updates runtime settings with a given struct or a preset template represented by one of the following strings

  • speed: fast but may miss a few codes;
  • coverage: slow but try to find all codes, this is the default setting for a BarcodeReader instance;
  • balance: between speed and coverage;
  • single: optimized for scanning one single barcode from a video input, this is supported only by the sub-class BarcodeScanner and is also the default setting for a BarcodeScanner instance.
updateRuntimeSettings(settings: RuntimeSettings | string): Promise<void>

Parameters

settings: a RuntimeSettings object that contains the new settings for barcode reading.

Return value

A promise that resolves when the operation succeeds.

Code snippet

await reader.updateRuntimeSettings('balance');
let settings = await reader.getRuntimeSettings();
settings.barcodeFormatIds = Dynamsoft.DBR.EnumBarcodeFormat.BF_ONED;
await reader.updateRuntimeSettings(settings);

See also

resetRuntimeSettings

Resets all parameters to default values.

For a BarcodeReader instance, it is equivalent to setting the coverage template.

For a BarcodeScanner instance, it is equivalent to setting the speed template.

resetRuntimeSettings(): Promise<void>

Parameters

None.

Return value

A promise that resolves when the operation succeeds.

Code snippet

await reader.resetRuntimeSettings();

getModeArgument

Returns the argument value for the specified mode parameter.

getModeArgument(modeName: string, index: number, argumentName: string): Promise<string>

Parameters

modeName: specifies the mode which contains one or multiple elements. index: specifies an element of the mode by its index. argumentName: specifies the argument.

Return value

A promise resolving to a string which represents the value of the argument.

Code snippet

let argumentValue = await reader.getModeArgument("BinarizationModes", 0, "EnableFillBinaryVacancy");

See also

setModeArgument

Sets the argument value for the specified mode parameter.

setModeArgument(modeName: string, index: number, argumentName: string, argumentValue: string): Promise<void>

Parameters

modeName: specifies the mode which contains one or multiple elements. index: specifies an element of the mode by its index. argumentName: specifies the argument. argumentValue: specifies the value.

Return value

A promise that resolves when the operation succeeds.

Code snippet

await reader.setModeArgument("BinarizationModes", 0, "EnableFillBinaryVacancy", "1");

See also

bSaveOriCanvas

Whether to save the original image into a <canvas> element. The original image refers to the actual image the library tried to read barcodes from.

Note that the result is an HTMLCanvasElement element and you can insert it into the DOM to show the image.

bSaveOriCanvas: boolean;

Default value

false

Code snippet

reader.bSaveOriCanvas = true;
let results = await reader.decode(source);
document.body.append(reader.oriCanvas);

oriCanvas

An HTMLCanvasElement that holds the original image. The original image refers to the actual image the library tried to read barcodes from.

readonly oriCanvas: HTMLCanvasElement | OffscreenCanvas

Code snippet

reader.bSaveOriCanvas = true;
let results = await reader.decode(source);
document.body.append(reader.oriCanvas);

See also

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

version 8.4.0

  • Latest version(10.2.10)
  • Version 10.x
    • Version 10.0.21
    • Version 10.0.20
  • Version 9.x
    • Version 9.6.40
    • Version 9.6.33
    • Version 9.6.32
    • Version 9.6.31
    • Version 9.6.30
    • Version 9.6.21
    • Version 9.6.20
    • Version 9.6.11
    • Version 9.6.10
    • Version 9.6.2
    • Version 9.6.1
    • Version 9.6.0
    • Version 9.3.1
    • Version 9.3.0
    • Version 9.2.13
    • Version 9.2.12
    • Version 9.2.11
    • Version 9.0.2
    • Version 9.0.1
    • Version 9.0.0
  • Version 8.x
    • Version 8.8.7
    • Version 8.8.5
    • Version 8.8.3
    • Version 8.8.0
    • Version 8.6.3
    • Version 8.6.0
    • Version 8.4.0
    • Version 8.2.5
    • Version 8.2.3
    • Version 8.2.1
    • Version 8.2.0
    • Version 8.1.3
    • Version 8.1.2
    • Version 8.1.0
    • Version 8.0.0
  • Version 7.x
    • Version 7.6.0
    • Version 7.5.0
Change +