Nodejitsu

Save time managing and deploying your node.js app

npmawesome: Generating QR codes with Node.js

About the author

Name
Location
Worldwide
nodejitsu nodejitsu

Other popular posts

- Scaling Isomorphic Javascript Code - Keep a node.js server up with Forever - Package.json dependencies done right
- npm cheat sheet - 6 Must Have Node.js Modules
Sign up to our platform for free - get $20 usage

This is a guest post from Alex Gorbatchev and npmawesome.com. Nodejitsu loved what Alex was doing at npmawesome.com and is now supporting the project. Like what you see here? Why don't you contribute on Github?

QR code is this super cool thing you probably have been seeing all over the place lately. It started appearing in North America a few years ago but the actual standard is close to two decades old. It allows you to embed any small amount of data that could be decoded with a basic camera equipped phone. Most common use case here is embedding URLs in printed adverising so that those who are interested could simply point their phone at the add and go to the site instead of having to type in URLs.

qr-image (Github: alexeyten/qr-image, License: MIT) is a module by Alexey Ten to render QR code with node.js and no external dependencies.

npm install qr-image

Overview

  • No dependecies. This is a really cool bit, if you look at package.json there are literally no dependencies there.
  • Generate image in png, svg, eps and pdf formats.
  • Support UTF-8.

Usage

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

var code = qr.image('http://blog.nodejitsu.com', { type: 'svg' });  
var output = fs.createWriteStream('nodejitsu.svg')

code.pipe(output);  

Because we are dealing with streams here, hooking this up to HTTP server is super easy.

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);  

Now, going to http://localhost:3000 will spit out a QR code with current time. Wicked!

Metadata

QR code allows you embed pretty much any text, but with some additional formatting you can tell scanning applications about the data and what you want them to do with it. Here are a few most common formats:

Data Type Data Format
Website URL http://www.websitename.com (include the “http://” so it recognizes the data as a website URL)
Pre-populated email address mailto:name@email.com (include “mailto” so it recognizes the data as an email address)
Pre-Populated Telephone Number +16365553344 (URI should include the country code to support users outside the country of origin)
Contact Information LastName,FirstName;ADR:(insert address), (second address line), City, ST (Insert 5-digit zip code);TEL:+16365553344;EMAIL:name@email.com;;DO NOT use parenthesis or line breaks in code.

*For QR Codes, the MECARD standard format shown above is more standard than vCard standard format

Pre-populated SMS Message smsto:number:message
Pre-populated MMS Message mms:number:subject
Map or Location Coordinates latitude, longitude, altitude in meters (a minus sign denotes a South latitude and a West longitude)code for Google offices: 40.71872,-73.98905, 100
Android Market URI for Android Devices For a QR code you must replace all punctuation with code. For example,market://search?q=pub:”Search Query” becomes:

market://search?q=pub%3A%22Search%20Query%22

YouTube Video youtube://ID (may work on iPhone, untested, unreleased, not standardized)
iCal Appointment BEGIN:VEVENT
SUMMARY:Test Meeting
DTSTART:20110713T190000Z
DTEND:20110713T200000Z
END:VEVENT
(untested, unreleased, not standardized)