On this page

Stuck? Open a support ticket and the barKoder team will help. Open a ticket

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:

Shell
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:

Shell
cordova plugin add barkoder-cordova

Install Manually #

To install from a local folder instead:

  1. Download the plugin from Downloads in the barKoder Developer Portal
  2. Unpack it and rename the folder if you like (for example barkoder-cordova)
  3. Move the folder anywhere outside the project folder
  4. Add it to the project:
Shell
cordova plugin add "/your-path/barkoder-cordova"

Using the plugin with Angular #

Install the Angular CLI:

Shell
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:

Shell
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:

Shell
ng config projects.angular_app.architect.build.options.outputPath '{"base":"../www","browser":""}'

That sets this in angular.json:

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.

HTML
<script src="cordova.js"></script>

Make a component for the scanner:

Shell
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:

TypeScript
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:

HTML
<div id="barkoderView" #barkoderView></div>
<button (click)="startScanning()">Start scanning</button>

In barcode-scanner.scss, the height of the scanner:

CSS
#barkoderView {
  height: 400px;
}

Show the component in the app. In src/app/app.ts, import it and add it to the component's imports:

TypeScript
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:

HTML
<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):

Shell
ng build
cd ..
cordova build android
cordova run android