Steroids Barcode Scanner Plugin Tutorial (working)


#1

I spent weeks trying to figure this out on Android and finally Harri from AppGyver gave me some help this morning.

You don’t need to add any plugins to the plugin section. Scanner is a default element of the scanner build.

The following code works both in iOS and Android.

Here are the steps that works:

Go to Build Service and Create a Scanner Build.
Read More about Build Here.

Cut and Paste the following code and call the ScanBarCode() function from a button click.

function ScanBarCode()
{   
    var barcodeScanner = null;

        try{
                barcodeScanner = cordova.plugins.barcodeScanner;
            }catch(err){}

        if(! barcodeScanner)
            {

//For Android
                  steroids.nativeBridge.nativeCall({
                  method: "scanBarCode",
                  successCallbacks:[onSuccess],
                  failureCallbacks:[onScanError],
                  recurringCallbacks: [onScanResults]
                });
            }
        else
            {

//For iOS
                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);
                  }
               );
            }
    }

function onSuccess(result)
    {
        alert("Success!");
    }

    function onScanResults(result)
    {
        alert("We got a barcode\n" +
                            "Result: " + result.text + "\n" +
                            "Format: " + result.format + "\n" +
                            "Cancelled: " + result.cancelled);
    }

function onScanError(result)
    {
        alert("Error" + result);
    }

#2

Great, thanks for this! :slight_smile: