Thanks for downloading Dynamsoft Barcode Reader Package!
Your download will start shortly. If your download does not begin, click here to retry.
Angular is one of the most popular and mature JavaScript frameworks. Check out the following on how to implement Dynamsoft Barcode Reader JavaScript SDK (hereafter called “the library”) into an Angular application.
Make sure you have node and Angular CLI installed. node 14.16.0
and Angular CLI 11.2.4
are used in the example below.
ng new read-video-angular
npm install dynamsoft-javascript-barcode
import DBR from "dynamsoft-javascript-barcode";
DBR.BarcodeReader.engineResourcePath = "https://cdn.jsdelivr.net/npm/dynamsoft-javascript-barcode@8.4.0/dist/";
export default DBR;
Note:
- There are multiple settings available for the configuration, here we only set the
engineResourcePath
which is essential for the library to get the necessary resources at runtime.
ng generate component barcode-scanner
ng generate component hello-world
Open the file .\node_modules\dynamsoft-javascript-barcode\dist\dbr.scanner.html
, copy everything and paste in barcode-scanner.component.html
.
In barcode-scanner.component.ts
, add code for initializing and destroying the library.
import { Component, OnInit, ElementRef } from '@angular/core';
import DBR from '../dbr';
export class BarcodeScannerComponent implements OnInit {
bDestroyed = false;
pScanner = null;
constructor(private elementRef: ElementRef) { }
async ngOnInit(): Promise<void> {
try {
let scanner = await (this.pScanner = this.pScanner || DBR.BarcodeScanner.createInstance());
if (this.bDestroyed) {
scanner.destroy();
return;
}
scanner.setUIElement(this.elementRef.nativeElement);
await scanner.open();
} catch (ex) {
console.error(ex);
}
}
async ngOnDestroy() {
this.bDestroyed = true;
if (this.pScanner) {
(await this.pScanner).destroy();
}
}
}
Note:
- The method
createInstance()
is called to initialize the library as soon as the component initializes.- To release resources timely, the
BarcodeScanner
instance is destroyed with the component in the callbackngOnDestroy
.- The method
setUIElement()
specifies the UI for the library with the native element inbarcode-scanner.component.html
which we just copied over in the previous step.
hello-world.component.html
<div id="UIElement">
<app-barcode-scanner></app-barcode-scanner>
</div>
hello-world.component.css
#UIElement {
margin: 2vmin auto;
text-align: center;
font-size: medium;
height: 40vh;
width: 80vw;
}
app.component.html
Edit the file app.component.html
to contain nothing but the following
<app-hello-world></app-hello-world>
ng serve -o
If you followed all the steps correctly, you will have a working page that turns one of the cameras hooked to or built in your computer or mobile device into a barcode scanner. However, the found barcodes are not displayed anywhere yet. At the same time, there is a short delay for the initialization of the library during which nothing happens and is not user-friendly. The following takes care of these two issues.
hello-world.component.html
like this<div id="UIElement">
<span style='font-size:x-large' *ngIf="!libLoaded">Loading Library...</span>
<app-barcode-scanner *ngIf="bShowScanner" (appendMessage)="appendMessage($event)"></app-barcode-scanner>
</div>
<input id="resultText" type="text" [value]="resultValue" readonly="true">
hello-world.component.css
#resultText {
display: block;
margin: 0 auto;
padding: 0.4rem 0.8rem;
color: inherit;
width: 80vw;
border: none;
font-size: 1rem;
border-radius: 0.2rem;
text-align: center;
}
hello-world.component.ts
, write the following codeimport DBR from '../dbr';
export class HelloWorldComponent implements OnInit {
bShowScanner = false;
resultValue = "";
libLoaded = false;
constructor() { }
async ngOnInit(): Promise<void> {
try {
//Load the library on page load to speed things up.
await DBR.BarcodeScanner.loadWasm();
this.libLoaded = true;
this.showScanner();
} catch (ex) {
alert(ex.message);
throw ex;
}
}
showScanner(): void {
this.bShowScanner = true;
}
hideScanner(): void {
this.bShowScanner = false;
}
appendMessage(message) {
switch (message.type) {
case "result":
this.resultValue = message.format + ": " + message.text;
break;
case "error":
this.resultValue = message.msg;
break;
default: break;
}
}
}
NOTE :
- The method
loadWasm()
initializes the library in the background. The scanner UI is only shown when the initialization finishes.- The method
appendMessage()
is used to show the result text on the page.
EventEmitter
in barcode-scanner.component.ts
and use the event onFrameRead
to return the results.import { Component, OnInit, EventEmitter, Output, ElementRef } from '@angular/core';
export class BarcodeScannerComponent implements OnInit {
//Omitted code...
@Output() appendMessage = new EventEmitter();
async ngOnInit(): Promise<void> {
try {
//Omitted code...
scanner.setUIElement(this.elementRef.nativeElement);
scanner.onFrameRead = results => {
for (let result of results) {
this.appendMessage.emit({ format: result.barcodeFormatString, text: result.barcodeText, type: "result" });
}
};
await scanner.open();
} catch (ex) {
this.appendMessage.emit({ msg: ex.message, type: "error" });
console.error(ex);
}
}
//Omitted code...
}
NOTE :
- The event
onFrameRead
is triggered upon reading of each frame. If barcodes are found on that frame, the results will be returned and shown on the page.
After the above changes, the application is made more user-friendly and the barcode text is displayed on the page right away. You can start implementing your own business workflow and make the application useful.
latest version