index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<!DOCTYPE html> <html lang="en"> <head> <title>QR Code Scanner</title> <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet"> </head> <body> <div class="flex flex-col items-center justify-center min-h-screen"> <h1 class="text-4xl font-bold mb-8">QR Code Scanner</h1> <div class="mb-4"> <input type="file" id="image-input" accept="image/*" class="mb-2"> <img id="preview" src="" alt="Image Preview" class="w-46 h-46 object-contain border-4 border-gray-300 rounded-lg overflow-hidden"> </div> <button id="scan-button" class="mt-4 px-4 py-2 bg-blue-500 text-white rounded-md">Scan QR Code from Image</button> <textarea id="result-textarea" rows="4" class="mt-4 w-96 border border-gray-300 rounded-lg p-2" placeholder="QR Code result will be displayed here..." readonly></textarea> <br><br> </div> <!-- Include the qr-scanner library --> <script type="module"> import QrScanner from "https://unpkg.com/qr-scanner/qr-scanner.min.js"; const imageInput = document.getElementById('image-input'); const preview = document.getElementById('preview'); const scanButton = document.getElementById('scan-button'); const resultTextarea = document.getElementById('result-textarea'); // Display image preview imageInput.addEventListener('change', (event) => { const file = event.target.files[0]; if (file) { preview.src = URL.createObjectURL(file); } }); // Scan QR code when the button is clicked scanButton.addEventListener('click', () => { const file = imageInput.files[0]; if (file) { QrScanner.scanImage(file) .then(result => { resultTextarea.value = `QR Code Result: ${result}`; // Show result in textarea console.log(result); }) .catch(error => { console.error('Error scanning QR code:', error); resultTextarea.value = 'No QR code found in the image.'; // Show error message in textarea }); } else { resultTextarea.value = 'Please upload an image first.'; // Show message in textarea } }); </script> </body> </html> |