1- // TODO: Generate QR code from input text/URL
2- // TODO: Display generated QR code
3- // TODO: Download QR code image
4- // TODO: Scan uploaded QR code image
5- // TODO: Display scanned QR code result
61
72function initQRCodeGeneratorScanner ( ) {
8- // TODO: Generate QR code from input
9- // TODO: Scan QR code from image
10- // TODO: Display results
11- // TODO: Download QR code
3+ const qrContainer = document . getElementById ( "qr-container" ) ;
4+
5+
6+ qrContainer . innerHTML = `
7+ <section class="generator">
8+ <h2>Generate a QR Code</h2>
9+ <input id="qr-input" type="text" placeholder="Enter text or URL..." />
10+ <button id="generate-btn">Generate</button>
11+ <div id="qrcode"></div>
12+ <button id="download-btn" class="hidden">Download QR</button>
13+ </section>
14+
15+ <section class="scanner">
16+ <h2>Scan a QR Code (Upload Image)</h2>
17+ <input id="file-input" type="file" accept="image/*" />
18+ <p id="scan-result"></p>
19+ </section>
20+ ` ;
21+
22+ const qrInput = document . getElementById ( "qr-input" ) ;
23+ const generateBtn = document . getElementById ( "generate-btn" ) ;
24+ const qrContainerEl = document . getElementById ( "qrcode" ) ;
25+ const downloadBtn = document . getElementById ( "download-btn" ) ;
26+ const fileInput = document . getElementById ( "file-input" ) ;
27+ const resultEl = document . getElementById ( "scan-result" ) ;
28+
29+ let qrCodeInstance = null ;
30+
31+ generateBtn . addEventListener ( "click" , ( ) => {
32+ const text = qrInput . value . trim ( ) ;
33+ if ( ! text ) {
34+ alert ( "Please enter some text or a URL!" ) ;
35+ return ;
36+ }
37+
38+ qrContainerEl . innerHTML = "" ;
39+
40+ qrCodeInstance = new QRCode ( qrContainerEl , {
41+ text,
42+ width : 200 ,
43+ height : 200 ,
44+ } ) ;
45+
46+ downloadBtn . classList . remove ( "hidden" ) ;
47+ } ) ;
48+
49+ downloadBtn . addEventListener ( "click" , ( ) => {
50+ const img = qrContainerEl . querySelector ( "img" ) ;
51+ if ( ! img ) {
52+ alert ( "Please generate a QR code first!" ) ;
53+ return ;
54+ }
55+
56+ const link = document . createElement ( "a" ) ;
57+ link . href = img . src ;
58+ link . download = "qrcode.png" ;
59+ link . click ( ) ;
60+ } ) ;
61+
62+
63+ fileInput . addEventListener ( "change" , async ( e ) => {
64+ const file = e . target . files [ 0 ] ;
65+ if ( ! file ) return ;
66+
67+ const reader = new FileReader ( ) ;
68+ reader . onload = async function ( ) {
69+ const imageDataUrl = reader . result ;
70+ resultEl . textContent = "Scanning..." ;
71+
72+ try {
73+ const result = await scanQRCodeFromImage ( imageDataUrl ) ;
74+ if ( result ) {
75+ resultEl . textContent = `Scanned Result: ${ result } ` ;
76+ } else {
77+ resultEl . textContent = "No QR code detected." ;
78+ }
79+ } catch ( err ) {
80+ console . error ( err ) ;
81+ resultEl . textContent = "Error scanning QR code." ;
82+ }
83+ } ;
84+ reader . readAsDataURL ( file ) ;
85+ } ) ;
86+ }
87+
88+ async function scanQRCodeFromImage ( imageDataUrl ) {
89+ return new Promise ( ( resolve , reject ) => {
90+ const img = new Image ( ) ;
91+ img . onload = ( ) => {
92+ const canvas = document . createElement ( "canvas" ) ;
93+ const ctx = canvas . getContext ( "2d" ) ;
94+ canvas . width = img . width ;
95+ canvas . height = img . height ;
96+ ctx . drawImage ( img , 0 , 0 , img . width , img . height ) ;
97+
98+ const imageData = ctx . getImageData ( 0 , 0 , img . width , img . height ) ;
99+
100+
101+ if ( typeof jsQR === "undefined" ) {
102+ const script = document . createElement ( "script" ) ;
103+ script . src = "https://cdn.jsdelivr.net/npm/jsqr/dist/jsQR.js" ;
104+ script . onload = ( ) => {
105+ const code = jsQR ( imageData . data , imageData . width , imageData . height ) ;
106+ resolve ( code ? code . data : null ) ;
107+ } ;
108+ script . onerror = ( ) => reject ( "Failed to load jsQR library." ) ;
109+ document . body . appendChild ( script ) ;
110+ } else {
111+ const code = jsQR ( imageData . data , imageData . width , imageData . height ) ;
112+ resolve ( code ? code . data : null ) ;
113+ }
114+ } ;
115+ img . onerror = reject ;
116+ img . src = imageDataUrl ;
117+ } ) ;
12118}
13119
14- window . addEventListener ( ' DOMContentLoaded' , initQRCodeGeneratorScanner ) ;
120+ window . addEventListener ( " DOMContentLoaded" , initQRCodeGeneratorScanner ) ;
0 commit comments