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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>前端二维码生成与识别</title> <style> .container { max-width: 600px; margin: 20px auto; padding: 20px; font-family: Arial, sans-serif; } .section { margin-bottom: 30px; padding: 20px; border: 1px solid #eee; border-radius: 8px; } input, button { padding: 8px 12px; margin-right: 10px; border: 1px solid #ddd; border-radius: 4px; } button { background: #409eff; color: white; border: none; cursor: pointer; } button:hover { background: #66b1ff; } #qrcode { margin-top: 15px; border: 1px solid #eee; padding: 10px; display: inline-block; } #scanResult { margin-top: 15px; padding: 10px; border: 1px solid #eee; min-height: 40px; color: #333; } canvas { display: none; } </style> </head> <body> <div class="container"> <div class="section"> <h3>二维码生成</h3> <input type="text" id="textInput" placeholder="请输入要生成二维码的内容" value="https://www.example.com"> <button id="generateBtn">生成二维码</button> <div id="qrcode"></div> </div>
<div class="section"> <h3>二维码识别</h3> <input type="file" id="fileInput" accept="image/*"> <div id="scanResult">识别结果将显示在这里...</div> </div>
<canvas id="scanCanvas"></canvas> </div>
<script src="https://cdn.bootcdn.net/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/jsQR/1.4.0/jsQR.min.js"></script>
<script> const generateBtn = document.getElementById('generateBtn'); const textInput = document.getElementById('textInput'); const qrcodeContainer = document.getElementById('qrcode');
function generateQRCode(text) { qrcodeContainer.innerHTML = ''; new QRCode(qrcodeContainer, { text: text, width: 200, colorDark: '#000000', colorLight: '#ffffff', correctLevel: QRCode.CorrectLevel.H }); }
generateBtn.addEventListener('click', () => { const text = textInput.value.trim(); if (!text) { alert('请输入要生成二维码的内容!'); return; } generateQRCode(text); });
window.onload = () => { generateQRCode(textInput.value.trim()); };
const fileInput = document.getElementById('fileInput'); const scanResult = document.getElementById('scanResult'); const scanCanvas = document.getElementById('scanCanvas'); const ctx = scanCanvas.getContext('2d');
fileInput.addEventListener('change', (e) => { const file = e.target.files[0]; if (!file) return;
if (!file.type.startsWith('image/')) { scanResult.textContent = '请上传图片文件!'; return; }
const reader = new FileReader(); reader.onload = (event) => { const img = new Image(); img.onload = () => { scanCanvas.width = img.width; scanCanvas.height = img.height; ctx.drawImage(img, 0, 0); const imageData = ctx.getImageData(0, 0, img.width, img.height); const code = jsQR(imageData.data, img.width, img.height);
if (code) { scanResult.textContent = `识别成功:${code.data}`; console.log('二维码位置:', code.location); } else { scanResult.textContent = '未识别到二维码,请确认图片包含清晰的二维码!'; } }; img.src = event.target.result; }; reader.readAsDataURL(file); }); </script> </body> </html>
|