create account

[Ionic Tutorial #2] - Barcode Scanning by hsynterkr

View this thread on: hive.blogpeakd.comecency.com
· @hsynterkr · (edited)
$0.74
[Ionic Tutorial #2] - Barcode Scanning
![ionic](https://ionicframework.com/img/meta/ionic-framework-og.png)
>[Source](https://ionicframework.com)
#### Repository
https://github.com/ionic-team/ionic

#### What Will I Learn?
In the [previous tutorial](https://steemit.com/utopian-io/@hsynterkr/ionic-framework-tutorial-1-qr-code-generator-app) 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](https://steemia.net/ipfs/QmZThh73Xh2cN8k1tPE6VyWM9vedrP2DVNhhzwZXr1fWMF)

#### Curriculum
[[Ionic Tutorial #1] - QR Code Generator App](https://steemit.com/utopian-io/@hsynterkr/ionic-framework-tutorial-1-qr-code-generator-app)

#### Proof of Work Done
https://github.com/hsynterkr/Ionic-QR-Code-Generator-Scanner
👍  , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorhsynterkr
permlinkionic-tutorial-2-barcode-scanning
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","ionic","steemia","dev"],"image":["https://ionicframework.com/img/meta/ionic-framework-og.png"],"links":["https://ionicframework.com","https://github.com/ionic-team/ionic","https://steemit.com/utopian-io/@hsynterkr/ionic-framework-tutorial-1-qr-code-generator-app","https://steemia.net/ipfs/QmZThh73Xh2cN8k1tPE6VyWM9vedrP2DVNhhzwZXr1fWMF","https://steemit.com/utopian-io/@hsynterkr/ionic-framework-tutorial-1-qr-code-generator-app","https://github.com/hsynterkr/Ionic-QR-Code-Generator-Scanner"],"app":"busy/2.4.0","format":"markdown","community":"busy","users":["hsynterkr","ionic-native","NgModule"]}
created2018-05-30 07:50:24
last_update2018-05-30 21:18:45
depth0
children5
last_payout2018-06-06 07:50:24
cashout_time1969-12-31 23:59:59
total_payout_value0.621 HBD
curator_payout_value0.120 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,862
author_reputation12,645,130,948,582
root_title"[Ionic Tutorial #2] - Barcode Scanning"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id58,403,977
net_rshares193,437,432,733
author_curate_reward""
vote details (24)
@mcfarhat ·
$0.04
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](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
👍  ,
properties (23)
authormcfarhat
permlinkre-hsynterkr-ionic-tutorial-2-barcode-scanning-20180531t101411648z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["portugalcoin"],"links":["https://ionicacademy.com/ionic-qr-code-generator-reader/","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-05-31 10:14:18
last_update2018-05-31 10:14:18
depth1
children0
last_payout2018-06-07 10:14:18
cashout_time1969-12-31 23:59:59
total_payout_value0.032 HBD
curator_payout_value0.009 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length406
author_reputation150,651,671,367,256
root_title"[Ionic Tutorial #2] - Barcode Scanning"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id58,576,892
net_rshares11,254,786,798
author_curate_reward""
vote details (2)
@portugalcoin ·
Thank you for your contribution.

- I found a tutorial similar to yours <a href="https://ionicacademy.com/ionic-qr-code-generator-reader/">here</a>.
- Try to come up with new and more innovative/useful ways to utilize Ionic.

See in this <a href="https://steemit.com/utopian-io/@espoem/make-your-posts-look-great-in-steemit-and-busy">link</a> an example of a good tutorial.


Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/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](https://review.utopian.io/result/8/00000000).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
properties (22)
authorportugalcoin
permlinkre-hsynterkr-ionic-tutorial-2-barcode-scanning-20180530t204343438z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://ionicacademy.com/ionic-qr-code-generator-reader/","https://steemit.com/utopian-io/@espoem/make-your-posts-look-great-in-steemit-and-busy","https://join.utopian.io/guidelines","https://review.utopian.io/result/8/00000000","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-05-30 20:43:42
last_update2018-05-30 20:43:42
depth1
children2
last_payout2018-06-06 20:43:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length866
author_reputation599,467,940,300,649
root_title"[Ionic Tutorial #2] - Barcode Scanning"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id58,495,246
net_rshares0
@hsynterkr ·
Link is incorrect, I can't see the answers.
properties (22)
authorhsynterkr
permlinkre-portugalcoin-re-hsynterkr-ionic-tutorial-2-barcode-scanning-20180530t212133574z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"busy","app":"busy/2.4.0"}
created2018-05-30 21:21:36
last_update2018-05-30 21:21:36
depth2
children1
last_payout2018-06-06 21:21:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length43
author_reputation12,645,130,948,582
root_title"[Ionic Tutorial #2] - Barcode Scanning"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id58,499,470
net_rshares0
@portugalcoin ·
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](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
properties (22)
authorportugalcoin
permlinkre-hsynterkr-re-portugalcoin-re-hsynterkr-ionic-tutorial-2-barcode-scanning-20180530t214209950z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-05-30 21:42:09
last_update2018-05-30 21:42:09
depth3
children0
last_payout2018-06-06 21:42:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length337
author_reputation599,467,940,300,649
root_title"[Ionic Tutorial #2] - Barcode Scanning"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id58,501,850
net_rshares0
@qurator-tier-0 ·
<div class="pull-left"><img src="![Qsmall.png](https://steemitimages.com/DQmcWoZUnPrRW1UdZeVPWhHpnXfkgWAdFHGxMrSgZoxSZw8/Qsmall.png)" /></div>
		
<center>You just received a Tier 0 upvote!  Looking for bigger rewards? Click [here](https://steemit.com/qurator/@qurator/qurator-tier-changes) and learn how to get them or visit us on [Discord](https://discord.gg/nhQehdv)</center><center><sup>If you would like to opt out of receiving comments reply with `STOP`</sup></center>
properties (22)
authorqurator-tier-0
permlinkre-ionic-tutorial-2-barcode-scanning-20180530t100229
categoryutopian-io
json_metadata""
created2018-05-30 10:02:30
last_update2018-05-30 10:02:30
depth1
children0
last_payout2018-06-06 10:02:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length473
author_reputation59,310,010,441
root_title"[Ionic Tutorial #2] - Barcode Scanning"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id58,417,972
net_rshares0