QR and Barcode Scanning
Implement QR code and barcode scanner in your Ionic app. Also, generate your own QR code and share with others

What is QR code and Barcode

A Barcode is a machine-readable optical label that contains information about the item to which it is attached. This is mostly used to catalog products in large warehouses, or departmental stores etc.
A QR code consists of black squares arranged in a square grid on a white background, which can be read by an imaging device such as a camera, and processed to return information like a website URL. Most mobile devices today have QR code scanners inbuilt, and they return the response on the screen.

Integrating Scanner in Ionic 5

This functionality requires Cordova plugin: phonegap-plugin-barcodescanner.The Barcode Scanner Plugin opens a camera view and automatically scans a barcode/ QR code, returning the data back to you.
Install the plugin using
$ ionic cordova plugin add phonegap-plugin-barcodescanner
$ npm install @ionic-native/barcode-scanner
Import the plugin in your page using
import { BarcodeScanner } from '@ionic-native/barcode-scanner/ngx';
constructor(private barcodeScanner: BarcodeScanner) { }
Barcode / QR code scanning functionality can be found in Ionic 5 Full App at src/app/pages/addons3/bar-code

Scanning a Barcode / QR code

The scanning function can be simply called with
this.barcodeScanner.scan().then(barcodeData => {
// success. barcodeData is the data returned by scanner
}).catch(err => {
// error
});
This opens the camera. When you point the camera to a valid barcode / QR code, it scans the code and returns the data in success
Scanning a barcode
Scanning a QR code
Data returned from scanning
You can use several options in the scan method
preferFrontCamera : true, // iOS and Android
showFlipCameraButton : true, // iOS and Android
showTorchButton : true, // iOS and Android
torchOn: true, // Android, launch with the torch switched on (if available)
saveHistory: true, // Android, save scan history (default false)
prompt : "Place a barcode inside the scan area", // Android
resultDisplayDuration: 500, // Android, display scanned text for X ms. 0 suppresses it entirely, default 1500
formats : "QR_CODE,PDF_417", // default: all but PDF_417 and RSS_EXPANDED
orientation : "landscape", // Android only (portrait|landscape), default unset so it rotates with the device
disableAnimations : true, // iOS
disableSuccessBeep: false // iOS and Android

Creating your own QR code

The plugin creates the object barcodeScanner with the method encode(type, data, success, fail).
Supported encoding types:
  • TEXT_TYPE
  • EMAIL_TYPE
  • PHONE_TYPE
  • SMS_TYPE
// Example for a string QR code (TEXT_TYPE)
this.barcodeScanner
.encode(this.barcodeScanner.Encode.TEXT_TYPE, "Your string here")
.then((encodedData) => {
console.log(encodedData);
}, (err) => {
console.log('Error occured : ' + err);
});
Created QR Code appears in a separate page, and can be shared
QR code for enappd.com
Encoding data has a known issue in iOS with Webview. We are keeping this as a // TODO for upcoming fix releases

iOS quirks

Since iOS 10 it's mandatory to add a NSCameraUsageDescription in the Info.plist
NSCameraUsageDescription describes the reason that the app accesses the user's camera. When the system prompts the user to allow access, this string is displayed as part of the dialog box. If you didn't provide the usage description, the app will crash before showing the dialog. Also, Apple will reject apps that access private data but don't provide an usage description.
To add this entry you can use the edit-config tag in the config.xml like this:
<edit-config target="NSCameraUsageDescription" file="*-Info.plist" mode="merge">
<string>To scan barcodes</string>
</edit-config>

For more info, please see the BarcodeScanner plugin docs.
Copy link
On this page
What is QR code and Barcode
Integrating Scanner in Ionic 5
Scanning a Barcode / QR code
Creating your own QR code
iOS quirks