You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{html,css,LitElement}from"https://unpkg.com/lit?module";import{Html5QrcodeScanner}from"https://unpkg.com/html5-qrcode?module";exportclassQRCodeScannerextendsLitElement{render(){returnhtml`<divid="reader"></div><div>${this.decodedText}</div><div>${this.errorMessage}</div> `;}firstUpdated(){constonScanSuccess=(decodedText,decodedResult)=>{// handle the scanned code as you likethis.decodedText=decodedText;};constonScanFailure=(errorMessage,error)=>{// handle scan failure, usually better to ignore and keep scanningthis.errorMessage=errorMessage;};constconfig={fps: 10,qrbox: {width: 350,height: 250,},};constreader=this.shadowRoot.querySelector("#reader");constscanner=newHtml5QrcodeScanner(reader,config);scanner.render(onScanSuccess,onScanFailure);}}customElements.define("qrcode-scanner",QRCodeScanner);
Create the component in TypeScript
import{css,html,LitElement,TemplateResult}from'lit';import{customElement}from'lit/decorators/custom-element.js';import{query}from'lit-element';import{Html5QrcodeError,Html5QrcodeResult}from'html5-qrcode/esm/core';import{Html5QrcodeScanner}from'html5-qrcode';
@customElement('qrcode-scanner')exportclassQRCodeScannerextendsLitElement{
@query('#reader')reader: HTMLElement;protectedrender(): TemplateResult{returnhtml` <divid="reader"></div> `;}protectedfirstUpdated(): void{constonScanSuccess=(decodedText: string,decodedResult: Html5QrcodeResult)=>{// handle the scanned code as you likeconsole.log(`Code matched = ${decodedText}`,decodedResult);};constonScanFailure=(errorMessage: string,error: Html5QrcodeError)=>{// handle scan failure, usually better to ignore and keep scanningconsole.warn(`Code scan error = ${errorMessage}`,error);};constconfig={fps: 10,qrbox: {width: 350,height: 250,},};constscanner=newHtml5QrcodeScanner(this.reader,config,false);scanner.render(onScanSuccess,onScanFailure);}}declare global {interfaceHTMLElementTagNameMap{'qrcode-scanner': QRCodeScanner;}}