QR Code Scanner HTML5

by Rapha Souza

HTML

<script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5-qrcode/2.2.0/html5-qrcode.min.js"></script>
<h1>QR Code Reader using Javascript</h1>

<!-- QR SCANNER CODE BELOW  -->
<div class="row">
  <div class="col">
    <div id="reader"></div>
  </div>
  <div class="col" style="padding: 30px">
    <h4>Retorno </h4>
    <div id="result">
      Resultado
    </div>
  </div>

</div>

<div class="row">

<button type="button" class="btn btn-info" onclick="chama()">
Teste
</button>

</div>

JavaScript

// When scan is successful fucntion will produce data
function onScanSuccess(qrCodeMessage) {
  document.getElementById("result").innerHTML =
    '<span class="result">' + qrCodeMessage + "</span>";
}

// When scan is unsuccessful fucntion will produce error message
function onScanError(errorMessage) {
  // Handle Scan Error
}


// Setting up Qr Scanner properties
var html5QrCodeScanner = new Html5QrcodeScanner("reader", {
  fps: 10,
  qrbox: 250
});

function chama() {
  // in
  html5QrCodeScanner.render(onScanSuccess, onScanError);

}