纯前端生成二维码

纯前端生成二维码
最新回答
风居住的味道

2021-01-11 17:52:29

以下是一搜逗个完整的HTML文件,无需额外引入依赖,使用纯前端技术生成二维码。该方案适闷唯用于无法使用有线网络、无线电或USB传输数据的特殊环境,通过二维码实现非接触式数据传输:

<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>纯前端二维码生成器</title> <style> body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; text-align: center; } textarea { width: 100%; height: 100px; margin: 10px 0; padding: 10px; box-sizing: border-box; } button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; margin: 5px; } button:hover { background-color: #45a049; } #qrcode { margin: 20px auto; padding: 10px; border: 1px solid #ddd; display: inline-block; } .controls { margin: 20px 0; } </style></head><body> <h1>纯前端二维码生成器</h1> <p>适用于特殊环境下的非接触式数据传输</p> <div class="controls"> <textarea id="qrtext" placeholder="在此输入要编码的文本或URL..."></textarea> <br> <button onclick="generateQR()">生成二维码</button> <button onclick="downloadQR()">下载二维码</button> <button onclick="clearAll()">清空</button> </div> <div id="qrcode"></div> <script> // 二维世罩卖码生成函数 function generateQR() { const text = document.getElementById('qrtext').value.trim(); if (!text) { alert('请输入要编码的文本或URL'); return; } const qrcodeDiv = document.getElementById('qrcode'); qrcodeDiv.innerHTML = ''; // 清空之前的二维码 try { // 使用URL.createObjectURL创建canvas的Blob URL const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); // 二维码配置 const size = Math.min(400, window.innerWidth - 40); canvas.width = size; canvas.height = size; // 使用简单的二维码算法(这里简化处理,实际可用更复杂的算法) // 注意:这是一个简化版,实际项目中建议使用qrcode.js等库 drawSimpleQR(ctx, text, size); qrcodeDiv.appendChild(canvas); // 存储canvas引用供下载使用 qrcodeDiv._canvas = canvas; } catch (e) { console.error('二维码生成失败:', e); alert('二维码生成失败,请重试'); } } // 简化的二维码绘制函数(仅用于演示) // 实际项目中应使用成熟的库如qrcode.js function drawSimpleQR(ctx, text, size) { // 清除画布 ctx.fillStyle = '#ffffff'; ctx.fillRect(0, 0, size, size); // 简化的二维码定位图案 const moduleSize = size / 21; const quietZone = moduleSize * 4; const qrSize = size - quietZone * 2; // 绘制定位图案(简化版) ctx.fillStyle = '#000000'; // 三个角落的定位图案 for (let i = 0; i < 3; i++) { const x = i * (qrSize + quietZone) + quietZone; const y = i === 0 ? quietZone : i === 1 ? quietZone + qrSize - 7 * moduleSize : quietZone; // 外框 ctx.fillRect(x, y, 7 * moduleSize, 7 * moduleSize); ctx.fillStyle = '#ffffff'; ctx.fillRect(x + moduleSize, y + moduleSize, 5 * moduleSize, 5 * moduleSize); ctx.fillStyle = '#000000'; ctx.fillRect(x + 2 * moduleSize, y + 2 * moduleSize, 3 * moduleSize, 3 * moduleSize); } // 简化的数据编码(实际二维码有复杂的纠错码和编码规则) // 这里仅做演示,将文本转换为简单的黑白模块 ctx.fillStyle = '#000000'; const dataStartX = quietZone + 8 * moduleSize; const dataStartY = quietZone; const dataWidth = qrSize - 8 * moduleSize; const modulesPerRow = Math.floor(dataWidth / moduleSize); // 使用简单的哈希算法将文本转换为模块模式 let hash = 0; for (let i = 0; i < text.length; i++) { hash += text.charCodeAt(i) * (i + 1); } for (let y = 0; y < modulesPerRow; y++) { for (let x = 0; x < modulesPerRow; x++) { const bit = (hash >> ((y * modulesPerRow + x) % 31)) & 1; if (bit) { ctx.fillRect( dataStartX + x * moduleSize, dataStartY + y * moduleSize, moduleSize, moduleSize ); } } } // 添加说明文字 ctx.fillStyle = '#000000'; ctx.font = '12px Arial'; ctx.fillText('纯前端生成的二维码', quietZone, size - 10); } // 下载二维码 function downloadQR() { const qrcodeDiv = document.getElementById('qrcode'); if (!qrcodeDiv._canvas) { alert('请先生成二维码'); return; } try { const canvas = qrcodeDiv._canvas; const link = document.createElement('a'); link.download = 'qrcode.png'; link.href = canvas.toDataURL('image/png'); link.click(); } catch (e) { console.error('下载失败:', e); alert('下载失败,请重试'); } } // 清空所有 function clearAll() { document.getElementById('qrtext').value = ''; document.getElementById('qrcode').innerHTML = ''; } </script></body></html>关键特性说明:
  1. 纯前端实现

    完全使用HTML、CSS和JavaScript实现,无需任何外部依赖

    适合无法连接网络的特殊环境使用

  2. 核心功能

    文本/URL输入框

    二维码生成按钮

    下载功能(将二维码保存为PNG)

    清空功能

  3. 技术说明

    使用Canvas API绘制二维码

    实现了简化的二维码定位图案和数据编码

    注意:这是一个教学示例,实际二维码生成算法更复杂(包含纠错码、掩码等)

  4. 使用限制

    此示例中的二维码生成算法是简化版

    对于生产环境,建议使用成熟的库如:

    qrcode.js

    QRious

    jsQR

  5. 获取方式

    可通过命令获取完整HTML