Decoding qrcode using Phonegap and JavaScript

I show a button to open the camera using 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> 

When I click the button, I want to decode the QR code and get the decoded value shown on my page. I want to do this using only javascript and phonegap, and don't want to use any native code.

+6
source share
2 answers

There is an official plugin for this ... check Barcode Scanner

+8
source

There is a library that decrypts QR code only in Javascript, I used: https://github.com/LazarSoft/jsqrcode

Since this solution is only for Javascript, it should also work with telephony.

Here's an online demo: http://webqr.com/

jsqrcode works with a Data-URI, so you can use it like this:

 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); } 
+7
source

Source: https://habr.com/ru/post/950341/


All Articles