2
votes

I am try to scan barcode using barcode scanner in android mobile using ionic2. I am a new learner of ionic. I tried some code,

home.ts

  import { Component } from '@angular/core';

  import { NavController } from 'ionic-angular';
  declare var cordova:any;
  @Component({
  selector: 'page-home',
  templateUrl: 'home.html'
  })
  export class HomePage {

  constructor(public navCtrl: NavController) {

  }

  scan() {
  var me = this;
  if (cordova.plugins.barcodeScanner) {
    cordova.plugins.barcodeScanner.scan((imageData) => {
        alert(imageData.text);

    }, (error) => {
        alert("An error happened -> " + error);         
    });
  }

  }
  }

home.html

  <ion-navbar *navbar>
  <ion-title>
    Home
  </ion-title>
  </ion-navbar>

 <ion-content class="home">
 <button primary (click)="scan()">Scan</button>
 </ion-content>

But I got an error is 'Cannot find variable: Cordova'.

I used this plugin -

https://ionicframework.com/docs/v2/native/barcode-scanner/

for barcode scanner in ionic2

Thank you for the answer.

7
Do you still get the error after making the mentioned changes? Build the application by typing "ionic build android" at the command prompt. Then run the apk file on your phone. It's normal to get this error in the browser.onurssaral

7 Answers

2
votes

You need to add:

import { BarcodeScanner } from 'ionic-native';

after that you can use BarcodeScanner.scan() methods to get the information from a barcode or qr code. Try this code: home.ts

import { Component } from '@angular/core';    
import { NavController } from 'ionic-angular';
import { BarcodeScanner } from 'ionic-native';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController) {}

  click() {
    BarcodeScanner.scan()
      .then((result) => {
        alert(
          "We got a barcode\n" +
          "Result: " + result.text + "\n" +
          "Format: " + result.format + "\n" +
          "Cancelled: " + result.cancelled
        )
      })
      .catch((error) => {
        alert(error);
      })
  }
}

home.html

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <h1>Scan</h1>
  <button block (click)="click()" color="primary">Scan</button>
</ion-content>
1
votes

First do import to your project:

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

Component files:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers: [ BarcodeScanner ]
})
export class HomePage {

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

  }

  scan() {
   this.barcodeScanner.scan().then((barcodeData) => {
        console.log(JSON.stringify(barcodeData))
      }, (err) => {
       //error
      });
  }

}
1
votes

1.

$ cordova plugin add phonegap-plugin-barcodescanner

$ npm install --save @ionic-native/barcode-scanner

2.

app.module.ts

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

   ......

   providers: [
     BarcodeScanner,
     {provide: ErrorHandler, useClass: IonicErrorHandler}
   ]
 })
 export class AppModule {}

scanner.ts

 import { Component } from '@angular/core';
 import { NavParams, NavController } from 'ionic-angular';
 import { BarcodeScanner } from '@ionic-native/barcode-scanner';

 @Component({
    selector: 'page-contact',
    templateUrl: 'contact.html'
 })
export class ContactPage {
    constructor( public nacCtrl: NavController,
    public NavParams: NavParams,
    private barcodeScanner: BarcodeScanner) { }

  takeScan(){
      this.barcodeScanner.scan().then((barcodeData) => {
      // Success! Barcode data is here
      alert(barcodeData.text);
      console.log("Barcode Format -> " + barcodeData.format);
      console.log("Cancelled -> " + barcodeData.cancelled);

   }, (err) => {
      // An error occurred
     console.log("An error happened -> " + err);
   });
 }

}
0
votes

As in the docs you need to do this to use barcode scanner with ionic 2

import { BarcodeScanner } from 'ionic-native';

BarcodeScanner.scan().then((barcodeData) => {
    // Success! Barcode data is here
  }, (err) => {
   // An error occurred
});

After installing, you need to import it in your page from ionic-native and then call it to use its functions.

Hope it helps :D

0
votes

For using Barcode Scanner you have to add

import { BarcodeScanner } from 'ionic-native';

and create a function like this

barcode() {
  BarcodeScanner.scan()
  .then((result) => {
     if (!result.cancelled) {
       alert("Success"+result);
     }
  })
  .catch((err) => {
    alert(err); 
   })
 }

now you can call barcode() function anywhere in html to scan

0
votes

1.Add the plugin to your app see below

ionic plugin add phonegap-plugin-barcodescanner

npm install --save @ionic-native/barcode-scanner

2.import

import { BarcodeScanner } from 'ionic-native';

import * as xml2js from "xml2js";

  1. BarcodeScanner.scan().then((barcodeData) => {

var name,uid;

xml2js.parseString(barcodeData.text,{trim: true}, function (err, result) { debugger;

name=result.PrintLetterBarcodeData.$.name; uid=result.PrintLetterBarcodeData.$.uid; });

debugger; this.AdharCardName=name; this.AdharcardUid=uid;
}, (err) => {

});

0
votes
How to create perfect Barcode/QR Scanner. let's do it.

firstly,install

  1. ionic cordova plugin add phonegap-plugin-barcodescanner
  2. npm install --save @ionic-native/barcode-scanner
  3. npm install @ionic-native/core --save
  4. cordova platform update android

all are done..import BarcodeScanner in app.module.ts .like this
import { BarcodeScanner } from '@ionic-native/barcode-scanner';

providers: [ BarcodeScanner ]

after that, we must declare in typescript file which you'll use
import { BarcodeScanner } from '@ionic-native/barcode-scanner';

scan(){
      this.barcodeScanner.scan().then((barcodeData) => {
        console.log(JSON.stringify(barcodeData))  
      }, (err) => {  
       //error 
      });
    }

in this case. you can face like this errors,
ERR_FILE_NOT_FOUND (file:///android_asset/www/index.html): or
*Unexpected token ) in main.ts *
don't worry about it..remove android/ios plugin from command prompt. and install it.
cordova platform update android

I hope good new. spy