Use this lightweight library to easily / quickly integrate QR code, bar code, and other common code scanning capabilities to your web application. Show
Key highlights
Supports two kinds of APIs
Demo at scanapp.orgDemo at qrcode.minhazav.dev - Scanning different types of codes NoticeUX/UXD/UXR help wanted: We are looking to improve user experience of this code scanning library — if you are interested in making this experience better for both developers and end users, please share your ideas, thoughts, inputs in this discussion Supported platformsWe are working continuously on adding support for more and more platforms. If you find a platform or a browser where the library is not working, please feel free to file an issue. Check the demo link to test it out. Legends
PC / MacFirefox Chrome Safari Opera Edge AndroidChrome Firefox Edge Opera Opera Mini UC IOSSafari Chrome Firefox Edge**
Framework supportThe library can be easily used with several other frameworks, I have been adding examples for a few of them and would continue to add more. Html5VueJsElectronJsReactSupported Code formatsCode scanning is dependent on Zxing-js library. We will be working on top of it to add support for more types of code scanning. If you feel a certain type of code would be helpful to have, please file a feature request. CodeExampleQR CodeAZTECCODE_39CODE_93CODE_128ITFEAN_13EAN_8PDF_417UPC_AUPC_EDATA_MATRIXMAXICODE*RSS_14*RSS_EXPANDED*
Description - View Demo
This is a cross-platform JavaScript library to integrate QR code, bar codes & a few other types of code scanning capabilities to your applications running on HTML5 compatible browser. Supports:
How to useIf you are on medium- Thorough documentation on Html5-qrcode library on Medium. DocumentationDownload the script from release page or install using <script src="https://unpkg.com/html5-qrcode" type="text/javascript">7 with: npm i html5-qrcode Add an element you want to use as a placeholder for QR Code scanner <div id="reader" width="600px"></div>
Using directly in browser without any loaderIf you are not using any loader, you can get the latest UMD javascript code in production from https://unpkg.com/html5-qrcode. <script src="https://unpkg.com/html5-qrcode" type="text/javascript">
Using with module loadersInclude the script with // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode" Easy Mode - With end to end scanner user interface<script src="https://unpkg.com/html5-qrcode" type="text/javascript">5 lets you implement an end to end scanner with few lines of code with the default user interface which allows scanning using the camera or selecting an image from the file system. You can set up the scanner as follows: function onScanSuccess(decodedText, decodedResult) { // handle the scanned code as you like, for example: console.log(`Code matched = ${decodedText}`, decodedResult); } function onScanFailure(error) { // handle scan failure, usually better to ignore and keep scanning. // for example: console.warn(`Code scan error = ${error}`); } let html5QrcodeScanner = new Html5QrcodeScanner( "reader", { fps: 10, qrbox: {width: 250, height: 250} }, /* verbose= */ false); html5QrcodeScanner.render(onScanSuccess, onScanFailure); Pro Mode - if you want to implement your own user interfaceYou can use <script src="https://unpkg.com/html5-qrcode" type="text/javascript">6 class to set up your QR code scanner (with your own user interface) and allow users to scan QR codes using the camera or by choosing an image file in the file system or native cameras in smartphones. You can use the following APIs to // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"2, // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"3 scanning and // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"4 scanning. For using inline QR Code scanning with Webcam or Smartphone cameraStart ScanningTo get a list of supported cameras, query it using static method // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"5. This method returns a // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"6 with a list of devices supported in format // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"7. // This method will trigger user permissions Html5Qrcode.getCameras().then(devices => { /** * devices would be an array of objects of type: * { id: "id", label: "label" } */ if (devices && devices.length) { var cameraId = devices[0].id; // .. use this to start scanning. } }).catch(err => { // handle err }); Important: Note that this method will trigger user permission if the user has not granted it already.
Once you have the camera ID from // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"8, start camera using // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"9. This method returns a // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"6 with Qr code scanning initiation. const html5QrCode = new Html5Qrcode(/* element id */ "reader"); html5QrCode.start( cameraId, { fps: 10, // Optional, frame per seconds for qr code scanning qrbox: { width: 250, height: 250 } // Optional, if you want bounded box UI }, (decodedText, decodedResult) => { // do something when code is read }, (errorMessage) => { // parse error, ignore it. }) .catch((err) => { // Start failed, handle it. });
const html5QrCode = new Html5Qrcode("reader", /* verbose= */ true); Scanning without cameraIdIn mobile devices you may want users to directly scan the QR code using the back camera or the front camera for some use cases. For such cases you can avoid using the exact camera device ID that you get from // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"5. The function onScanSuccess(decodedText, decodedResult) { // handle the scanned code as you like, for example: console.log(`Code matched = ${decodedText}`, decodedResult); } function onScanFailure(error) { // handle scan failure, usually better to ignore and keep scanning. // for example: console.warn(`Code scan error = ${error}`); } let html5QrcodeScanner = new Html5QrcodeScanner( "reader", { fps: 10, qrbox: {width: 250, height: 250} }, /* verbose= */ false); html5QrcodeScanner.render(onScanSuccess, onScanFailure);3 method allows passing constraints in place of camera device ID similar to . You can start scanning like mentioned in these examples: const html5QrCode = new Html5Qrcode("reader"); const qrCodeSuccessCallback = (decodedText, decodedResult) => { /* handle success */ }; const config = { fps: 10, qrbox: { width: 250, height: 250 } }; // If you want to prefer front camera html5QrCode.start({ facingMode: "user" }, config, qrCodeSuccessCallback); // If you want to prefer back camera html5QrCode.start({ facingMode: "environment" }, config, qrCodeSuccessCallback); // Select front camera or fail with `OverconstrainedError`. html5QrCode.start({ facingMode: { exact: "user"} }, config, qrCodeSuccessCallback); // Select back camera or fail with `OverconstrainedError`. html5QrCode.start({ facingMode: { exact: "environment"} }, config, qrCodeSuccessCallback); Passing the function onScanSuccess(decodedText, decodedResult) { // handle the scanned code as you like, for example: console.log(`Code matched = ${decodedText}`, decodedResult); } function onScanFailure(error) { // handle scan failure, usually better to ignore and keep scanning. // for example: console.warn(`Code scan error = ${error}`); } let html5QrcodeScanner = new Html5QrcodeScanner( "reader", { fps: 10, qrbox: {width: 250, height: 250} }, /* verbose= */ false); html5QrcodeScanner.render(onScanSuccess, onScanFailure);4 (recommended approach) is similar to html5QrCode.start({ deviceId: { exact: cameraId} }, config, qrCodeSuccessCallback); Stop ScanningTo stop using camera and thus stop scanning, call function onScanSuccess(decodedText, decodedResult) { // handle the scanned code as you like, for example: console.log(`Code matched = ${decodedText}`, decodedResult); } function onScanFailure(error) { // handle scan failure, usually better to ignore and keep scanning. // for example: console.warn(`Code scan error = ${error}`); } let html5QrcodeScanner = new Html5QrcodeScanner( "reader", { fps: 10, qrbox: {width: 250, height: 250} }, /* verbose= */ false); html5QrcodeScanner.render(onScanSuccess, onScanFailure);5 which returns a // To use Html5QrcodeScanner (more info below) import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode (more info below) import {Html5Qrcode} from "html5-qrcode"6 for stopping the video feed and scanning. <div id="reader" width="600px"></div>0
For QR Code scanning using local files or inbuilt camera on SmartphonesSelector in AndroidSelector in IOSTaken on Pixel 3, Google ChromeTaken on iPhone 7, Google Chrome You can alternatively leverage QR Code scanning for local files on the device or default camera on the device. It works similar to inline QR Code scanning. Define the HTML container and import the JavaScript library as mentioned above <div id="reader" width="600px"></div>1
Add an // This method will trigger user permissions Html5Qrcode.getCameras().then(devices => { /** * devices would be an array of objects of type: * { id: "id", label: "label" } */ if (devices && devices.length) { var cameraId = devices[0].id; // .. use this to start scanning. } }).catch(err => { // handle err });0 element for supporting file selection like this: <div id="reader" width="600px"></div>2 Find more information about this at developers.google.com. And in JavaScript code initialize the object and attach listener like this: <div id="reader" width="600px"></div>3
Demo
For more informationCheck these articles on how to use this library:
Screenshots
DocumentationFollowing methods are available in this library (typescript definition) <div id="reader" width="600px"></div>4 Extra optional // This method will trigger user permissions Html5Qrcode.getCameras().then(devices => { /** * devices would be an array of objects of type: * { id: "id", label: "label" } */ if (devices && devices.length) { var cameraId = devices[0].id; // .. use this to start scanning. } }).catch(err => { // handle err });2 in function onScanSuccess(decodedText, decodedResult) { // handle the scanned code as you like, for example: console.log(`Code matched = ${decodedText}`, decodedResult); } function onScanFailure(error) { // handle scan failure, usually better to ignore and keep scanning. // for example: console.warn(`Code scan error = ${error}`); } let html5QrcodeScanner = new Html5QrcodeScanner( "reader", { fps: 10, qrbox: {width: 250, height: 250} }, /* verbose= */ false); html5QrcodeScanner.render(onScanSuccess, onScanFailure);3 methodConfiguration object that can be used to configure both the scanning behavior and the user interface (UI). Most of the fields have default properties that will be used unless a different value is provided. If you do not want to override anything, you can just pass in an empty object // This method will trigger user permissions Html5Qrcode.getCameras().then(devices => { /** * devices would be an array of objects of type: * { id: "id", label: "label" } */ if (devices && devices.length) { var cameraId = devices[0].id; // .. use this to start scanning. } }).catch(err => { // handle err });4.
|