QR Code Reader

1.0.4 · active · verified Sun Apr 19

qrcode-reader is a JavaScript library for decoding QR codes from various image data sources. It functions as a maintained fork of Lazarsoft's popular jsqrcode project, providing QR code reading capabilities in both Node.js environments and HTML5-enabled browsers. The package is currently at version 1.0.4, and while a formal release cadence isn't specified, its "maintained fork" status indicates ongoing support and bug fixes, though updates may not be frequent. A key characteristic is its pure JavaScript implementation, avoiding native dependencies that can complicate installation, particularly for cross-platform deployment. For Node.js usage, it necessitates an external image parsing library such as Jimp or image-parser to handle raw image buffers and convert them into the specific bitmap format expected by the decoder. Conversely, in browser contexts, it integrates directly with the native Image global object or canvas ImageData objects, simplifying client-side implementation. It utilizes a callback-based API for asynchronous operations.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to read and decode a QR code from an image file in a Node.js environment, utilizing the Jimp library for image parsing.

const fs = require('fs');
const Jimp = require('jimp');
const QrCode = require('qrcode-reader');

// IMPORTANT: For this code to run, you need a QR code image file (e.g., 'qrcode.png')
// in the same directory as your script, or update 'imagePath' to point to one.
// You can generate a simple 'Hello World' QR code using an online generator or a library.
const imagePath = './qrcode.png'; 

try {
  const buffer = fs.readFileSync(imagePath);

  Jimp.read(buffer, function(err, image) {
      if (err) {
          console.error("Error reading image with Jimp:", err);
          return;
      }
      const qr = new QrCode();
      qr.callback = function(err, value) {
          if (err) {
              console.error("QR Code decoding error:", err);
              return;
          }
          console.log("QR Code result:", value.result);
          console.log("Decoded value details:", value);
      };
      // The 'image.bitmap' object from Jimp provides { width, height, data } 
      // in a format expected by qrcode-reader.
      qr.decode(image.bitmap);
  });
} catch (readErr) {
  console.error(`Failed to read QR code image at ${imagePath}. Please ensure the file exists and is accessible.\nIf you don't have one, create a simple 'qrcode.png' in the same directory as this script.`);
}

view raw JSON →