Generate QR Code in NodeJS with QR code

Tram Ho

Generate QR Code in nodejs with qrcode

What is QR Code?

QR Code (Quick Response Code) is a two-dimensional barcode that can be read by a barcode reader or a smartphone with barcode scanning function, or through a 3rd party application (zalo, facebook, …) . The QR code is a two-dimensional symbol, it was invented in 1994 by the Denso Wave company (Japan). More information can be found here

Generate QR code with qrcode

Install qrcode library

First we need to create a new project. And install express library on comand line with command

npm install express –save

Next we use the following command on the command line to install the qrcode library

npm install qrcode –save

Or you can install global with the command

npm install -g qrcode

Use qrcode library

You can create a simple QR code with a few lines of code as follows

The example above I am creating a QR code for the sentence ‘I am Cuamotcang’. When accessing the address http: // localhost: 3000 / you will see the following result. You can take out your phone to test it!

Also the QR code information printed out in the comand line by the above console.log (qr) command will look like this:

data: image / png; base64, iVBORw0KGgoAAAANSUhEUgAAAIQAAACECAYAAABRRIOnAAAAAklEQVR4AewaftIAAAOPSURBVO3BQY4jSQIDQWdA // + ybx / mwFMACUk1Nb00i38w84 / DTDnMlMNMOcyUw0w5zJTDTDnMlMNMOcyUw0w5zJTDTDnMlMNMefGmJPwklXck4ZNUbpLwk1TecZgph5lymCkvPkzlk5LwSSo3SXgiCU3lRuWTkvBJh5lymCmHmfLiy5LwhMoTSWgqLQk3KjcqLQlN5R1JeELlmw4z5TBTDjPlxV8mCU2lJaEloam0JPw / WHH OcyUw0w5zJQXfxmVG5WWhBuVloSWhKbyX3aYKYeZcpgpL75M5Scl4ZtUPknlNznMlMNMOcyUFx + + + kyTcJOEnqfyXHGbKYaYcZkr8gzckoam0JHySyk0SnlD5piR8kso3HWbKYaYcZkr8gy9KQlNpSbhRaUloKjdJaCo3SXhC5YkkNJWWhKbykw4z5TBTDjPlxb9M5SYJTyShqdwk4R1JuFF5QuUmCTcq7zjMlMNMOcyUFx XFm1R TSktCU2lJuElCU2lJaCo3SfjNDjPlMFMOM + + + WhKZyk4QblSdUbpLQVFoSmspNEt6RhKZyo9KS8EmHmXKYKYeZ8uJNSWgqLQlN5UalJaGp3CShqdwkoam0JNyotCS0JDSVloSWhBuVbzrMlMNMOcyUF7 cSktCU3lC5QmVmyTcqLQkNJWbJNyofNJhphxmymGmvPhlkvBEEprKTRJuVG6S0FRaEloS3qFyk4Sm8o7DTDnMlMNMefHLqNwk4SYJTeVG5SYJTaUloam0JDSVmyQ0lZaEbzrMlMNMOcyUFz8sCU8koancJOGTVJ5Iwk0SnkhCU / mmw0w5zJTDTIl / 8B + WhKbSktBUWhI + SeWJJNyo3CShqbzjMFMOM + UwU168KQk / SaWptCQ8odKScKPSknCThKbyjiR802GmHGbKYaa8 + D CVT0rCTRKaSkvCTRJuVN6h8oTKEyqfdJgph5lymCkvviwJT6h8ksoTKi0JTyThHUloKj / pMFMOM + UwU178ZZLQVG5UblSeSMKNSkvCEyotCU3lHYeZcpgph5ny4i + j0pLwhMpNEppKU7lJQlO5SUJT + abDTDnMlMNMefFlKt + kcqPSktBUbpJwk4Sm0pLwDpWfdJgph5lymCkvPiwJPykJTaUl4R0qTyShqbQkPJGEn3SYKYeZcpgp8Q9m / nGYKYeZcpgph5lymCmHmXKYKYeZcpgph5lymCmHmXKYKYeZcpgp / wPa0IYoBgikYgAAAABJRU5ErkJggg ==


In addition to the Data URI format, qrcode also supports creating qrcode with different formats such as String (toString), canvas (toCanvas), file (toFile), file stream (toFileStream). The following is an example of creating a QR code as a file:

And here is the result obtained accessing http: // localhost: 3000 / . A new image file named cmc.png has been created

qrcode option

qrcode supports 4 different input characters, and each corresponds to the maximum number of characters that can be encoded.

DatatypesCharacter typeEncryption capabilities
Simple number0, 1, 2, 3, 4, 5, 6, 7, 8, 9Maximum 7,089 characters
Numbers and letters0–9, A – Z (upper-case only), space, $,%, +, -, …Maximum 4,296 characters
Binary number (8bit)Characters from JIS X 0208 setMaximum of 2,953 bytes
Kanji / KanaCharacter from ISO / IEC 8859-1 setMaximum 1,817 characters

It is also possible to mix encodings together as shown in the example below:

In addition, we can also change the color, size, file extension, … of the generated QR code. Details see here . Here is an illustrative example:


This is my first post, hope everyone likes it. There are many shortcomings in the article. Hope everyone comments more sir. Thank you for reading this article.


Share the news now

Source : Viblo