How to import Zxing library to OpenCV

1,448 views
Skip to first unread message

Jayneil Dalal

unread,
Nov 22, 2011, 11:18:27 AM11/22/11
to zx...@googlegroups.com
Hey,

I was wondering if there was a way we could import the Zxing library to OpenCV? As you might know that OpenCV is one of the world's most popular libraries for Image Processing and they do not have a very good module for scanning or decoding Qr codes. So this would be a good initiative to do the same. I am willing to do it if someone can give me proper guidance.

Cheers.
Jayneil.

Daniel Switkin

unread,
Nov 22, 2011, 12:39:41 PM11/22/11
to Jayneil Dalal, zx...@googlegroups.com
I'm not familiar with their API but it looks like it's in C++. I imagine you could write a wrapper around ZXing's C++ QR decoder to pass image data in. They have a forum you can post to which might help:


Let us know if you get it working.

Daniel

Gary Bradski

unread,
May 29, 2013, 11:02:20 PM5/29/13
to zx...@googlegroups.com
Answer is:
/**
 * Gary Bradski, Reading 1D barcodes
 *   License BSD, (c) 2013
 *
 *   Working example of how to call zxing using OpenCV 2.4+ cv::Mat
 *
 * Calling example, this one for 128 barcodes:
 *
 *   Code128Reader cr; //Instantiate a zxing barcode reader, int this case for 128 barcodes
 *   ... by magic, I find, rectify and islotate a barcode into cv::Mat barcodeImage
 *   decode_image(&cr, barcodeImage);
 *
 */

#include <string>
#include <fstream>
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

//////////////ZXING BARCODE READER////////////////////////////////////////////////////
#include <zxing/LuminanceSource.h>
#include <zxing/MultiFormatReader.h>
#include <zxing/oned/OneDReader.h>
#include <zxing/oned/EAN8Reader.h>
#include <zxing/oned/EAN13Reader.h>
#include <zxing/oned/Code128Reader.h>
#include <zxing/datamatrix/DataMatrixReader.h>
#include <zxing/qrcode/QRCodeReader.h>
#include <zxing/aztec/AztecReader.h>
#include <zxing/common/GlobalHistogramBinarizer.h>
#include <zxing/Exception.h>


using namespace zxing;
using namespace oned;
using namespace datamatrix;
using namespace qrcode;
using namespace aztec;

class OpenCVBitmapSource : public LuminanceSource
{
private:
    cv::Mat m_pImage;

public:
    OpenCVBitmapSource(cv::Mat &image)
    : LuminanceSource(image.cols, image.rows)
    {
        m_pImage = image.clone();
    }

    ~OpenCVBitmapSource()
    {
    }

    int getWidth() const { return m_pImage.cols; }
    int getHeight() const { return m_pImage.rows; }

    ArrayRef<char> getRow(int y, ArrayRef<char> row) const
    {
        int width_ = getWidth();
        if (!row)
            row = ArrayRef<char>(width_);
        const char *p = m_pImage.ptr<char>(y);
        for(int x = 0; x<width_; ++x, ++p)
            row[x] = *p;
        return row;
    }

    ArrayRef<char> getMatrix() const
    {
        int width_ = getWidth();
        int height_ =  getHeight();
        ArrayRef<char> matrix = ArrayRef<char>(width_*height_);
        for (int y = 0; y < height_; ++y)
        {
            const char *p = m_pImage.ptr<char>(y);
            for(int x = 0; x < width_; ++x, ++p)
            {
                matrix[y*width_ + x] = *p;
            }
        }
        return matrix;
    }
    /*
    // The following methods are not supported by this demo (the DataMatrix Reader doesn't call these methods)
    bool isCropSupported() const { return false; }
    Ref<LuminanceSource> crop(int left, int top, int width, int height) {}
    bool isRotateSupported() const { return false; }
    Ref<LuminanceSource> rotateCounterClockwise() {}
    */
};

void decode_image(Reader *reader, cv::Mat &image)
{
    try
    {
        Ref<OpenCVBitmapSource> source(new OpenCVBitmapSource(image));
        Ref<Binarizer> binarizer(new GlobalHistogramBinarizer(source));
        Ref<BinaryBitmap> bitmap(new BinaryBitmap(binarizer));
        Ref<Result> result(reader->decode(bitmap, DecodeHints(DecodeHints::TRYHARDER_HINT)));//+DecodeHints::DEFAULT_HINT)));
        cout << result->getText()->getText() << endl;
    }
    catch (zxing::Exception& e)
    {
        cerr << "Error: " << e.what() << endl;
    }
}

...
Reply all
Reply to author
Forward
0 new messages