Installing Phonegap/Cordova 3.1 plugins (barcodescanner)

11,968

Yes, You dont need to import any plugin specific javascript file in your index.html. Just Verify that plugin in properly installed in your project by confirming YourProject/res/config.xml file has the following entry:

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

For using the plugin, just use the updated syntax of calling the plugin functions -

function clickScan() {
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);
  });}
Share:
11,968
Aaron Fisher
Author by

Aaron Fisher

Web designer &amp; developer. Podcaster at http://munchtech.tv. Tech &amp; Motorsport!

Updated on June 29, 2022

Comments

  • Aaron Fisher
    Aaron Fisher almost 2 years

    Been trying this for a few hours now and have made a little progress but not in the right direction.

    I have successfully setup an Android Cordova project which loads onto a phone and runs fine. I just cannot get the barcode scanner plugin to work in Cordova 3.1. I believe it has installed correctly but it does not appear in the config.xml, it does however appear in the cordova_plugins.js file etc.

    I have this in my index.js

    function clickScan() {
        var scanner = cordova.require("com.phonegap.plugins.barcodescanner.BarcodeScanner");
        scanner.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);
            }
       );
    }
    

    Now when I press the scan button it seems to run this code but jumps straight to the success function and just displays the alert box with blank results.

    The scanner I am using and have installed via cordova plugin add is https://github.com/wildabeast/BarcodeScanner

    I am not currently importing the barcodescanner.js file into the html as I have done with older versions of cordova as I believe this is handled differently in 3+ and seems to be defined in the cordova_plugins.js file?

    Update: As far as I am aware with the config above there does not seem to be any glaring errors popup in Eclipse.

  • Aaron Fisher
    Aaron Fisher over 10 years
    Upon double checking I had it in the Res folder I realised that I had missed some steps that are not immediately obvious. I needed to run the build command from the root folder and that would push the plugin to all platforms, thanks for your help!
  • Dan Schien
    Dan Schien over 10 years
    This does not work for me with cordova 3.3.1-0.1.2. What exactly do you mean with updated syntax? Is this an undocumented change? If so, could you point me to the source? In the developer docs, the syntax is cordova.exec('<plugin.ref>') cordova.apache.org/docs/en/3.3.0/…
  • netalex
    netalex about 10 years
    i think it will refers to barcodescanner api syntax: it passed from var scanner = window.cordova.require("cordova/plugin/BarcodeScanner"); scanner.scan(function (result) {}, function (error) {}); } to simple window.plugins.barcodeScanner, and finally to cordova.plugins.barcodeScanner