Ionic 5 Full Starter App
  • Enappd Apps
  • Ionic 5 Full App
    • What is Ionic 5 Full App?
    • Template Features
    • Initial Setup
      • Environment Configuration
      • Credentials
    • Running the app
    • Deploying app as PWA
    • Building App on device
    • How to use this template?
      • Copy a module to another app
      • Shared Component & Modules
      • Map Service
    • Beginner Pack Features
      • Firebase
      • Layouts
      • Sidemenus
      • Login and Signups
      • Chat screens
      • Chat Lists
      • Video Playlists
      • Grids and Lists Layouts
    • Startup Pack Features
      • Wordpress
        • Wordpress JSON API Basics
        • Wordpress in Ionic 5 Full App
      • Translation - Multi-language
      • Using Custom Fonts
      • Infinite scroll
      • Content Loaders
      • Pull to refresh
      • List re-ordering
      • Date Pickers
    • Pro Pack Features
      • Phaser Game Framework
      • AdMob Integration
        • AdMob Introduction
        • Setting up Google Admob
        • Integration
      • Social Sharing
      • QR and Barcode Scanning
      • Google Places
      • Google Autocomplete
      • Social Logins
        • Google Login
        • Facebook Login
        • Twitter Login
      • Woo-commerce Integration
    • Removing a Page / Component
    • Removing a plugin
    • FAQs
    • Changelog
    • Troubleshoot
Powered by GitBook
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

Was this helpful?

  1. Ionic 5 Full App
  2. Pro Pack Features

QR and Barcode Scanning

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

PreviousSocial SharingNextGoogle Places

Last updated 5 years ago

Was this helpful?

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

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

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
Scanning a barcode
Scanning a QR code
Data returned from scanning
QR code for enappd.com