QR Code Scanning

Download javascript file from here and upload as static resource in your salesforce org with name ‘Html5QRCode’

Now create a visualforce page using below code-

<apex:page >
  <head>
    <title>Html-Qrcode Demo</title>
  
   <apex:includeScript value="{!$Resource.Html5QRCode}"/>
   <script>
       function docReady(fn) {
            // see if DOM is already available
            if (document.readyState === "complete" || document.readyState === "interactive") {
                // call on next available tick
                setTimeout(fn, 1);
            } else {
                document.addEventListener("DOMContentLoaded", fn);
            }
        } 
        
        docReady(function() {
            var resultContainer = document.getElementById('qr-reader-results');
            var lastResult, countResults = 0;
            
            var html5QrcodeScanner = new Html5QrcodeScanner(
                "qr-reader", { fps: 10, qrbox: 250 });
            
            function onScanSuccess(qrCodeMessage) {
                /*if (qrCodeMessage !== lastResult) {
                    ++countResults;
                    lastResult = qrCodeMessage;
                    resultContainer.innerHTML += `<div>[${countResults}] - ${qrCodeMessage}</div>`;
                    
                    // Optional: To close the QR code scannign after the result is found
                    html5QrcodeScanner.clear();
                }*/
                alert(qrCodeMessage);
                window.location = 'qrCodeMessage;
            }
            
            // Optional callback for error, can be ignored.
            function onScanError(qrCodeError) {
                // This callback would be called in case of qr code scan error or setup error.
                // You can avoid this callback completely, as it can be very verbose in nature.
            }
            
            html5QrcodeScanner.render(onScanSuccess, onScanError);
        });
      </script>
  </head>
  <body>
    <div id="qr-reader" style="width:500px"></div>
    <div id="qr-reader-results"></div>
  </body>
</apex:page>

Above code is self explanatory. Upon successful scanning, this code will redirect you the website related to your QR code.

Credit for this QRCode JS library goes to Minhazav

Published by Sandeep Kumar

He is a Salesforce Certified Application Architect having 11+ years of experience in Salesforce.

Leave a Reply