[Ionic Tutorial #2] - Barcode Scanning

in #utopian-io6 years ago (edited)

ionic

Source

Repository

https://github.com/ionic-team/ionic

What Will I Learn?

In the previous tutorial I explained how to generate QR Code with Ionic. In this tutorial I will try to show you how to read any QR Code with Ionic and how to parse the data in QR Code.

  • You will learn how to generate Scan barcodes with Ionic Framework
  • You will learn how to parse data from scanned barcode with Ionic Framework

Requirements

  • NodeJS & NPM
  • Ionic NPM Package
  • Typescript & Angular Knowledge

Difficulty

  • Intermediate / Advanced

Start with a Blank App

First, start a simple Ionic application with following command, this command will create a simple Ionic application with starter template.

$ ionic start myApp blank

Add Barcode Scanner Package with NPM

I will use the Ionic Native Barcode Scanner plugin to scan the barcodes. Barcode Scanner Plugin opens the camera of phone and scans barcodes automatically, then returning scanned data back. You can easily read the raw data by assigning the returned data to a variable. Now download the barcode scanner plugin from NPM with the following commands.

$ ionic cordova plugin add phonegap-plugin-barcodescanner
$ npm install --save @ionic-native/barcode-scanner

Once you have completed downloading the library with NPM, you need to import this library into the app.module.ts file and add it to the providers list.

import { BarcodeScanner } from '@ionic-native/barcode-scanner';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    ...
  ],
  providers: [
    BarcodeScanner,
    ...
  ]
})
export class AppModule {}

Once you have imported the libraries, you can start working on the home page to read the barcodes, QR codes etc.
Reading barcodes with Ionic is really easy. You only need to define a variable for scanned data and assign the returned data from scanCode() function to the scanned_code variable.

export class HomePage {
  private scanned_code = null;

  constructor(public navCtrl: NavController,
    private barcodeScanner: BarcodeScanner) { }

  scanCode() {
    this.barcodeScanner.scan().then(data => {
      this.scanned_code = data;
     }).catch(err => {
         console.log('Error', err);
     });
  }
}

I just created a scanCode() function and defined a variable(scanned_code) to assign scanned data. I initially defined the scanned_code variable to be null. I have defined this variable as null because if there is no barcode data read from the camera, it will not be shown on the home page. So on the home page only the data read from a real barcode will be displayed.
The scanCode() function is also simple, it just triggers the camera, if there is any data from the barcode, it takes the data and assigns it to the scanned_code variable.

Now, you just need to add a button on the home page to trigger the scanCode() function.

<button (click)='scanCode()' color="dark"><ion-icon name="qr-scanner"></ion-icon></button>

Deploying to an Android Phone

You can test the app on your Android phone with the following commands.

$ ionic cordova build android

I installed my own phone and made a short gif. I just scanned the QR code of my Litecoin wallet on the phone. Yeah, it's working :)

Click to watch via IPFS

Curriculum

[Ionic Tutorial #1] - QR Code Generator App

Proof of Work Done

https://github.com/hsynterkr/Ionic-QR-Code-Generator-Scanner

Sort:  

Due to note earlier by @portugalcoin and the fact that your content has been plagiarized from other sources on the web https://ionicacademy.com/ionic-qr-code-generator-reader/
Your account has been banned from receiving Utopian reviews.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

You just received a Tier 0 upvote! Looking for bigger rewards? Click here and learn how to get them or visit us on Discord
If you would like to opt out of receiving comments reply with STOP

Thank you for your contribution.

  • I found a tutorial similar to yours here.
  • Try to come up with new and more innovative/useful ways to utilize Ionic.

See in this link an example of a good tutorial.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Link is incorrect, I can't see the answers.

No answers because it wasn't approved. I found a tutorial similar to yours.
Please try to come up with new and more innovative/useful ways to utilize Ionic.
Thank you.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.029
BTC 63362.14
ETH 2592.64
USDT 1.00
SBD 2.80