https://github.com/mebjas/html5-qrcode#extra-optional-configuration-in-start-method
Html5QrcodeScanner 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.
Html5QrcodeScanner
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);
You can use Html5Qrcode 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.
Html5Qrcode
You can use the following APIs to fetch camera, start scanning and stop scanning.
fetch camera
start
stop
To get a list of supported cameras, query it using static method Html5Qrcode.getCameras(). This method returns a Promise with a list of devices supported in format { id: "id", label: "label" }.
Html5Qrcode.getCameras()
Promise
{ id: "id", label: "label" }
// 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.
Warning: Direct access to the camera is a powerful feature. It requires consent from the user, and your site MUST be on a secure origin (HTTPS).Warning: Asking for access to the camera on page load will result in most of your users rejecting access to it. More info
Warning: Direct access to the camera is a powerful feature. It requires consent from the user, and your site MUST be on a secure origin (HTTPS).
Warning: Asking for access to the camera on page load will result in most of your users rejecting access to it. More info
Once you have the camera ID from device.id, start camera using Html5Qrcode#start(..). This method returns a Promise with Qr code scanning initiation.
device.id
Html5Qrcode#start(..)
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. });
You can optionally set another argument in constructor called verbose to print all logs to console
verbose
const html5QrCode = new Html5Qrcode("reader", /* verbose= */ true);
In 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 Html5Qrcode.getCameras(). The start() method allows passing constraints in place of camera device ID similar to html5 web API syntax. You can start scanning like mentioned in these examples:
start()
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 cameraId (recommended approach) is similar to
cameraId
html5QrCode.start({ deviceId: { exact: cameraId} }, config, qrCodeSuccessCallback);
To stop using camera and thus stop scanning, call Html5Qrcode#stop() which returns a Promise for stopping the video feed and scanning.
Html5Qrcode#stop()
html5QrCode.stop().then((ignore) => { // QR Code scanning is stopped. }).catch((err) => { // Stop failed, handle it. });
Note that the class is stateful and stop() should be called to properly tear down the video and camera objects safely after calling start() when the scan is over or the user intend to move on. stop() will stop the video feed on the viewfinder.
stop()
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.
Figure: Screenshot from Google Chrome running on MacBook Pro