Rejected the plan of adding the threads for now. Fixed hudge memory leak. Fixed erroneous code in UPCE decoding.

This commit is contained in:
favoritas37 2013-08-02 19:52:30 +03:00
parent b12aec0b55
commit 4f5efb0436
6 changed files with 63 additions and 139 deletions

View File

@ -2,10 +2,8 @@
#include <QColor>
#include <QApplication>
#include <QDesktopWidget>
#include <QDebug>
#include <vector>
std::vector<unsigned char*> allocatedObjects;
#include <vld.h>
CameraImageWrapper::CameraImageWrapper() : LuminanceSource(0,0), isSmoothTransformationEnabled(false)
{
@ -23,13 +21,6 @@ CameraImageWrapper::CameraImageWrapper(CameraImageWrapper& otherInstance) : Lumi
CameraImageWrapper::~CameraImageWrapper()
{
for(int i=0; i<allocatedObjects.size(); i++)
{
qDebug() << "Deallocating...";
delete allocatedObjects[i];
}
allocatedObjects.clear();
}
int CameraImageWrapper::getWidth() const
@ -62,9 +53,6 @@ unsigned char* CameraImageWrapper::copyMatrix() const
}
}
qDebug() << "Adding pointer";
allocatedObjects.push_back(newMatrix);
return newMatrix;
}
@ -75,11 +63,11 @@ bool CameraImageWrapper::setImage(QString fileName, int maxWidth, int maxHeight)
if(!isLoaded)
return false;
scale(maxWidth, maxHeight);
width = image.width();
height = image.height();
scale(maxWidth, maxHeight);
return true;
}
@ -90,11 +78,11 @@ bool CameraImageWrapper::setImage(QImage newImage, int maxWidth, int maxHeight)
image = newImage.copy();
scale(maxWidth, maxHeight);
width = image.width();
height = image.height();
scale(maxWidth, maxHeight);
return true;
}
@ -138,10 +126,6 @@ ArrayRef<char> CameraImageWrapper::getMatrix() const
char* matrix = new char[width*height];
char* m = matrix;
qDebug() << "getting matrix";
allocatedObjects.push_back((unsigned char*)matrix);
for(int y=0; y<height; y++)
{
ArrayRef<char> tmpRow;
@ -157,7 +141,13 @@ ArrayRef<char> CameraImageWrapper::getMatrix() const
}
//pMatrix = matrix;
return ArrayRef<char>(matrix, width*height);
ArrayRef<char> arr = ArrayRef<char>(matrix, width*height);
if(matrix)
delete matrix;
return arr;
}
void CameraImageWrapper::setSmoothTransformation(bool enable)
@ -167,13 +157,10 @@ void CameraImageWrapper::setSmoothTransformation(bool enable)
void CameraImageWrapper::scale(int maxWidth, int maxHeight)
{
if((maxWidth != -1 || maxHeight != -1) && (image.width() > maxWidth || image.height() > maxHeight))
{
qDebug() << "Scaling image to: width " << maxWidth << ", " << ", maxHeight: " << maxHeight;
if((maxWidth != 1 || maxHeight != 1) && (image.width() > maxWidth || image.height() > maxHeight))
image = image.scaled(
maxWidth != -1 ? maxWidth : image.width(),
maxHeight != -1 ? maxHeight : image.height(),
Qt::KeepAspectRatio,
isSmoothTransformationEnabled ? Qt::SmoothTransformation : Qt::FastTransformation);
}
}

View File

@ -4,7 +4,6 @@
#include <QImage>
#include <QString>
#include <zxing/zxing/LuminanceSource.h>
#include <vector>
using namespace zxing;

View File

@ -116,19 +116,6 @@ public slots:
*/
void setDecoder(const uint& hint);
/**
* If set to true, the decoding takes place in a different thread.
* Note that if threading is used, the decodeImage function returns
* an empty string. To acquire the data the user needs to connect to the signal
* tagFound.
*
* This option was created for convinience in case the decoding functions are used
* at a low-power device with big images.
*
* (PS. not sure if it gives any advantage, it will be investigated further in the future)
*/
void setIsThreaded(bool enabledThreads);
signals:
void decodingStarted();
void decodingFinished(bool succeeded);
@ -137,8 +124,6 @@ signals:
private:
void* decoder;
void* worker;
void* thread;
DecoderFormatType enabledDecoders;
QObject* imageHandler;
int processingTime;

View File

@ -9,6 +9,8 @@ DEFINES += QZXING_LIBRARY \
INCLUDEPATH += $$PWD \
$$PWD/zxing
HEADERS += $$PWD/QZXing_global.h \
HEADERS += $$PWD/QZXing_global.h \
$$PWD/CameraImageWrapper.h \
$$PWD/imagehandler.h \
@ -121,8 +123,7 @@ HEADERS += $$PWD/QZXing_global.h \
$$PWD/zxing/bigint/BigIntegerUtils.hh \
$$PWD/zxing/bigint/BigIntegerLibrary.hh \
$$PWD/zxing/bigint/BigIntegerAlgorithms.hh \
$$PWD/zxing/bigint/BigInteger.hh \
qzxing/qzxingworker_p.h
$$PWD/zxing/bigint/BigInteger.hh
SOURCES += $$PWD/CameraImageWrapper.cpp \
$$PWD/qzxing.cpp \
@ -226,8 +227,7 @@ SOURCES += $$PWD/CameraImageWrapper.cpp \
$$PWD/zxing/bigint/BigUnsigned.cc \
$$PWD/zxing/bigint/BigIntegerUtils.cc \
$$PWD/zxing/bigint/BigIntegerAlgorithms.cc \
$$PWD/zxing/bigint/BigInteger.cc \
qzxing/qzxingworker_p.cpp
$$PWD/zxing/bigint/BigInteger.cc
symbian {
TARGET.UID3 = 0xE618743C

View File

@ -9,9 +9,6 @@
#include "imagehandler.h"
#include <QTime>
#include "qzxingworker_p.h"
#include <QThread>
using namespace zxing;
QZXing::QZXing(QObject *parent) : QObject(parent)
@ -28,16 +25,6 @@ QZXing::QZXing(QObject *parent) : QObject(parent)
DecoderFormat_ITF |
DecoderFormat_Aztec);*/
imageHandler = new ImageHandler();
QZXingWorker_p* worker_p = new QZXingWorker_p();
connect(worker_p, SIGNAL(decodingFinished(bool)), this, SIGNAL(decodingFinished(bool)));
connect(worker_p, SIGNAL(decodingStarted()), this, SIGNAL(decodingStarted()));
connect(worker_p, SIGNAL(tagFound(QString)), this, SIGNAL(tagFound(QString)));
thread = new QThread;
worker_p->moveToThread(static_cast<QThread*>(thread));
worker = worker_p;
}
QZXing::QZXing(QZXing::DecoderFormat decodeHints, QObject *parent) : QObject(parent)
@ -108,97 +95,61 @@ void QZXing::setDecoder(const uint &hint)
emit enabledFormatsChanged();
}
void QZXing::setIsThreaded(bool enabledThreads)
{
isThreaded = enabledThreads;
}
QString QZXing::decodeImage(QImage image, int maxWidth, int maxHeight, bool smoothTransformation)
{
QTime t;
t.start();
Ref<Result> res;
emit decodingStarted();
qDebug() << "decoding";
isThreaded = false;
if(isThreaded)
if(image.isNull())
{
// QZXingWorker_p* worker = new QZXingWorker_p();
// connect(worker, SIGNAL(decodingFinished(bool)), this, SIGNAL(decodingFinished(bool)));
// connect(worker, SIGNAL(decodingStarted()), this, SIGNAL(decodingStarted()));
// connect(worker, SIGNAL(tagFound(QString)), this, SIGNAL(tagFound(QString)));
//connect(worker, SIGNAL(quitThread()), thread, SLOT(quit()));
//connect(thread, SIGNAL(finished()), worker, SLOT(deleteLater()));
//connect(thread, SIGNAL(finished()), SLOT(deleteLater()));
QZXingWorker_p* worker_p = static_cast<QZXingWorker_p*>(worker);
worker_p->setData(&processingTime, image, maxWidth, maxHeight, smoothTransformation, decoder, enabledDecoders);
static_cast<QThread*>(thread)->start();
QMetaObject::invokeMethod(worker_p, "decode", Qt::QueuedConnection);
emit decodingFinished(false);
processingTime = -1;
return "";
}
else
try{
CameraImageWrapper* ciw;
if(maxWidth > 0 || maxHeight > 0)
{
ciw = new CameraImageWrapper();
ciw->setSmoothTransformation(smoothTransformation);
ciw->setImage(image, maxWidth, maxHeight);
}
else
ciw = new CameraImageWrapper(image);
Ref<LuminanceSource> imageRef(ciw);
GlobalHistogramBinarizer* binz = new GlobalHistogramBinarizer(imageRef);
Ref<Binarizer> bz (binz);
BinaryBitmap* bb = new BinaryBitmap(bz);
Ref<BinaryBitmap> ref(bb);
res = ((MultiFormatReader*)decoder)->decode(ref, DecodeHints((int)enabledDecoders));
QString string = QString(res->getText()->getText().c_str());
processingTime = t.elapsed();
emit tagFound(string);
emit decodingFinished(true);
return string;
}
catch(zxing::Exception& e)
{
QTime t;
t.start();
Ref<Result> res;
emit decodingStarted();
if(image.isNull())
{
emit decodingFinished(false);
processingTime = -1;
return "";
}
CameraImageWrapper* ciw = NULL;
try{
if(maxWidth > 0 || maxHeight > 0)
{
ciw = new CameraImageWrapper();
ciw->setSmoothTransformation(smoothTransformation);
ciw->setImage(image, maxWidth, maxHeight);
}
else
ciw = new CameraImageWrapper(image);
Ref<LuminanceSource> imageRef(ciw);
GlobalHistogramBinarizer binz(imageRef);
Ref<Binarizer> bz (&binz);
BinaryBitmap bb(bz);
Ref<BinaryBitmap> ref(&bb);
res = ((MultiFormatReader*)decoder)->decode(ref, DecodeHints((int)enabledDecoders));
QString string = QString(res->getText()->getText().c_str());
processingTime = t.elapsed();
emit tagFound(string);
emit decodingFinished(true);
delete ciw;
return string;
}
catch(zxing::Exception& e)
{
if(!ciw)
delete ciw;
emit decodingFinished(false);
processingTime = -1;
return "";
}
emit decodingFinished(false);
processingTime = -1;
return "";
}
}
QString QZXing::decodeImageFromFile(QString imageFilePath, int maxWidth, int maxHeight, bool smoothTransformation)
{
//used to have a check if this image exists
//but was removed because if the image file path doesn't point to a valid image
// then the QImage::isNull will return true and the decoding will fail eitherway.
//used to have a check if this image exists
//but was removed because if the image file path doesn't point to a valid image
// then the QImage::isNull will return true and the decoding will fail eitherway.
return decodeImage(QImage(imageFilePath), maxWidth, maxHeight, smoothTransformation);
}

View File

@ -136,7 +136,9 @@ Ref<String> UPCEReader::convertUPCEtoUPCA(Ref<String> const& upce_) {
result.append(1, lastChar);
break;
}
result.append(1, upce[7]);
//result.append(1, upce[7]);
char c = (char)(*(upce.data()+7));//upce[upce.size()];
result.append(1, c);
return Ref<String>(new String(result));
}