Skip to content

Instantly share code, notes, and snippets.

@liketaurus
Created May 3, 2019 15:10
Show Gist options
  • Save liketaurus/740bde966832947b39afe06d58e6114d to your computer and use it in GitHub Desktop.
Save liketaurus/740bde966832947b39afe06d58e6114d to your computer and use it in GitHub Desktop.
How to scan QR-code using JavaScript
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://rawgit.com/schmich/instascan-builds/master/instascan.min.js"></script>
<video id="preview" height="160" style="border: 1px solid black"></video>
<p id="results"></p>
<script type="text/javascript">
let scanner = new Instascan.Scanner({ video: document.getElementById('preview') });
scanner.addListener('scan', function (content) {
$('#results').html(content);
});
Instascan.Camera.getCameras().then(function (cameras) {
if (cameras.length > 0) {
scanner.start(cameras[0]);
} else {
alert('No cameras found.');
}
}).catch(function (e) {
alert(e);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment