Search code examples
c++opencvqr-codezbar

How to configure c++ Zbar scanner to decode only QR-Code data type?


I am using Zbar C++ library to decode QRCode,using this tutorial: https://www.learnopencv.com/barcode-and-qr-code-scanner-using-zbar-and-opencv/

Here I have to decode only QRCode from an image however using this tutorial it decodes both QRcode and barcodes from an Image.

In tutorial it says to decode only QRCode we have to Configure Zbar Imagescanner properly.

In tutorial they are using following configuration to decode both QRCode and Barcodes ImageScanner scanner; scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

So to decode only QRCode I am using following configuration: ImageScanner scanner; scanner.set_config(ZBAR_QRCODE, ZBAR_CFG_ENABLE, 1);

But using this Zbar configuration still it decodes both QRCode and barcodes data.I am getting Decoded Data type as EAN-13 and QR-Code.

How I can configure Zbar sccanner properly so it decodes only QR-Code Data type?

//Reference:https://www.learnopencv.com/opencv-qr-code-scanner-c-and-python/

#include <iostream>
#include <algorithm>
#include <vector>
#include <zbar.h>

#include <opencv2/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;
using namespace zbar;

typedef struct
{
  string type;
  string data;
  vector <Point> location;
}decodedObject;

// Find and decode barcodes and QR codes
void decode(Mat &im, vector<decodedObject>&decodedObjects)
{

  // Create zbar scanner
  ImageScanner scanner;

  // Configure scanner
  scanner.set_config(ZBAR_QRCODE, ZBAR_CFG_ENABLE, 1);

  // Convert image to grayscale
  Mat imGray;
  cvtColor(im, imGray,CV_BGR2GRAY);

  // Wrap image data in a zbar image
  Image image(im.cols, im.rows, "Y800", (uchar *)imGray.data, im.cols * im.rows);

  // Scan the image for barcodes and QRCodes
  int n = scanner.scan(image);

  // Print results
  for(Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol)
  {
    decodedObject obj;

    obj.type = symbol->get_type_name();
    obj.data = symbol->get_data();

    // Print type and data
    cout << "Type : " << obj.type << endl;
    cout << "Data : " << obj.data << endl << endl;
    decodedObjects.push_back(obj);
  }
}

int main(int argc, char *argv[])
{

  // Read image
  string imagepath = argv[1];
  Mat im = imread(imagepath);

   // Variable for decoded objects
   vector<decodedObject> decodedObjects;

   // Find and decode barcodes and QR codes
   decode(im, decodedObjects);

   return 0;
 }

testImage.png


Resulted Output:
Type : QR-Code
Data : http://LearnOpenCV.com

Type : EAN-13
Data : 0036000291452

Expected Output:
Type : QR-Code
Data : http://LearnOpenCV.com

Solution

  • I assume you need to disable all first with

        // disable all
        scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 0);
    
        // enable qr
        scanner.set_config(ZBAR_QRCODE, ZBAR_CFG_ENABLE, 1);