Home > Article > Web Front-end > 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]
Online example: http://www.lolmbbs.com/tool/qr
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)
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...
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
The padding is set to 0
scale Zoom multiple
If width is set, width takes effect first, then this parameter is useless.
The scaling ratio is 5
The zoom ratio is 10
##color.light: Foreground color
color.night: Background color The default foreground color is black and the background color is white.
errorCorrectionLevel Error correction level
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
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!