html5-qrcode
repository·master·Indexed 27 days ago
https://github.com/mebjas/html5-qrcodeA lightweight, cross-platform JavaScript library for scanning QR codes and various barcode formats (including AZTEC, CODE_128, EAN_13, and PDF_417) in web applications. It supports scanning via live camera feeds or local image files. The library provides two API levels: Html5QrcodeScanner for a built-in UI and Html5Qrcode for low-level camera control. It is compatible with Vanilla HTML5, VueJs, ElectronJs, React, and Lit, and supports the native BarcodeDetector API for improved performance.
What's inside html5-qrcode
- Html5-QRCode is a lightweight, cross-platform JavaScript library designed to integrate QR code, barcode, and other common code scanning capabilities into web applications. It supports scanning via live camera feeds (with user permissions) or by selecting local image files from the device. All file scanning is performed locally and is not uploaded to any server.
Use html5-qrcode in the browser without a module loader
masterIf you are not using a module loader (like Webpack or Vite), you can include the library directly via a<script>tag using the UMD version from unpkg. Alternatively, if you have installed the package via npm, you can reference the minified file located atnode_modules/html5-qrcode/html5-qrcode.min.js.Framework Support and Examples
masterThe library is compatible with several modern web frameworks. Examples and integrations are available for:
- Vanilla HTML5
- VueJs
- ElectronJs
- React (via
@scanapp-org/html5-qrcode-react) - Lit
Build the library from source
masterTo build the library for development or production:
- Install dependencies:
npm install. - Build JavaScript output:
npm run-script build(ornpm run-script build-windowson Windows). - The output is located in
./dist/html5-qrcode.min.js.
npm install npm run-script build- Install dependencies:
Use html5-qrcode with module loaders
masterWhen using a module loader (e.g., npm, Yarn, Webpack, Vite), import the specific classes you need from the
html5-qrcodepackage. UseHtml5QrcodeScannerfor a high-level scanner UI orHtml5Qrcodefor low-level camera control.// To use Html5QrcodeScanner import {Html5QrcodeScanner} from "html5-qrcode" // To use Html5Qrcode import {Html5Qrcode} from "html5-qrcode"Choose between Html5QrcodeScanner and Html5Qrcode APIs
masterThe library provides two distinct API levels depending on your integration needs:
Html5QrcodeScanner(Easy Mode): An end-to-end scanner that includes a built-in user interface. It can be integrated with minimal code (less than ten lines).Html5Qrcode(Pro Mode): A low-level API that provides a powerful set of tools to build your own custom user interface. This mode allows you to handle camera setup, permissions, and code reading without the library managing the UI components.
Use the native BarcodeDetector API for improved performance
masterThe library supports using the native browser
BarcodeDetectorAPI, which can provide significantly better scanning performance compared to the standard ZXing library.Note: This feature has graduated from the experimental configuration and is now part of
Html5QrcodeConfigs. It is enabled by default.Performance improvements are most notable on mobile devices and low-end Android hardware, where scanning latency can be reduced by up to 75-80%.
Configure the `start()` method configuration object
masterThe
start()method accepts a configuration object to control scanning behavior and UI. Most fields have default values. If you want to use defaults, pass an empty object{}.// Example of a configuration object let config = { fps: 10, qrbox: { width: 100, height: 100 }, rememberLastUsedCamera: true };Scan specific formats using `Html5QrcodeSupportedFormats`
masterBy default, the library scans all supported formats. You can restrict scanning to a specific subset of formats to improve performance or limit functionality. Supported formats include
QR_CODE,AZTEC,CODE_128,EAN_13,UPC_A, etc.// Scanning only QR codes with Html5Qrcode const html5QrCode = new Html5Qrcode( "reader", { formatsToSupport: [ Html5QrcodeSupportedFormats.QR_CODE ] } ); // Scanning specific formats with Html5QrcodeScanner const formatsToSupport = [ Html5QrcodeSupportedFormats.QR_CODE, Html5QrcodeSupportedFormats.UPC_A ]; const html5QrcodeScanner = new Html5QrcodeScanner( "reader", { fps: 10, formatsToSupport: formatsToSupport }, false ); html5QrcodeScanner.render(onScanSuccess);Configure TorchButtonOptions
masterThe
TorchButtonOptionsinterface defines the initial CSS styling for the torch button during creation.export interface TorchButtonOptions { display: string; marginLeft: string; }Configure Html5QrcodeCameraScanConfig
masterUse
Html5QrcodeCameraScanConfigto tune the behavior of the camera-based scanner during thestart()method.Keys:
fps(number): The expected framerate of scanning (e.g.,{ fps: 2 }scans every 500ms).qrbox(number | QrDimensions | QrDimensionFunction): Defines the scanning region. Can be a fixed size, aQrDimensionsobject, or a function that calculates size based on video stream dimensions. If not set, the entire video area is scanned.aspectRatio(number): The desired aspect ratio for the video feed (e.g.,1.777for 16:9).disableFlip(boolean): Iftrue, flipped (mirrored) QR codes will not be scanned.videoConstraints(MediaTrackConstraints): Overrides other parameters likeaspectRatioorfacingModeusing standard Web API constraints.
Configure the qrbox scanning area
masterThe
qrboxconfiguration option defines the scanning region (the 'shaded' area) in the viewfinder. It can be provided in three formats:- A number: Sets both width and height to the same value.
- A
QrDimensionsobject: An object specifying exact{ width, height }in pixels. - A function: A callback that receives the current
viewfinderWidthandviewfinderHeightand returns aQrDimensionsobject.
Important Constraints:
- The
qrboxdimensions must not be larger than the root element (the container) of the scanner. If the width is larger, it will be truncated to the width of the root element. - If the
qrboxheight is greater than the height of the video stream, the shading effect will be ignored. - If using an object, both
widthandheightmust be explicitly set.