Detect QR Code From an Image using Javascript

function base64ImageToBlob(str) {
// extract content type and base64 payload from original string
var pos = str.indexOf(‘;base64,’);
var type = str.substring(5, pos);
var b64 = str.substr(pos + 8);
// decode base64
var imageContent = atob(b64);
// create an ArrayBuffer and a view (as unsigned 8-bit)
var buffer = new ArrayBuffer(imageContent.length);
var view = new Uint8Array(buffer);
// fill the view, using the decoded base64
for(var n = 0; n < imageContent.length; n++) {
view[n] = imageContent.charCodeAt(n);
}
// convert ArrayBuffer to Blob
var blob = new Blob([buffer], { type: type });
return blob;
}

function getQRCode(imageBase64) {
var imageBlob = base64ImageToBlob(imageBase64);

const html5QrCode = new Html5Qrcode(“reader”);
const imageFile = imageBlob;

html5QrCode.scanFile(imageFile, false)
.then(qrCodeMessage => {
setQRCode(qrCodeMessage);
})
.catch(err => {
console.log(`Error scanning file. Reason: ${err}`)
});
}

function setQRCode(QRCode) {
alert(QRCode);
}

getQRCode(base64image);
if(!(t&&t instanceof File))throw”imageFile argument is mandatory and should be instance of File. Use ‘event.target.files[0]’”;
if(!(true))throw”imageFile argument is mandatory and should be instance of File. Use ‘event.target.files[0]’”;

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store