[BarcodeScanner Plugin] Upgrading scanner JavaScript API, code changes required

  • 1
  • Announcement
  • Updated 6 years ago
The BarcodeScanner Plugin on PhoneGap Build will be getting an update today, and apps using it will need to change their code to use cordova.require:

Old:
window.plugins.barcodeScanner.scan(function(){ ... }, function(){ ... }, optionsObj)

New:
var scanner = cordova.require("cordova/plugin/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);
}
);


More info:
https://github.com/phonegap/phonegap-...
http://simonmacdonald.blogspot.ca/201...

Will update this thread once the the new version is deployed.
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes

Posted 7 years ago

  • 1
Photo of Red2678

Red2678

  • 255 Posts
  • 0 Reply Likes
Yay!
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
Just realized I never updated this thread, thanks for the ping. The new version did get deployed. Current version is tag 0.5.2 of https://github.com/phonegap-build/Bar....
Photo of Hal DeLage

Hal DeLage

  • 7 Posts
  • 0 Reply Likes
That did the trick. I owe ya a beer!!!
Photo of pistoleta

pistoleta

  • 69 Posts
  • 1 Reply Like
so do we need to update to the new code even if we are not using the new 2.7.0 phonegap version?

Thanks
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
yes
Photo of pistoleta

pistoleta

  • 69 Posts
  • 1 Reply Like
omg, thanks god im pretty linked to the community.... will they do something to prevent this kind of thing again?
Photo of solrak64

solrak64

  • 1 Post
  • 0 Reply Likes
thanks for the advice
Photo of pistoleta

pistoleta

  • 69 Posts
  • 1 Reply Like
yeah thanks all
Photo of Jay

Jay

  • 8 Posts
  • 0 Reply Likes
Thanks a bunch!
I'm so glad I found this!
Photo of Vagner Bertoni

Vagner Bertoni

  • 27 Posts
  • 0 Reply Likes
Hello @ryan, i added the barcodescanner.js in www path, in head
but still shows me the error: module cordova/plugin/BarcodeScanner not found when run the command:
var scanner = cordova.require("cordova/plugin/BarcodeScanner");

i dont know what to do to solv this issue

barcodescanner.js bellow

cordova.define("cordova/plugin/BarcodeScanner",

function (require, exports, module) {

var exec = require("cordova/exec");

var BarcodeScanner = function () {
};

//-------------------------------------------------------------------
BarcodeScanner.Encode = {
TEXT_TYPE: "TEXT_TYPE",
EMAIL_TYPE: "EMAIL_TYPE",
PHONE_TYPE: "PHONE_TYPE",
SMS_TYPE: "SMS_TYPE",
// CONTACT_TYPE: "CONTACT_TYPE", // TODO: not implemented, requires passing a Bundle class from Javascript to Java
// LOCATION_TYPE: "LOCATION_TYPE" // TODO: not implemented, requires passing a Bundle class from Javascript to Java
};

//-------------------------------------------------------------------
BarcodeScanner.prototype.scan = function (successCallback, errorCallback) {
if (errorCallback == null) {
errorCallback = function () {
}
}

if (typeof errorCallback != "function") {
console.log("BarcodeScanner.scan failure: failure parameter not a function");
return
}

if (typeof successCallback != "function") {
console.log("BarcodeScanner.scan failure: success callback parameter must be a function");
return
}

exec(successCallback, errorCallback, 'BarcodeScanner', 'scan', []);
};

//-------------------------------------------------------------------
BarcodeScanner.prototype.encode = function (type, data, successCallback, errorCallback, options) {
if (errorCallback == null) {
errorCallback = function () {
}
}

if (typeof errorCallback != "function") {
console.log("BarcodeScanner.encode failure: failure parameter not a function");
return
}

if (typeof successCallback != "function") {
console.log("BarcodeScanner.encode failure: success callback parameter must be a function");
return
}

exec(successCallback, errorCallback, 'BarcodeScanner', 'encode', [
{"type": type, "data": data, "options": options}
]);
};

var barcodeScanner = new BarcodeScanner();
module.exports = barcodeScanner;
});
Photo of Vagner Bertoni

Vagner Bertoni

  • 27 Posts
  • 0 Reply Likes
completing the information, I downloaded the apk and unziped, barcodescanner.js is in www folder
Photo of Nux

Nux

  • 16 Posts
  • 0 Reply Likes
Please update the docs! I've been pulling my hair all day :-/...

The docs:
https://build.phonegap.com/docs/plugi...
https://github.com/phonegap/phonegap-...
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
@Vagner please share your app ID.

@Maclej the plugins docs page you mention references https://github.com/wildabeast/Barcode..., which does contain up to date api documentation ...
Photo of Nux

Nux

  • 16 Posts
  • 0 Reply Likes
You are right, thanks. I had the second one bookmarked probably because it contains more information about functions and types. Maybe you could cross update.
https://github.com/phonegap/phonegap-...
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
Yeah phonegap-plugins has essentially been deprecated, and plugins have (or haven't but should have) been broken out into individual repositories managed by the individual plugin maintainers. Easy to confuse, should be clarified.
Photo of Vagner Bertoni

Vagner Bertoni

  • 27 Posts
  • 0 Reply Likes
@ryan, i have an test app in #439489
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
Vagner after a quick look at your app, the first thing I see is this:

<script src="cordova.js"></script>
<script src="barcodescanner.js"></script>
<script src="phonegap.js"></script>

cordova.js and phonegap.js are the same source file -- you should not have both. What might be happening here is you include cordova.js, then include barcodescanner which defines cordova/plugins/barcodescanner, and then you include phonegap.js which likely clears that module.

Remove
<script src="phonegap.js"></script>
and get back to us.
Photo of Vagner Bertoni

Vagner Bertoni

  • 27 Posts
  • 0 Reply Likes
@ryan, i just forgot in my test, it was junk, i did tested without phonegap.js, just for continue, i removed and tested again, same error, it is published again
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
I just looked at your app, you still have phonegap.js. I verified that that is in fact the problem.
Photo of Vagner Bertoni

Vagner Bertoni

  • 27 Posts
  • 0 Reply Likes
@ryan I use a framework to send my application to build PhoneGap framework and this is always sending this file. Even so, I downloaded the file android, apk, removed the phonegap.js and installed on the device. The error message does not appear anymore, the screen opens the camera, but the application closes.
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
Here is a working version of your app:
https://build.phonegap.com/apps/44582...

The only difference between that and yours is that I removed the following from the index.html:

<script src="phonegap.js"></script>
Photo of Vagner Bertoni

Vagner Bertoni

  • 27 Posts
  • 0 Reply Likes
thanks @ryan!, i will check with framework company
Photo of Nux

Nux

  • 16 Posts
  • 0 Reply Likes
Hi all you can use my simple mock to keep away from troubles ;-). Works with new and old style.
http://mjappis.org/download/libs/code...

And here is a widget definition for jQuery Mobile which makes using it much simpler:
http://mjappis.org/download/libs/jque...
Just use input type="codeScanner" and JQM will render input with scanner button.
Photo of rene.vaessen

rene.vaessen

  • 114 Posts
  • 4 Reply Likes
Ok, don't forget to update the documentation ;)
Photo of Danny Haggard

Danny Haggard

  • 2 Posts
  • 0 Reply Likes
I am getting to following errors when I try to update the plugin. I am confused because I thought this update was changing the new plugin structure of 2.7.
What am I missing?

Description Resource Path Location Type
The method error(PluginResult, String) is undefined for the type BarcodeScanner BarcodeScanner.java /Inventory/src/com/phonegap/plugins/barcodescanner line 135 Java Problem
cordova cannot be resolved or is not a field BarcodeScanner.java /Inventory/src/com/phonegap/plugins/barcodescanner line 150 Java Problem
The method success(PluginResult, String) is undefined for the type BarcodeScanner BarcodeScanner.java /Inventory/src/com/phonegap/plugins/barcodescanner line 123 Java Problem
The method success(PluginResult, String) is undefined for the type BarcodeScanner BarcodeScanner.java /Inventory/src/com/phonegap/plugins/barcodescanner line 133 Java Problem
Plugin cannot be resolved to a type BarcodeScanner.java /Inventory/src/com/phonegap/plugins/barcodescanner line 101 Java Problem
cordova cannot be resolved or is not a field BarcodeScanner.java /Inventory/src/com/phonegap/plugins/barcodescanner line 101 Java Problem
Plugin cannot be resolved to a type BarcodeScanner.java /Inventory/src/com/phonegap/plugins/barcodescanner line 25 Java Problem
The import org.apache.cordova.api.Plugin cannot be resolved BarcodeScanner.java /Inventory/src/com/phonegap/plugins/barcodescanner line 19 Java Problem
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
I take it you are building locally, and not on PhoneGap Build? This is a discussion better suited for the BarcdodeScanner github repo rather than this (PhoneGap Build) forum.

Anyways BarcodeScanner hasn't yet been updated to use the CordovaPlugin class rather than the Plugin class. This works on build because we patched it. I'm hoping to update the BarcodeScanner plugin soon -- but feel free to contribute if you want to accelerate this.
Photo of Austyn Pember

Austyn Pember

  • 1 Post
  • 0 Reply Likes
Any Idea when this is going to happen?
Photo of Paelo Huelo

Paelo Huelo

  • 1 Post
  • 0 Reply Likes
Instructions in original topic no longer works after upgrading this whole plugin scheme (effective as of today) !?
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
Plugins currently failing, working on it, watch here:
http://community.phonegap.com/nitobi/...
Photo of ColinBau

ColinBau

  • 730 Posts
  • 18 Reply Likes
I am using phonegap 2.9 with BarcodeScanner 0.60 plugin problem

I compiled through build.phonegap.com
When I use phonegap 2.5 version,The BarcodeScanner plugin,everything is work(below the old code)

Scan(work)
window.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);
}
);

Encode(work)
window.plugins.barcodeScanner.encode(BarcodeScanner.Encode.TEXT_TYPE, "abc", function(success) {
alert("encode success: " + success);
}, function(fail) {
alert("encoding failed: " + fail);
}
);

But now,I use phonegap 2.9 version,The BarcodeScanner plugin,just Encode function was broken(below the new code)
here is the chrome's wrong message
http://www.littlebau.com/barcode.jpg

and I follow the latest document
https://github.com/phonegap-build/Bar...

Scan(work)
var scanner = cordova.require("cordova/plugin/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);
}
);

Encode(not work)
var scanner = cordova.require("cordova/plugin/BarcodeScanner");
scanner.encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) {
alert("encode success: " + success);
}, function(fail) {
alert("encoding failed: " + fail);
}
);

how to fix it,please help~
Photo of Nux

Nux

  • 16 Posts
  • 0 Reply Likes
change: scanner.encode(BarcodeScanner.Encode.TEXT_TYPE
to: scanner.encode(scanner.Encode.TEXT_TYPE)
Photo of ColinBau

ColinBau

  • 730 Posts
  • 18 Reply Likes
Thank's for Nux reply
No mater is "BarcodeScanner.Encode" or "scanner.Encode"
But the result is the same
http://www.littlebau.com/barcode.jpg
Photo of Nux

Nux

  • 16 Posts
  • 0 Reply Likes
I see you have set up console access. Why not just try `console.log(scanner)`. You could also just pass 'TEXT_TYPE' (string).

You could also use JavaScript libraries for encoding text to QR code (which I assume this function does). Personally I use jquery-qrcode by jeromeetienne.
Photo of ColinBau

ColinBau

  • 730 Posts
  • 18 Reply Likes
Thank's for Nux reply again

But I do not understand is

When I use an "earlier version" of barcode Scanner "2.20"
https://github.com/phonegap/phonegap-...
this old code got work, in the real machine can also be used normally
window.plugins.barcodeScanner.encode (BarcodeScanner.Encode.TEXT_TYPE, ....

But I'm using the "new version" of barcode Scanner "3.00"
https://github.com/wildabeast/Barcode...
this new code can't work, so can not properly use the real machine
http://www.littlebau.com/barcode.jpg
(cannot read property 'TEXT_TYPE' of undefined)
scanner.encode (BarcodeScanner.Encode.TEXT_TYPE, .....

Old version can understand TEXT_TYPE, but instead the new version can not read

=====
What you are using the "latest version"(3.00) of barcode scanner ??
if yes~please tell me how to do
Photo of R3dRuM

R3dRuM

  • 15 Posts
  • 0 Reply Likes
hey everyone,

I'm playing around with phonegap for a couple of days now. Started to use the barcodescanner today. ok... at least I tried to.

I'm using


<preference name="phonegap-version" value="2.9.0" />

and tried to include the scanner with


<gap:plugin name="com.phonegap.plugins.barcodescanner" />

using phonegap build.

In my index.html I put a reference to



<script src="barcodescanner.js"></script>


but when I try to access the plugin I just don't get a scanner


var scanner;
// various versions just in case...
if ('plugins' in window && 'barcodeScanner' in window.plugins) {
scanner = window.plugins.barcodeScanner;
}
else if ('barcodeScanner' in window) {
scanner = window.plugins.barcodeScanner;
}
else if ('cordova' in window) {
scanner = cordova.require("cordova/plugin/BarcodeScanner");
}

// always getting here......
if (scanner != null) {
log("couldn't find any scanner");
}


what am I doing wrong?
Photo of ColinBau

ColinBau

  • 730 Posts
  • 18 Reply Likes
I posted above articles are old barcode version method can be used normally
what version do you use??
Different from the old and new versions of the wording
now, The New version of the encoding method have some trouble
I am tried too..
Photo of Nux

Nux

  • 16 Posts
  • 0 Reply Likes
@Peter Don't know why it's not working for you. Here is a very simple working app, which you can use to figure out:
https://github.com/Eccenux/PP-tracker...
Photo of ColinBau

ColinBau

  • 730 Posts
  • 18 Reply Likes
I just watch this APP's index.html
While the app using a barcode scanner 0.60 version
But the code does not use to encode related functions ...
Photo of R3dRuM

R3dRuM

  • 15 Posts
  • 0 Reply Likes
Thanks Nux. I made a pull of you repo. zipped the app folder and put it up to phonegap build.
getting the same error with your projekt saying "Class not found"
Photo of Darnell Ford

Darnell Ford

  • 0 Posts
  • 0 Reply Likes
Hey Nux,
I had the "Class Not Found" error as well. Have you tried completely redownloading and installing your app. I mean like scanning the barcode and everything, not just reinstalling the already downloaded file.

That was my problem. Hope that helps.
Photo of Darnell Ford

Darnell Ford

  • 0 Posts
  • 0 Reply Likes
I get the error message: "Scanning Failed: Class Not Found" when I try to run the scanner code. Any thoughts?