Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

前端生成二维码并检查 #216

Closed
Dream4ever opened this issue Jun 28, 2022 · 0 comments
Closed

前端生成二维码并检查 #216

Dream4ever opened this issue Jun 28, 2022 · 0 comments
Labels
Browser All about browsers

Comments

@Dream4ever
Copy link
Owner

实现方案

二维码生成

用到了 soldair / node-qrcode 这个库。

核心代码如下:

// 引入依赖
import QRCode from 'qrcode'

const opts = {
  // 7% 的容错率
  errorCorrectionLevel: 'L',
  // PNG 格式图片
  type: 'png',
  // 码点与图片边界留出两个码点的距离
  margin: 2,
  // 图片宽高为 260px
  width: 260,
}

// 生成二维码
const img = await QRCode.toDataURL(url, opts)
// 将二维码的内容显示在 img 元素中
document.querySelectorAll('.qrcode img')[i].src = img

二维码检查

用到了 nuintun / qrcode 这个库。

核心代码如下:

// 引入依赖
import { Decoder } from '@nuintun/qrcode'

// 检查二维码
// 这里的 img 为前面生成二维码时保存的 img 变量
const qrcode = new Decoder()
const result = await qrcode.scan(img)
// 解析出的二维码 URL 就在 data 属性中
if (result.data === ...)
@Dream4ever Dream4ever added the Browser All about browsers label Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Browser All about browsers
Projects
None yet
Development

No branches or pull requests

1 participant