QR Code Scanner with Qt Framework + OpenCV

I need a program for scanning QR code and processing the data. For this purpose, there are several libraries which can help saving my time for developing it. For instance, ZXing library, or ZBar. For web developers, there are also corresponding library for scanning QR code, that are both client side (JavaScript) and server side (PHP).

As of now, I develop the desktop app. I utilize the Qt Framework, paired with OpenCV libs which handles the image processing. Here’s the snippet:

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = AbsensiQr
TEMPLATE = app

SOURCES += main.cpp\
presensi.cpp \
use_cvsnakeimage.cpp

HEADERS += presensi.h \
use_cvsnakeimage.h

FORMS += presensi.ui

INCLUDEPATH += /usr/include/opencv
INCLUDEPATH += /usr/include/opencv2
LIBS += -L/usr/lib64 -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_legacy -lzbar -lzint

For the development requirements, you need OpenCV development files to be installed. In Ubuntu, you may want to install libopencv-dev. And in Fedora it might be opencv-devel package. The other one is ZBar devel package, which you can install it in Ubuntu with the name libzbar-dev or in Fedora with zbar-devel. As for libzint, you may omit it, since it is actually needed for generate the barcode, which I don’t use it in this source code.

void Presensi::on_btnOke_clicked()
{
this->clearAll();
//Kiem tra trang thai bat tat cua camera
if(capture == 0)
{
capture = cvCaptureFromCAM(CV_CAP_ANY);
if(capture)
{
cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT,1600);
cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH,1600);

timer = new QTimer(this);

QObject::connect(timer,SIGNAL(timeout()),this,SLOT(ProcessFrame()));
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(tampilJam()));

timer->start(1000/60);
this->tampilJam();
} else {
ui->lineNik->setText("Cannot connect to Camera!");
}
}

return ;
}

void Presensi::ProcessFrame()
{
IplImage* src = cvQueryFrame(capture);
QImage img_show = QImage((unsigned char*)(src->imageData),src->width,src->height,QImage::Format_RGB888).rgbSwapped();
ui->areaBarcode->setPixmap(QPixmap::fromImage(img_show,Qt::AutoColor).scaledToWidth(300));

int n = ScanIDCard(src);

if(n)
{
timer->stop();
cvReleaseCapture(&capture);
}
}

int Presensi::ScanIDCard(IplImage *src)
{
IplImage *img = cvCreateImage(cvSize(src->width,src->height), IPL_DEPTH_8U,1);

cvCvtColor( src, img, CV_RGB2GRAY );

//Su dung thu vien Zbar de giai ma

uchar* raw = (uchar *)img->imageData;
int width = img->width;
int height = img->height;

ImageScanner *scanner=new ImageScanner();

scanner->set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

Image *zimg=new Image(width,height,"Y800",raw,width*height);

int n=scanner->scan(*zimg);

if(n>0)
{
for(Image::SymbolIterator symbol = zimg->symbol_begin(); symbol != zimg->symbol_end(); ++symbol)
{
//Hien thi ket qua len Textbox
// ui->lineNik->setText(QString::fromStdString(symbol->get_data()));
this->tampilkanHasil(QString::fromStdString(symbol->get_data()));
}

}

else
{
// ui->lineNik->setText("Cannot Detect Code!");
this->tampilkanHasil("CHIGAU_DETA");
}

//Giai phong tai nguyen
zimg->set_data(NULL, 0);

return n;
}

The code was originally obtained from here: https://sites.google.com/site/embedded247/npcourse/impcourse/barcode_qrcode

And the full source code I have modified is in Github: https://github.com/baddwin/qt-presensi

Advertisements

Published by

Badwi

A Muslim, sober, a Javanese

Share your idea here (markdown enabled)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s