Jean-Yves Didier

Ajout barcode

#include "barcodereader.h"
#include <cstdlib>
#ifndef MAX
#define MAX(A,B) (((A)>(B))?(A):(B))
#endif
#ifndef MIN
#define MIN(A,B) (((A)<(B))?(A):(B))
#endif
/* correspondances valeurs
*
* 3 => Début, Fin !
* 10 => Milieu !
*
* 13 => 0L !
* 25 => 1L
* 19 => 2L
* 61 => 3L
* 35 => 4L
* 49 => 5L
* 47 => 6L
* 59 => 7L
* 55 => 8L
* 11 => 9L !
*
* 114 => 0R
* 102 => 1R
* 108 => 2R
* 66 => 3R
* 92 => 4R
* 78 => 5R
* 80 => 6R
* 68 => 7R
* 72 => 8R
* 116 => 9R
*
*/
BarCodeReader::BarCodeReader(QObject* parent) : QObject(parent)
{
samplingValues = 0;
}
unsigned char BarCodeReader::readPart(int start, int stop)
{
unsigned char res = 0;
for (int i = start; i <= stop ; i++)
{
res = res << 1;
if (samplingValue[i*(idxStop-idxMin)/95] < mean)
res++
}
return res;
}
void BarCodeReader::readBarCode(IplImage* img)
{
if (samplingLine.isNull())
return ;
if (img->nChannels != 1)
return ;
int x1 = samplingLine.x1();
int x2 = samplingLine.x2();
int y1 = samplingLine.y1();
int y2 = samplingLine.y2();
if (x1 < 0 || x1 > img->width || x2 < 0 || x2 > img->width ||
y1 < 0 || y1 > img->height ||y2 < 0 || y2 > img->height)
return ;
int adx = abs(samplingLine.dx());
int ady = abs(samplingLine.dy());
int length = MAX(adx,ady);
float mean=0;
int i;
// first sampling along line and threshold computation
for (i = 0 ; i <= length ; i++)
{
samplingValues[i] = cvmGet(img, i*(x2-x1)/length,
i*(y2-y1)/length);
mean = (i*mean + samplingValues[i]) / (float)(i+1);
}
// then find the extremas
idxStart=length;
idxStop=0;
for (i=0; i <= length; i++)
{
if (samplingValues[i] < mean )
{
if (idxStart > i)
idxStart = i;
idxStop = i;
}
}
// we check D,M,F in the barcode
if (readPart(0,2) != 3)
{
std::cout << "D is not properly detected" << std::endl;
}
if (readPart(93,95) != 3)
{
std::cout << "F is not properly detected" << std::endl;
}
if (readPart(45,49) != 5)
{
std::cout << "M is not properly detected" << std::endl;
}
QString barcodeL, barcodeR;
for (i=0; i < 6; i++)
{
unsigned char r = readPart(3+i*7,9+i*7);
switch r {
case 13: barcodeL += "0"; break;
case 25: barcodeL += "1"; break;
case 19: barcodeL += "2"; break;
case 61: barcodeL += "3"; break;
case 35: barcodeL += "4"; break;
case 49: barcodeL += "5"; break;
case 47: barcodeL += "6"; break;
case 59: barcodeL += "7"; break;
case 55: barcodeL += "8"; break;
case 11: barcodeL += "9"; break;
}
r = readPart(50 + i*7, 56 + i*7);
switch r {
case 114: barcodeR += "0"; break;
case 102: barcodeR += "1"; break;
case 108: barcodeR += "2"; break;
}
}
void BarCodeReader::setLine(QLine l)
{
samplingLine = l;
if (samplingValues)
delete samplingValues;
samplingValues = new int(MAX(abs(samplingLine.dx()),
abs(samplingLine.dy()))+1);
}
#ifndef __BARCODEREADER_H__
#define __BARCODEREADER_H__
#include <QObject>
#include <QLine>
#include <opencv/cv.h>
class BarCodeReader : public QObject
{
public:
BarCodeReader(QObject* parent);
public slots:
void readBarCode(IplImage* img);
void setLine(QLine l);
signals:
void sendBarCode(QString);
private:
unsigned char readPart(int start,int stop);
QLine samplingLine;
int* samplingValues;
float mean;
int idxStart;
int idxStop;
};
#endif //__BARCODEREADER_H__