BarcodeReader
-
class BarcodeReader
The
BarcodeReader
class is used for decoding barcodes from images.let reader = await Dynamsoft.DBR.BarcodeReader.createInstance(); let results = await reader.decode(imageSource); for(let result of results){ console.log(result.barcodeText); }
Index
Static Property | Description |
---|---|
engineResourcePath | Specifies the WASM engine URL. |
_bUseFullFeature | If set to true , uses the full WASM engine. |
Static Method | Description |
---|---|
isLoaded | Checks if the decoding module is loaded. |
loadWasm | Manually loads and compiles the decoding WASM module. |
Property | Description |
---|---|
bDestroyed | Indicates whether the instance has been destroyed. |
Static Method | Description |
---|---|
createInstance | Creates a BarcodeReader instance. |
Method | Description |
---|---|
destroy | Destroies the BarcodeReader instance. |
Method | Description |
---|---|
decode | Decodes barcodes from images, binary data, URLs, and more. |
decodeBase64String | Decodes barcodes from a base64 encoded string. |
decodeUrl | Decodes barcodes from a URL. |
decodeBuffer | Decodes barcodes from raw image data. |
Method | Description |
---|---|
getRuntimeSettings | Gets current runtime settings. |
updateRuntimeSettings | Modifies and updates the current runtime settings. |
resetRuntimeSettings | Resets runtime settings to default. |
getModeArgument | Gets argument value for the specified mode parameter. |
setModeArgument | Sets argument value for the specified mode parameter. |
Property | Description |
---|---|
productKeys | Gets or sets the Dynamsoft Barcode Reader SDK product keys. |
licenseServer | Specifies the license server URL. |
handshakeCode | Uses Handshake Code to get authentication. |
organizationID | Uses Organization ID to get authentication. |
sessionPassword | Specifies a password to protect the Handshake Code from abuse. |
Static Property | Description |
---|---|
version | Gets the current product version. |
Property | Description |
---|---|
bSaveOriCanvas | If set to true , saves the original image in oriCanvas . |
oriCanvas | An canvas object that holds the original image. |
Static Method | Description |
---|---|
detectEnvironment | Detects environments and gets a report. |
Initialize the Engine
engineResourcePath
-
static
engineResourcePath: stringManually specifies the URL of Barcode Reader SDK engine (WASM) . The property needs to be set before loadWasm.
Dynamsoft.DBR.BarcodeReader.engineResourcePath = "https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@8.2.5/dist/"; await Dynamsoft.DBR.BarcodeReader.loadWasm();
_bUseFullFeature
-
static
_bUseFullFeature: boolean = falseWhether to use full WASM engine. The property needs to be set before loadWasm. The default is ‘false’.
DBR.BarcodeReader._bUseFullFeature = true; await DBR.BarcodeReader.loadWasm();
Learn more about differences between compact and full WASM engines.
isLoaded
-
static
isLoaded(): booleanChecks if the decoding module is loaded.
loadWasm
-
static
loadWasm(): Promise<void>Before most operations,
loadWasm
needs to be excuted firstly.Most time, you do not need excute
loadWasm
manually. Because when you excute createInstance,loadWasm
will be excuted implicitly.Some properties can’t be changed after
loadWasm
.Calling
loadWasm
in advance can avoid the long wait whencreateInstance
.window.addEventListener('DOMContentLoaded', (event) => { DBR.BarcodeReader.loadWasm(); });
Create and Destroy Instance
bDestroyed
-
bDestroyed: boolean
Indicates whether the instance has been destroyed.
createInstance
-
static
createInstance(): Promise<BarcodeReader>Create a
BarcodeReader
instance.let reader = await Dynamsoft.DBR.BarcodeReader.createInstance();
destroy
-
destroy(): Promise<void>
Destroies the
BarcodeReader
instance. If your page needs to create new instances from time to time, don’t forget to destroy unused old instances to avoid possible memory leaks.
Decode Barcode
decode
-
decode (source: Blob | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray
| HTMLImageElement | HTMLCanvasElement | HTMLVideoElement
| string): Promise<TextResult[]>
Decodes barcodes from an image.
The main decoding method can accept a variety of data types, including binary data, images, base64 (with MIME), URL, etc.
The image format can be
png
,jpeg
,bmp
,gif
and a few others (some browsers supportwebp
,tif
).let results = await reader.decode(blob); for(let result of results){ console.log(result.barcodeText); } let results2 = await reader.decode(htmlImageElement); let results2 = await reader.decode(url); // like `data:image/png;base64,iV************` let results3 = await reader.decode(strBase64WithMime);
And you can get a frame from the
HTMLVideoElement
for barcode scanning.// The frame currently played will be decoded. let results; try{ results = await reader.decode(htmlVideoElement); }catch(ex){ // If no frame in the video, throws an exception. }
If you need to continuously decode a video, you can use BarcodeScanner instead.
@see decodeBase64String, decodeUrl
decodeBase64String
-
decodeBase64String(base64: string): Promise<TextResult[]>
The decoding method can accept base64 with or without MIME. e.g.
data:image/jpg;base64,Xfjshekk....
orXfjshekk...
.let results = await reader.decodeBase64String(strBase64); for(let result of results){ console.log(result.barcodeText); }
decodeUrl
-
decodeUrl(url: string): Promise<TextResult[]>
The decoding method can accept an URL. The URL source needs to be in the same domain or allowed Cross-Origin Resource Sharing (CORS).
let results = await reader.decodeUrl("./1.png"); for(let result of results){ console.log(result.barcodeText); }
decodeBuffer
-
decodeBuffer(
buffer: Uint8Array | Uint8ClampedArray | ArrayBuffer | Blob | Buffer,
width: number, height: number, stride: number,
format: EnumImagePixelFormat
): Promise<TextResult[]>Decodes barcodes from raw image data.
Decoding Settings
getRuntimeSettings
-
getRuntimeSettings(): Promise<RuntimeSettings>
Gets current runtime settings.
let settings = await reader.getRuntimeSettings(); settings.deblurLevel = 5; await reader.updateRuntimeSettings(settings);
updateRuntimeSettings
-
updateRuntimeSettings(settings: RuntimeSettings | string): Promise<void>
Updates runtime settings with a given struct or a string of
speed
,balance
orcoverage
to use preset settings forBarcodeReader
.The default runtime setting for BarcodeReader is
coverage
.await reader.updateRuntimeSettings('balance'); let settings = await reader.getRuntimeSettings(); settings.barcodeFormatIds = Dynamsoft.DBR.EnumBarcodeFormat.BF_ONED; await reader.updateRuntimeSettings(settings);
resetRuntimeSettings
-
resetRuntimeSettings(): Promise<void>
Resets all parameters to default values.
await reader.resetRuntimeSettings();
getModeArgument
-
getModeArgument(modeName: string, index: number, argumentName: string): Promise<string>
Gets argument value for the specified mode parameter.
let argumentValue = await reader.getModeArgument("BinarizationModes", 0, "EnableFillBinaryVacancy");
@see C++ getModeArgument
setModeArgument
-
setModeArgument(modeName: string, index: number, argumentName: string, argumentValue: string): Promise<string>
Sets argument value for the specified mode parameter.
await reader.setModeArgument("BinarizationModes", 0, "EnableFillBinaryVacancy", "1");
@see C++ setModeArgument
License
productKeys
-
static
productKeys: stringGets or sets the Dynamsoft Barcode Reader SDK product keys.
Dynamsoft.DBR.BarcodeReader.productKeys = "PRODUCT-KEYS";
For convenience, you can set
productKeys
inscript
tag instead.<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@8.2.5/dist/dbr.js" data-productKeys="PRODUCT-KEYS"></script>
licenseServer
-
static
licenseServer: string[] | stringSpecifies the license server URL.
handshakeCode
-
static
handshakeCode: stringGets or sets the Dynamsoft Barcode Reader SDK handshake code. The
handshakeCode
is an alias ofproductKeys
. Specifically refers to the key that requires network authentication.Dynamsoft.DBR.BarcodeReader.handshakeCode = "123****-mytest";
For convenience, you can set
handshakeCode
inscript
tag instead.<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@8.2.5/dist/dbr.js" data-handshakeCode="123****-mytest"></script>
organizationID
-
static
organizationID: stringUse organization ID to get authentication from network. Keep handshakeCode empty if you want to use default handshake of the organization.
Dynamsoft.DBR.BarcodeReader.organizationID = "123****";
For convenience, you can set
organizationID
inscript
tag instead.<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@8.2.5/dist/dbr.js" data-organizationID="123****"></script>
sessionPassword
-
static
sessionPassword: stringSpecifies a password to protect the Handshake Code.
Dynamsoft.DBR.BarcodeReader.handshakeCode = "123****-mytest"; Dynamsoft.DBR.BarcodeReader.sessionPassword = "@#$%****";
For convenience, you can set
organizationID
inscript
tag instead.<script src="https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@8.2.5/dist/dbr.js" data-handshakeCode="123****-mytest" data-sessionPassword="@#$%****"></script>
Other
bSaveOriCanvas
-
bSaveOriCanvas: boolean = false;
Whether to save the original image into canvas.
reader.bSaveOriCanvas = true; let results = await reader.decode(source); document.body.append(reader.oriCanvas);
oriCanvas
-
readonly
oriCanvas: HTMLCanvasElement | OffscreenCanvasA canvas object that holds the original image.
reader.bSaveOriCanvas = true; let results = await reader.decode(source); document.body.append(reader.oriCanvas);
version
-
readonly
static
version: stringGets the current product version. Needs to call after loadWasm.
console.log(Dynamsoft.DBR.BarcodeReader.version); // "loading...(JS 8.2.5.20210426)" await Dynamsoft.DBR.BarcodeReader.loadWasm(); console.log(Dynamsoft.DBR.BarcodeReader.version); // "8.4.0.8960(JS 8.2.5.20210426)"
detectEnvironment
-
static
detectEnvironment(): Promise<any>Detects environments and gets a report.
console.log(Dynamsoft.DBR.BarcodeReader.detectEnvironment()); // {"wasm":true, "worker":true, "getUserMedia":true, "camera":true, "browser":"Chrome", "version":90, "OS":"Windows"}