Reputation: 135
I'm currently trying to implement barcodescanner in my phonegap project. But I'm completly lost because I read a lot of topics about barecodescanner and all possible solutions provided didn't work for me.
First, some tutorials and documents says that I have to use cordova.plugin.barcodeScanner.scan (...). But for me, cordova.plugin is always undefined.
Some others says that I have to do cordova.require("cordova/plugin/BarcodeScanner"); and it doesn't work, when I run my app, I get the following error : "module "cordova/plugin/BarcodeScanner" not found.
Upvotes: 3
Views: 575
Reputation: 36
If you use PhoneGap Build... This is an example of implementation...
In the config.xml file add this line:
<!-- We'll include the Barcode plugin -->
<gap:plugin name="com.phonegap.plugins.barcodescanner" />
Then in the index.html file:
<script type="text/javascript">
function Scan() {
cordova.plugins.barcodeScanner.scan(
function (result) {
window.open(result.text,'_self', 'location=no') //Opens URL in browser
//alert("We got a barcode\n" +
// "Result: " + result.text + "\n" +
// "Format: " + result.format + "\n" +
// "Cancelled: " + result.cancelled);
},
function (error) {
alert("Scanning failed: " + error);
}
);
}
</script>
To call the script with a button in the body section of page:
<button onclick="Scan()">Barcode</button>
Good luck!
Upvotes: 2