Skip to content

Instantly share code, notes, and snippets.

@macdonst
Created December 2, 2011 20:53
Show Gist options
  • Save macdonst/1424796 to your computer and use it in GitHub Desktop.
Save macdonst/1424796 to your computer and use it in GitHub Desktop.
BarcodeScanner Android Example
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap</title>
<link rel="stylesheet" href="master.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>
<script type="text/javascript" charset="utf-8" src="barcodescanner.js"></script>
<script type="text/javascript" charset="utf-8">
function init(){
console.log("GOT AN ONLOAD!!!")
document.addEventListener("touchmove", preventBehavior, false);
document.addEventListener("deviceready", deviceInfo, true);
}
var preventBehavior = function(e) {
e.preventDefault();
};
var deviceInfo = function(){
console.log("In deviceInfo!");
document.getElementById("platform").innerHTML = device.platform;
document.getElementById("version").innerHTML = device.version;
document.getElementById("uuid").innerHTML = device.uuid;
}
function scan() {
window.plugins.barcodeScanner.scan(function(result) {
alert("We got a barcode\n" +
"Result: " + result.text + "\n" +
"Format: " + result.format);
}, function(error) {
alert("Scanning failed: " + error);
});
}
function encode(type, data) {
window.plugins.barcodeScanner.encode(type, data, function(result) {
alert("encode success: " + result);
}, function(error) {
alert("encoding failed: " + error);
});
}
</script>
</head>
<body onload="init();" id="stage" class="theme">
<h1>Welcome to PhoneGap!</h1>
<h2>this file is located at assets/index.html</h2>
<div id="info">
<h4>Platform: <span id="platform">&nbsp;</span></h4>
<h4>Version: <span id="version">&nbsp;</span></h4>
<h4>UUID: <span id="uuid">&nbsp;</span></h4>
</div>
<a href="#" class="btn large" onclick="scan();">Scan</a>
<a href="#" class="btn large" onclick="encode(BarcodeScanner.Encode.TEXT_TYPE, 'http://www.phonegap.com');">Encode Text</a>
<a href="#" class="btn large" onclick="encode(BarcodeScanner.Encode.EMAIL_TYPE, '[email protected]');">Encode Email</a>
<a href="#" class="btn large" onclick="encode(BarcodeScanner.Encode.PHONE_TYPE, '1800551212');">Encode Phone</a>
</body>
</html>
@macdonst
Copy link
Author

macdonst commented May 8, 2012

Yeah, the success function of encode never gets called. The encode function never seems to call onActivityResult.

@vaibhavbparikh
Copy link

Exactly , i checked out the native code . it isn't calling onActivityResult.so it is not possible to get callback in encode function in android.

@azrashaikh
Copy link

how to display genetared QR code in our app... Plz help me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment