2
votes

I have a problem with the barcode scanner plugin (I'm not a genius and I don't know well how to programm a web-app.).
I use phonegap and cordova and I've tried to do a web-app that scan a barcode after a click on a link.
I've installed the plugin, before with this command line:

cordova plugin add cordova-plugin-barcodescanner

and I write this js code:

function scan(){
 cordova.plugins.barcodeScanner.scan(
  function (result) {
      alert("We got a barcode\n" +
            "Result: " + result.text + "\n" +
            "Format: " + result.format + "\n" +
            "Cancelled: " + result.cancelled);
  }, 
  function (error) {
      alert("Scanning failed: " + error);
  }
 );
}

in html:

<a href="javascript:void(0);" onclick="scan()">Scan</a>

But when I click on the link, It doesn't happen anything.
If I put an alert after: function scan(){, it show if I put an alert after: cordova.plugins.barcodeScanner.scan(, doesn't do anything.
So, I tried to uninstall the plugin and install it with this command line:

cordova plugin add com.phonegap.plugins.barcodescanner

With the same js and html code, but it doesn't work yet.
So, I uninstall the plugin and I try to install it with:

cordova plugin add https://github.com/wildabeast/BarcodeScanner.git

But anything yet. I search a lot and I try a lot of solutions!
I use cordova 5.0.0 and I try the plugin on android 4.4 and IOS.
I also installed the plugin camera. So, please, help me! Where is the error?
I followed all the solutions that I've find on the web and on stackoverflow.
The code always crashes after:

cordova.plugins.barcodeScanner.scan(

Can anyone help me, please? Thank you so much.

2
Are you waiting for the cordova deviceready event before you start your script?Joerg
Yes, I'm waiting for the cordova deviceready event before start my script.Giulia
Write your plugin code in onDeviceReady event, Make sure you had include cordova.js in html file.Dilip Suvagiya
Have we got to put the function scan() into onDeviceReady? function onDeviceReady () { function scan(){ cordova.plugins.barcodeScanner.scan( function (result) { alert("We got a barcode\n" + "Result: " + result.text + "\n" + "Format: " + result.format + "\n" + "Cancelled: " + result.cancelled); }, function (error) { alert("Scanning failed: " + error); } ); } } Is it correct? I'm sure that I have included cordova.jsGiulia

2 Answers

0
votes

First of all can you check your code in firefox with firebug, see if it is giving some error which can let you to do so or you can try other library which is intelXDK

document.addEventListener("intel.xdk.device.barcode.scan",function(evt){});
0
votes

You have to add the feature to the config.xml in case of Android as below:

<feature name="BarcodeScanner">
    <param name="android-package" value="com.phonegap.plugins.barcodescanner.BarcodeScanner" />
</feature>