使用 Phonegap 和 JavaScript 解码二维码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17878566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Decode qrcode with Phonegap and JavaScript
提问by user2549538
I am showing a button to open camera using phonegap :
我正在显示一个使用 phonegap 打开相机的按钮:
document.addEventListener("deviceready", loaded, false);
function loaded() {
pictureSource = navigator.camera.PictureSourceType;
destinationType = navigator.camera.DestinationType;
}
function capturePhoto() {
navigator.camera.getPicture(getPhoto, onFail, {
quality : 50
});
}
function getPhoto(imageData) {
alert(imageData);
var smallImage = document.getElementById('cameraPic');
smallImage.style.display = 'block';
smallImage.src = "data:image/jpeg;base64," + imageData;
}
function onFail(message) {
alert('Failed because: ' + message);
}
<body>
<div id="camera">
<button class="camera-control" onclick="capturePhoto();">CapturePhoto</button>
<div style="text-align: center; margin: 20px;">
<img id="cameraPic" src="" style="width: auto; height: 120px;"> <img>
</div></div>
</body>
On click of button i want to decode a QR code and get show the decoded value on my page. I want to do this using javascript and phonegap only and dont want to use any native code.
单击按钮时,我想解码一个二维码并在我的页面上显示解码后的值。我只想使用 javascript 和 phonegap 来做到这一点,不想使用任何本机代码。
回答by avi
There is an official plugin for that... check Barcode Scanner
有一个官方插件......检查条形码扫描仪
回答by edi9999
There's a Library that decodes Qr-Code in Javascript only I have used: https://github.com/LazarSoft/jsqrcode
有一个库可以在 Javascript 中解码 Qr 代码,只有我使用过:https: //github.com/LazarSoft/jsqrcode
As it is a Javascript only solution, it should work on phonegap too.
由于它是仅 Javascript 的解决方案,因此它也应该适用于 phonegap。
There's an online Demo here: http://webqr.com/
这里有一个在线演示:http: //webqr.com/
jsqrcode works with Data-URIs, so you could use it like this:
jsqrcode 与数据 URI 一起使用,因此您可以像这样使用它:
qrcode.decode("data:image/jpeg;base64," + imageData);
In your code:
在您的代码中:
function getPhoto(imageData) {
alert(imageData);
var smallImage = document.getElementById('cameraPic');
smallImage.style.display = 'block';
qrcode.callback=function(){alert('Decoded:'+this.result)};
qrcode.decode("data:image/jpeg;base64," + imageData);
}