Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #7 from JaysonRaymond/master
Browse files Browse the repository at this point in the history
Updates for Cordova/PhoneGap's 2.0 plugins
  • Loading branch information
Ryan Willoughby committed Mar 5, 2013
2 parents bb3812c + c7158af commit 3c73e21
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 67 deletions.
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ Cross-platform BarcodeScanner for Cordova / PhoneGap.

Follows the [Cordova Plugin spec](https://github.com/alunny/cordova-plugin-spec), so that it works with [Pluginstall](https://github.com/alunny/pluginstall).

This plugin leverages Cordova/PhoneGap's [require/define functionality used for plugins](http://simonmacdonald.blogspot.ca/2012/08/so-you-wanna-write-phonegap-200-android.html).

Note: the Android source for this project includes an Android Library Project.
pluginstall currently doesn't support Library Project refs, so its been
prebuilt as a jar library. Any updates to the Library Project should be
committed with an updated jar.

## Using the plugin ##
The plugin creates the object `window.plugins.barcodeScanner` with the method `scan(success, fail)`.
The following barcode types are currently supported:
The plugin creates the object `cordova/plugin/BarcodeScanner` with the method `scan(success, fail)`.

The following barcode types are currently supported:
### Android

* QR_CODE
Expand Down Expand Up @@ -46,15 +48,20 @@ The following barcode types are currently supported:

A full example could be:

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);
}
);
var scanner = window.PhoneGap.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);
}
);


## Encoding a Barcode ##
The plugin creates the object `window.plugins.barcodeScanner` with the method `encode(type, data, success, fail)`.
Expand All @@ -67,7 +74,9 @@ Supported encoding types:

A full example could be:

window.plugins.barcodeScanner.encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) {
var scanner = window.PhoneGap.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);
Expand Down
120 changes: 65 additions & 55 deletions www/barcodescanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,68 @@
* Copyright (c) 2011, IBM Corporation
*/

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 Javascriopt to Java
// LOCATION_TYPE: "LOCATION_TYPE" // TODO: not implemented, requires passing a Bundle class from Javascriopt 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
}

cordova.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.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
}

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

//-------------------------------------------------------------------

if(!window.plugins) {
window.plugins = {};
}
if (!window.plugins.barcodeScanner) {
window.plugins.barcodeScanner = new BarcodeScanner();
}

PhoneGap.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.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', 'encode', [
{"type": type, "data": data, "options": options}
]);
};

var barcodeScanner = new BarcodeScanner();
module.exports = barcodeScanner;
});

0 comments on commit 3c73e21

Please sign in to comment.