您当前的位置: 首页 >  node.js

彭世瑜

暂无认证

  • 0浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Node.js:浏览器环境下使用qrcode生成二维码

彭世瑜 发布时间:2021-04-25 11:51:08 ,浏览量:0

  • github: https://github.com/soldair/node-qrcode
  • npmjs: https://www.npmjs.com/package/qrcode
  • cdn: https://cdn.jsdelivr.net/npm/qrcode/build/qrcode.min.js
Node.js

安装

npm i qrcode

API

toDataURL(text, [options], [cb(error, url)])

## 参数:
text String|Array

options: {
    version Type: Number

    errorCorrectionLevel Type: String Default: M
    # low, medium, quartile, high or L, M, Q, H
    
    maskPattern Type: Number
    # 0, 1, 2, 3, 4, 5, 6, 7.

    toSJISFunc Type: Function

    type Type: String Default: image/png
    # image/png, image/jpeg, image/webp
    
    quality Type: Number Default: 0.92
    
    # Renderers options
    margin  Type: Number Default: 4
    scale   Type: Number Default: 4
    width   Type: Number
    color: {
        dark  Type: String Default: #000000ff
        light Type: String Default: #ffffffff
    }
}

cb Type: Function

使用示例

import QRCode from 'qrcode'
 
// With promises
QRCode.toDataURL('I am a pony!')
  .then(url => {
    console.log(url)
  })
  .catch(err => {
    console.error(err)
  })

生成的二维码(边框和文字是我加的) 在这里插入图片描述

Browser

配合jquery使用





$(document).ready(function () {
        QRCode.toDataURL(window.location.href, { errorCorrectionLevel: 'H' }, function (err, url) {
            // console.log('QRCode', url)
            $('#weixin-code').attr('src', url)
        })
    });
 
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.4287s