7

The code is as follows:

    import { Component } from '@angular/core';
    import { NavController, Platform } from 'ionic-angular';
    import { TranslateService } from '@ngx-translate/core';
    import { BarcodeScanner } from '@ionic-native/barcode-scanner';

    @Component({
        selector: 'page-home',
        templateUrl: 'home.html'
    })
    export class HomePage {
        constructor(public navCtrl: NavController, private translate: TranslateService, private barcodeScanner: BarcodeScanner) {

        }
        //Switch language 
       changelanguage() {
            let browserLang = this.translate.currentLang;
            this.translate.use(browserLang.match(/en/) ? 'zh' : 'en');
        }
        //Scan test
        ScanCode() {
            this.barcodeScanner.scan()
                .then((result) => {
                    console.log(result);
                })
                .catch((error) => {
                    alert(error);
                });
        }
    }

Error Picture:

enter image description here

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



new questions arose:
According to Suraj recommendations, the prompt "plugin_not_installed"

Error Picture:
enter image description here
enter image description here development environment: Visual Studio 2017

0

3 Answers 3

20

You need to set the scanner as provider in app.module.ts

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

@NgModule({
  ...

  providers: [
    ...
    BarcodeScanner
    ...
  ]
  ...
})
export class AppModule { }

Reference: Add Plugins to Your App's Module here

UPDATE: From your package.json it looks like you have installed older ionic-native - 2.4.1.Remove it and do:

npm install --save @ionic-native/core
5
  • 按你说的我已经加上了,但是调用方法提示 “plugin_not_installed” Commented Mar 31, 2017 at 8:18
  • catch((error) => { alert(error); }); Commented Mar 31, 2017 at 8:20
  • 运行到这里 提示 “plugin_not_installed” Commented Mar 31, 2017 at 8:20
  • 1
    $ ionic plugin add phonegap-plugin-barcodescanner --save $ npm install --save @ionic-native/barcode-scanner did you do this?
    – Suraj Rao
    Commented Mar 31, 2017 at 8:21
  • It's a good itea test the solutions, this doesn't work Commented Aug 23, 2020 at 3:27
4

I had the same issue with ionic cli 3.9.2.

I solved it using npm i @ionic-native/barcode-scanner@beta --save.

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

@NgModule({
  ...

  providers: [
    ...
    BarcodeScanner
    ...
  ]
  ...
})
export class AppModule { }
1

I got same issue at android device after installing ionic cordova plugin add phonegap-plugin-barcodescanner --save which error message is plugin_not_installed. Just added CAMERA_USAGE_DESCRIPTION variable inside phonegap-plugin-barcodescanner tag at config.xml and working :)

<plugin name="phonegap-plugin-barcodescanner" spec="^7.0.1">
    <variable name="CAMERA_USAGE_DESCRIPTION" value=" " />
</plugin>

Not the answer you're looking for? Browse other questions tagged or ask your own question.