In this example we will use the ZXing library for QR code reading.
Please download the Cinder ZXing block from GitHub and unpack it to the blocks
folder: https://github.com/dawidgorny/Cinder-ZXing
We will now create a QR code reader:
$(CINDER_PATH)/blocks/zxing/include
$(CINDER_PATH)/blocks/zxing/lib/macosx/libzxing.a
. For a debug configuration, use $(CINDER_PATH)/blocks/zxing/lib/macosx/libzxing_d.a
.libiconv.dylib
library to the Link Binary With Libraries
list:#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"
Capture mCapture; Surface8u mCaptureImg; gl::Texture mCaptureTex; bool mDetected; string mData;
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; }
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; } }
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); }
3.15.239.214