Cordova Angular Integration Example
Start a Cordova Project #
You need Node.js, and for Android JDK 17 or later and Gradle (see Installation). Install the Cordova CLI, make a project and add the platforms you build for:
npm install -g cordova
cordova create myApp com.example.myapp MyApp
cd myApp
cordova platform add android
# on a Mac, for iOS: cordova platform add ios
Use your app ID in place of com.example.myapp: the license key is issued for it. The commands below run in this folder.
Installation #
Add the plugin from npm:
cordova plugin add barkoder-cordova
Install Manually #
To install from a local folder instead:
- Download the plugin from Downloads in the barKoder Developer Portal
- Unpack it and rename the folder if you like (for example barkoder-cordova)
- Move the folder anywhere outside the project folder
- Add it to the project:
cordova plugin add "/your-path/barkoder-cordova"
Using the plugin with Angular #
Install the Angular CLI:
npm install -g @angular/cli
In the Cordova project's folder, make an Angular app (SCSS styles, no server-side rendering: Cordova serves the files from the app) and go into it:
ng new angular_app --style=scss --ssr=false
cd angular_app
Point its build at the Cordova project's www folder. Since Angular 17 the build puts the app in a browser subfolder; "browser": "" keeps it in www itself:
ng config projects.angular_app.architect.build.options.outputPath '{"base":"../www","browser":""}'
That sets this in angular.json:
"build": {
"options": {
"outputPath": {
"base": "../www",
"browser": ""
},
In src/index.html, load cordova.js before </head>: it puts the plugin on window.Barkoder and fires deviceready. The file exists only in the Cordova app, so the scanner does not run under ng serve.
<script src="cordova.js"></script>
Make a component for the scanner:
ng generate component BarcodeScanner
Angular makes it in src/app/barcode-scanner/: barcode-scanner.ts with the class BarcodeScanner (before Angular 20, barcode-scanner.component.ts with BarcodeScannerComponent). In barcode-scanner.ts, put the declare and import lines at the top and the rest inside the class:
declare var Barkoder: any;
import { ElementRef, ViewChild } from '@angular/core';
import { BarcodeType } from 'barkoder-cordova/www/BarkoderConfig';
@ViewChild('barkoderView') barkoderViewRef!: ElementRef;
setActiveBarcodeTypes() {
Barkoder.setBarcodeTypeEnabled(BarcodeType.code128, true);
Barkoder.setBarcodeTypeEnabled(BarcodeType.ean13, true);
}
setBarkoderSettings() {
Barkoder.setRegionOfInterestVisible(true);
Barkoder.setRegionOfInterest(5, 5, 90, 90);
Barkoder.setCloseSessionOnResultEnabled(false);
Barkoder.setImageResultEnabled(true);
Barkoder.setBarcodeThumbnailOnResultEnabled(true);
Barkoder.setBeepOnSuccessEnabled(true);
Barkoder.setPinchToZoomEnabled(true);
Barkoder.setZoomFactor(2.0);
}
async startScanning() {
const boundingRect = this.barkoderViewRef.nativeElement.getBoundingClientRect() as DOMRect;
Barkoder.registerWithLicenseKey("your_license_key");
await Barkoder.initialize(
Math.round(boundingRect.width),
Math.round(boundingRect.height),
Math.round(boundingRect.x),
Math.round(boundingRect.y));
this.setBarkoderSettings();
this.setActiveBarcodeTypes();
Barkoder.startScanning((barkoderResult: any) => {
barkoderResult.decoderResults.forEach((result: any) => console.log("Result: " + result.textualData));
}, (err: any) => {
console.log(err);
});
}
Put your license key in place of your_license_key. Without one the SDK still scans, but each result starts with (Unlicensed) and part of its data is replaced with dots: see Licensing on the Installation page.
In barcode-scanner.html, the element the scanner shows in, and a button that starts scanning:
<div id="barkoderView" #barkoderView></div>
<button (click)="startScanning()">Start scanning</button>
In barcode-scanner.scss, the height of the scanner:
#barkoderView {
height: 400px;
}
Show the component in the app. In src/app/app.ts, import it and add it to the component's imports:
import { BarcodeScanner } from './barcode-scanner/barcode-scanner';
@Component({
imports: [RouterOutlet, BarcodeScanner],
In src/app/app.html, replace Angular's placeholder (everything in the file) with:
<app-barcode-scanner></app-barcode-scanner>
Build and run #
Build the Angular app into www, then build and run the Cordova app (again after every change):
ng build
cd ..
cordova build android
cordova run android