Webcam Bar code/ QR code Read

Hi, I am doing a project on reading qr code using processing. I have tried using logitech web camera C310 (windows 7) and the laptop webcam for example qrcode and bar code but it does not detect the qr code although I already snap it. How can I solve this problem?

It states that Error: Invalid number of Finder Pattern detected.

Can I ask you to be more specific? For instance

  • Is the library youā€™re using for reading the code up to date?
  • Does the error occur in your own code, or the libraryā€™s code?
  • Which QR-Code library are you using? Thereā€™s a lot!

Also, if you havenā€™t already, take a look at our guide for asking questions:

(Donā€™t feel bad about it though, breaking down a problem and communicating it well is a skill that is really only learned through practice. Itā€™s something that I still have trouble with sometimes :sweat_smile:)

1 Like

Please provide the code you are using for this task as well as your testing image.

Kf

1 Like

Sorry for that. Ok, Iā€™m using the QRCode 0.3a by Daniel Shiffman and [Zxing For Processing (barcode library)]
[Rolf van Gelder] examples. Iā€™m using processing 3.4

The code for the QRcode example is:

/*
QRcode reader
 Generate images from a QRcode generator such as
 http://qrcode.kaywa.com/ and put them in this sketch's
 data folder.
 Press spacebar to read from the camera, generate an image,
 and scan for barcodes.  Press f to read from a file and scan.
 Press s for camera settings.
 Created 9 June 2007
 by Tom Igoe / Daniel Shiffman
 */


import processing.video.*;
import qrcodeprocessing.*;

Capture video;                                 // instance of the video capture library
String statusMsg = "Waiting for an image";     // a string to return messages:

// Decoder object from prdecoder library
Decoder decoder;

void setup() {
  size(400, 320);
  video = new Capture(this, 320, 240);
  video.start();

  // Create a decoder object
  decoder = new Decoder(this);
}

// When the decoder object finishes
// this method will be invoked.
void decoderEvent(Decoder decoder) {
  statusMsg = decoder.getDecodedString();
}

void captureEvent(Capture video) {
  video.read();
}

void draw() {
  background(0);

  // Display video
  image(video, 0, 0);
  // Display status
  text(statusMsg, 10, height-4);

  // If we are currently decoding
  if (decoder.decoding()) {
    // Display the image being decoded
    PImage show = decoder.getImage();
    image(show, 0, 0, show.width/4, show.height/4); 
    statusMsg = "Decoding image";
    for (int i = 0; i < (frameCount/2) % 10; i++) statusMsg += ".";
  }
}

void keyReleased() {
  // Depending on which key is hit, do different things:
  switch (key) {
  case ' ':        
    // Spacebar takes a picture and tests it:
    // copy it to the PImage savedFrame:
    PImage savedFrame = createImage(video.width, video.height, RGB);
    savedFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
    savedFrame.updatePixels();
    // Decode savedFrame
    decoder.decodeImage(savedFrame);
    break;
  case 'f':    // f runs a test on a file
    PImage preservedFrame = loadImage("qrcode.png");
    // Decode file
    decoder.decodeImage(preservedFrame);
    break;
  }
}

The barcode reader cam from the zxing library is:

/*****************************************************************************
 *
 *  barcodeReaderCam - v07/26/2018
 *
 *  An example of the use of the ZXING4P.barcodeReader() method.
 *
 *  Opens a webcam and tries to find barcodes in the image.
 *
 *  You can define for which barcode types it will scan.
 *  Per default it looks for all available types.
 *
 *  When a barcode is detected it will display the decoded text.
 *
 *  Run this sketch and hold a printed copy of a barcode in front of the cam.
 *
 *  Note: make sure your video image is NOT mirrored! It won't detect QRCodes
 *  that way...
 *
 *  Library page:
 *  http://cagewebdev.com/zxing4processing-processing-library/
 *
 *  (c) 2013-2018 Rolf van Gelder, http://cagewebdev.com, http://rvg.cage.nl
 * 
 *****************************************************************************/

// IMPORT THE ZXING4PROCESSING LIBRARY AND DECLARE A ZXING4P OBJECT
import com.cage.zxing4p3.*;
ZXING4P zxing4p;

// PROCESSING VIDEO LIBRARY
import processing.video.*;
Capture video;

// PIMAGE WITH THE BARCODES TO DETECT
PImage scanArea;

// TEXT THAT WAS FOUND
String decodedText;
String latestDecodedText = "";

// FORMAT OF THE LATEST FOUND BARCODE
String barcodeFormat = "";

int    txtWidth;

// FOR THE VIEW FINDER AND SCAN AREA
int    frameWidth  = 240;
int    frameHeight = 260;
int    lineSize    = 40;

// CORNERS OF THE VIEW FINDER
PVector ul, ur, ll, lr;

// LIST WITH BARCODE TYPES TO LOOK FOR
ArrayList<String> barcodeTypes = new ArrayList<String>();


/*****************************************************************************
 *
 *  SETUP
 *
 *****************************************************************************/
void setup() {
  size(640, 480);

  // LAYOUT
  textAlign(CENTER);
  textSize(30);

  // CREATE CAPTURE
  video = new Capture(this, width, height);

  // START CAPTURING
  video.start();

  // CREATE A NEW EN-/DECODER INSTANCE
  zxing4p = new ZXING4P();

  // DISPLAY VERSION INFORMATION
  zxing4p.version();

  // UPPER LEFT CORNER OF THE VIEW FINDER
  ul = new PVector((width - frameWidth) >> 1, (height - frameHeight) >> 1);
  // UPPER RIGHT CORNER OF THE VIEW FINDER
  ur = new PVector(ul.x + frameWidth, ul.y);
  // LOWER LEFT CORNER OF THE VIEW FINDER
  ll = new PVector(ul.x, ul.y + frameHeight);
  // LOWER RIGHT CORNER OF THE VIEW FINDER
  lr = new PVector(ur.x, ur.y + frameHeight);

  // AREA TO SCAN
  scanArea = createImage(frameWidth, frameHeight, RGB);

  // BARCODE TYPES THAT WILL BE SCANNED FOR
  // SUPPORTED BARCODE TYPES:
  // AZTEC
  // CODABAR
  // CODE_128
  // CODE_39
  // CODE_93
  // DATA_MATRIX
  // EAN_13
  // EAN_8
  // ITF
  // MAXICODE
  // PDF_417
  // QR_CODE
  // RSS_14
  // RSS_EXPANDED
  // UPC_A
  // UPC_E
  // UPC_EAN_EXTENSION  
  barcodeTypes.add("EAN_8");
  barcodeTypes.add("EAN_13");
  barcodeTypes.add("DATA_MATRIX");
  barcodeTypes.add("QR_CODE");
} // setup()


/*****************************************************************************
 *
 *  DRAW
 *
 *****************************************************************************/
void draw() { 
  background(0);

  // UPDATE CAPTURE
  if (video.available()) video.read();

  // DISPLAY VIDEO CAPTURE
  set(0, 0, video);

  if (frameCount % 50 == 0) {
    // COPY THE VIEW FINDER AREA TO THE SCAN AREA
    scanArea.copy(video, (int)ul.x, (int)ul.y, frameWidth, frameHeight, 0, 0, frameWidth, frameHeight);
  }

  // DISPLAY VIEW FINDER
  pushStyle();
  noFill();
  strokeWeight(4);
  stroke(255, 0, 0);
  line(ul.x, ul.y, ul.x + lineSize, ul.y);
  line(ul.x, ul.y, ul.x, ul.y + lineSize);
  line(ur.x - lineSize, ur.y, ur.x, ur.y);
  line(ur.x, ur.y, ur.x, ur.y + lineSize);
  line(ll.x, ll.y, ll.x + lineSize, ll.y);
  line(ll.x, ll.y - lineSize, ll.x, ll.y);
  line(lr.x - lineSize, lr.y, lr.x, lr.y);
  line(lr.x, lr.y - lineSize, lr.x, lr.y);
  popStyle();

  // DISPLAY LATEST DECODED TEXT AND FORMAT
  if (!latestDecodedText.equals("")) {
    pushStyle();
    txtWidth = int(textWidth(latestDecodedText + " (" + barcodeFormat + ")"));
    fill(0, 150);
    rect((width>>1) - (txtWidth>>1) - 5, 15, txtWidth + 10, 36);
    fill(255, 255, 0);
    text(latestDecodedText + " (" + barcodeFormat + ")", width>>1, 43);
    popStyle();
  } // if (!latestDecodedText.equals(""))

  // TRY TO DETECT AND DECODE A QRCODE IN THE VIDEO CAPTURE
  // multiFormatReader(PImage img, boolean tryHarder)
  // multiFormatReader(PImage img, boolean tryHarder, ArrayList<String> barcodeTypes)
  // img: image to scan for barcodes
  // tryHarder: false => fast detection (less accurate)
  //            true  => best detection (little slower)
  // barcodeTypes: barcode types to scan for
  try {
    // SCAN FOR ALL AVAILABLE BARCODE TYPES
    //decodedText = zxing4p.barcodeReader(scanArea, false);

    // SCAN FOR SPECIFIC BARCODE TYPES
    decodedText   = zxing4p.barcodeReader(scanArea, false, barcodeTypes);

    // GET THE TYPE OF THE LAST SCANNED BARCODE
    barcodeFormat = zxing4p.getBarcodeFormat();
  } 
  catch (Exception e) {
    // NOT FOUND
    decodedText = "";
  } // try

  if (!decodedText.equals("")) {
    // FOUND A BARCODE!
    if (latestDecodedText.equals("") || (!latestDecodedText.equals(decodedText))) {
      println("Zxing4processing detected: " + decodedText + " (" + barcodeFormat + ")");
    }
    latestDecodedText = decodedText;
  } // if (!decodedText.equals(""))
} // draw()


/*****************************************************************************
 *
 *  KEYBOARD HANDLER
 *
 *****************************************************************************/
void keyPressed() {
  switch(key) {
  case ' ':
    // RESET
    decodedText       = "";
    latestDecodedText = "";
    break;
  } // switch
} // keyPressed()

For the QR code example I press the spacebar to snap the image of the QR code from printed as attached below then the error shown. For the barcode example I didnā€™t press anything and just show any barcode on books and it donā€™t read them. Iā€™m new to the qr code and bar code processing. I just try them based on what I googled.

All the libraries that I added from the contribution manager is:

AP-Sync peace Nigel Tiany

AndroidCapture for Processing 2.0 JianbinQi

Arduino (Firmata) 9 David A. Mellis

GL Video 1.2.3 Gottfried Haider

Image processing algorithms 1.3.0 Nick ā€˜Milchreisā€™ MĆ¼ller

IPCapture 0.4.2 Stefano ā€œsingintimeā€ Baldan

meter 1.0c Bill (Papa) Kujawa

QRCode 0.3a Daniel Shiffman

Syphon 2.0 Andres Colubri

Video 1.0.1 The Processing Foundation

Video Export 0.2.3 Abe Pazos

VSync for Processing v0.1 Maximilian Ernestus

Zxing For Processing (barcode library) 3.5 Rolf van Gelder

The code that you show looks good; Daniel and Tom are great programmers.
You probably need to check your configuration, and set the right environment.
All that you need are the libraries:
Use this for ZXING:
https://repo1.maven.org/maven2/com/google/zxing/core/3.3.2/
You can use the processingā€™s ā€œadd library toolā€

  1. QRCode
  2. VIDEO
  3. Zxing for processing
    Both programs run fine:

Iā€™m using : java version ā€œ1.8.0_191ā€
and Processing: 3.4

Ā”Good luck! :+1:

ā€œWhen in doubt, use brute forceā€.

    ā€” [Ken Thompson](http://genius.cat-v.org/ken-thompson/)
1 Like