使用Phonegap和JavaScript解码qrcode

use*_*538 6 javascript qr-code cordova

我正在显示一个使用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>
Run Code Online (Sandbox Code Playgroud)

点击按钮我想解码一个二维码,并在我的页面上显示解码值.我想只使用javascript和phonegap这样做,不想使用任何本机代码.

avi*_*avi 8

有一个官方插件...检查条码扫描器


edi*_*999 7

有一个库只用我在Javascript中解码Qr-Code:https://github.com/LazarSoft/jsqrcode

由于它是一个仅限Javascript的解决方案,它也应该适用于phonegap.

这里有一个在线演示:http://webqr.com/

jsqrcode与Data-URI一起使用,因此您可以像这样使用它:

qrcode.decode("data:image/jpeg;base64," + imageData);
Run Code Online (Sandbox Code Playgroud)

在你的代码中:

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);
    }
Run Code Online (Sandbox Code Playgroud)