Home >Web Front-end >JS Tutorial >See how to use nodejs to generate QR codes

See how to use nodejs to generate QR codes

青灯夜游
青灯夜游forward
2022-10-25 09:28:102539browse

See how to use nodejs to generate QR codes

QR codes are everywhere in life. I have also used Java’s zxing library to generate QR codes before, which is very powerful.

But in fact, there are many third-party libraries on nodejs that can generate QR codes. Today we are using the qrcode library to generate QR codes. [Related tutorial recommendations: nodejs video tutorial]

Effect display

Online example: http://www.lolmbbs.com/tool/qr

See how to use nodejs to generate QR codes

Detailed code

Generate QR code:

const qrCode = require('qrcode')
class QrController {
  async create (ctx) {
    const { text = 'Luban', options } = ctx.request.body
    const qrOptions = {
      type: 'image/png',
      width: 180,
      margin: 0,
      scale: 1,
      color: {
        dark: '#000000',
        light: '#ffffff'
      },
      errorCorrectionLevel: 'M',
      quality: 1
    }
    Object.assign(qrOptions, options)
    const imgData = await qrCode.toDataURL(text, qrOptions)
    return ctx.success({ imgData })
  }
}

module.exports = new QrController()

Download QR code:

const a = document.createElement('a')
const event = new MouseEvent('click')
a.download = '二维码'
a.href = this.imgSrc
a.dispatchEvent(event)

Explanation of main parameters

  • type: Generate image type
    mainly includes three types: image/png, image/jpeg, image/web .
    ps: But even if I set the type to image/jpeg in the code, I found that the generated image is still png. Later, after reading the document carefully, I found out that the toDataURL method only supports generating png type images...
    See how to use nodejs to generate QR codes

  • width: The width of the QR code
    I found that there is no height field setting, maybe the generated QR codes are all square.

  • margin: Padding
    The padding is set to 10
    See how to use nodejs to generate QR codes
    The padding is set to 0
    See how to use nodejs to generate QR codes

  • scale Zoom multiple
    If width is set, width takes effect first, then this parameter is useless.
    The scaling ratio is 5
    See how to use nodejs to generate QR codes
    The zoom ratio is 10
    See how to use nodejs to generate QR codes

  • ##color.light: Foreground color

  • color.night: Background color The default foreground color is black and the background color is white.

  • errorCorrectionLevel Error correction level
    See how to use nodejs to generate QR codes Even if part of the QR code cannot be displayed, the content of the QR code can still be recognized. This is the error correction of QR code.
    L level error correction means that as long as the incompleteness is less than 7%, it can be identified, and so on

For more node-related knowledge, please visit:

nodejs Tutorial!

The above is the detailed content of See how to use nodejs to generate QR codes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete