简体   繁体   中英

How can I generate QR images in Node.js without canvas?

I'm building a website for a client in Node.js and I need to generate QR-codes or Barcodes for the ticket system.

I've found a few modules, but all of them need an install like canvas, one way or another.

I'm on a shared hosting package and my host does not allow me to install any such packages, unless I upgrade to a VPS or dedicated server (which I do not have the money for).

Does any of you know how I can pull this off in Node.js or do I need to put up a subdomain for generating the QR in PHP or front-end generating (which I do not prefer AT ALL)?

Currently using:

  • Node.js
  • Express.js
  • Angular.js

Modules found:

Have a look over this code:

var qr = require('qr-image');  
var express = require('express');

var app = express();

app.get('/', function(req, res) {  
  var code = qr.image(new Date().toString(), { type: 'svg' });
  res.type('svg');
  code.pipe(res);
});

app.listen(3000);

visit npm-qr-image

您可以使用qr-image它是一个完整的 JavaScript 解决方案,它使用 Buffers 生成以下格式的二维码:png、svg、eps 和 pdf 格式

In my case i did following please refer qrcode for more information.

const qrcode = require('qrcode');
const qrOption = { 
  margin : 7,
  width : 175
};
const qrString = 'QR_STRING';
const bufferImage = await qrcode.toDataURL(qrString,qrOption);
console.log(bufferImage);

try it

1)npm i qrcode
2)var QRCode = require('qrcode')
3)QRCode.toString('I am a pony!',{type:'terminal'}, function (err, 
base64image) {
 console.log(base64image)
  })

Here is another suggestion about how you can achieve this. You just need to grab the url of the QRCode and assign it to an img tag source in html. It's possible to do that with the toDataUrl() api of node-qrcode .

Here is the definition :

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

Returns a Data URI containing a representation of the QR Code image. If provided, canvasElement will be used as canvas to generate the data URI.

canvasElement

Type: DOMElement Canvas where to draw QR Code.

text

Type: String|Array Text to encode or a list of objects describing segments.

options

type

Type: String Default: image/png Data URI format. Possible values are: image/png, image/jpeg, image/webp .

rendererOpts.quality

Type: Number Default: 0.92 A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp .

cb Type: Function Callback function called on finish.

So in order to display the neo QRCode, this is how I suggest you to do it based on the documentation:

const fs = require('fs');
const path = require('path');
const express = require('express');
const router = module.exports = express.Router()
const QRCode = require('qrcode')

router.get('/', (req, res) => {
    QRCode.toDataURL('Hello World !').then(url => {
        res.send(`
        <h2>QRCode Generated</h2>
        <div><img src='${url}'/></div>
      `)
    }).catch(err => {
        console.debug(err)
    })
});

Coded 🖖

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM