Creating a QR reader using Processing

This entry will show you how to create a QR reader using a live webcam feed.

Install Processing

Option 1: Manual Download

Processing 2.2.1 Disk Image

Option 2: Homebrew

brew cask install processing 2.2.1

Install libraries

You will need to install these libraries.


Create a QR Code

There are dozens of free QR Code generators online. Here's one that I use regularly.


Script

import hypermedia.video.*;
import qrcodeprocessing.*;

OpenCV opencv;
Decoder decoder;

String statusMsg = "Waiting for an image";

void setup(){
  size(400, 320);
  
  opencv = new OpenCV( this );
  opencv.capture( width, height );
  
  decoder = new Decoder( this );
  
}

void draw(){
  opencv.read();
  image( opencv.image(), 0, 0 );
  
  text(statusMsg, 10, height - 4 );
  
  if( decoder.decoding()){
    PImage show = decoder.getImage();
    image(show, 0, 0, show.width/4, show.height/4);
    
    statusMsg = "Decoding image";
    for( int i = 0; i < (frameCount) % 10; i++){
      statusMsg += ".";
    }
  }
}

void keyReleased(){
  String qrPath = "/path/to/qr/code.png";
  String code = "";
  switch(key){
   case ' ':
     decoder.decodeImage(opencv.image());
   break;
   case 'f':
   PImage frame = loadImage(qrPath);
   decoder.decodeImage( frame );
   break;
  }
}

void decoderEvent(Decoder decoder){
  statusMsg = decoder.getDecodedString();
}

Resources