Reading QR code

In this example we will use the ZXing library for QR code reading.

Getting ready

Please download the Cinder ZXing block from GitHub and unpack it to the blocks folder: https://github.com/dawidgorny/Cinder-ZXing

How to do it…

We will now create a QR code reader:

  1. Add a header search path to the build settings of your project:
    $(CINDER_PATH)/blocks/zxing/include
  2. Add a path from the precompiled ZXing library to the build settings of your project: $(CINDER_PATH)/blocks/zxing/lib/macosx/libzxing.a. For a debug configuration, use $(CINDER_PATH)/blocks/zxing/lib/macosx/libzxing_d.a.
  3. Add Cinder ZXing block files to your project structure as follows:
    How to do it…
  4. Add the libiconv.dylib library to the Link Binary With Libraries list:
    How to do it…
  5. Add the necessary header files:
    #include "cinder/gl/Texture.h"
    #include "cinder/Surface.h"
    #include "cinder/Capture.h"
    
    #include <zxing/qrcode/QRCodeReader.h>
    #include <zxing/common/GlobalHistogramBinarizer.h>
    #include <zxing/Exception.h>
    #include <zxing/DecodeHints.h>
    
    #include "CinderZXing.h"
  6. Add the following members to your main application class:
    Capture     mCapture;
    Surface8u   mCaptureImg;
    gl::Texture mCaptureTex;
    bool        mDetected;
    string      mData;
  7. Inside the setup method, set window dimensions and initialize capturing from camera:
    setWindowSize(640, 480);
    
    mDetected = false;
    
    try {
        mCapture = Capture( 640, 480 );
        mCapture.start();
    }
    catch( ... ) {
        console() <<"Failed to initialize capture"<< std::endl;
    }
  8. Implement the update function as follows:
    if( mCapture && mCapture.checkNewFrame() ) {
        mCaptureImg = mCapture.getSurface();
        mCaptureTex = gl::Texture( mCaptureImg );
    
        mDetected = false;
    
    try {
            zxing::Ref<zxing::SurfaceBitmapSource> source(new zxing::SurfaceBitmapSource(mCaptureImg));
    
            zxing::Ref<zxing::Binarizer> binarizer(NULL);
            binarizer = new zxing::GlobalHistogramBinarizer(source);
    
            zxing::Ref<zxing::BinaryBitmap> image(new zxing::BinaryBitmap(binarizer));
            zxing::qrcode::QRCodeReader reader;
            zxing::DecodeHints hints(zxing::DecodeHints::BARCODEFORMAT_QR_CODE_HINT);
    
            zxing::Ref<zxing::Result> result( reader.decode(image, hints) );
    
            console() <<"READ("<< result->count() <<") : "<< result->getText()->getText() << endl;
    
    if( result->count() ) {
                mDetected = true;
                mData = result->getText()->getText();
            }
    
        } catch (zxing::Exception& e) {
            cerr <<"Error: "<< e.what() << endl;
        }
    
    }
  9. Implement the draw function as follows:
    gl::clear( Color( 0.1f, 0.1f, 0.1f ) );
    
    gl::color(Color::white());
    
    if(mCaptureTex) {
        gl::draw(mCaptureTex);
    
    }
    
    if(mDetected) {
        Vec2f pos = Vec2f( getWindowWidth()*0.5f, getWindowHeight()-100.f );
        gl::drawStringCentered(mData, pos);
    }

How it works…

We are using regular ZXing library methods. The SurfaceBitmapSource class delivered by the Cinder ZXing block provides integration with Cinder Surface type objects. While the QR code is detected and read, the mDetected flag is set to true and the read data is stored in the mData member.

How it works…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.239.214