2011-11-16 13:40:46 +00:00
|
|
|
#include "qzxing.h"
|
|
|
|
|
|
|
|
#include <zxing/common/GlobalHistogramBinarizer.h>
|
2013-07-27 17:01:25 +00:00
|
|
|
#include <zxing/Binarizer.h>
|
2011-11-16 13:40:46 +00:00
|
|
|
#include <zxing/BinaryBitmap.h>
|
|
|
|
#include <zxing/MultiFormatReader.h>
|
|
|
|
#include <zxing/DecodeHints.h>
|
|
|
|
#include "CameraImageWrapper.h"
|
2012-05-14 14:01:57 +00:00
|
|
|
#include "imagehandler.h"
|
2013-07-28 17:29:54 +00:00
|
|
|
#include <QTime>
|
2011-11-16 13:40:46 +00:00
|
|
|
|
|
|
|
using namespace zxing;
|
|
|
|
|
2011-11-29 14:38:16 +00:00
|
|
|
QZXing::QZXing(QObject *parent) : QObject(parent)
|
2011-11-16 13:40:46 +00:00
|
|
|
{
|
|
|
|
decoder = new MultiFormatReader();
|
2013-07-28 17:29:54 +00:00
|
|
|
/*setDecoder(DecoderFormat_QR_CODE |
|
2011-11-29 14:38:16 +00:00
|
|
|
DecoderFormat_DATA_MATRIX |
|
|
|
|
DecoderFormat_UPC_E |
|
|
|
|
DecoderFormat_UPC_A |
|
|
|
|
DecoderFormat_EAN_8 |
|
|
|
|
DecoderFormat_EAN_13 |
|
|
|
|
DecoderFormat_CODE_128 |
|
|
|
|
DecoderFormat_CODE_39 |
|
2012-05-14 14:01:57 +00:00
|
|
|
DecoderFormat_ITF |
|
2013-07-28 17:29:54 +00:00
|
|
|
DecoderFormat_Aztec);*/
|
2012-05-14 14:01:57 +00:00
|
|
|
imageHandler = new ImageHandler();
|
2011-11-16 13:40:46 +00:00
|
|
|
}
|
|
|
|
|
2013-07-28 19:28:58 +00:00
|
|
|
QZXing::QZXing(QZXing::DecoderFormat decodeHints, QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
decoder = new MultiFormatReader();
|
|
|
|
imageHandler = new ImageHandler();
|
|
|
|
|
|
|
|
setDecoder(decodeHints);
|
|
|
|
}
|
|
|
|
|
2013-07-28 17:29:54 +00:00
|
|
|
void QZXing::setDecoder(const uint &hint)
|
2011-11-16 13:40:46 +00:00
|
|
|
{
|
2013-07-14 18:29:34 +00:00
|
|
|
unsigned int newHints = 0;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2013-07-14 18:29:34 +00:00
|
|
|
if(hint & DecoderFormat_Aztec)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::AZTEC_HINT;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2013-07-14 18:29:34 +00:00
|
|
|
if(hint & DecoderFormat_CODABAR)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::CODABAR_HINT;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2013-07-14 18:29:34 +00:00
|
|
|
if(hint & DecoderFormat_CODE_39)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::CODE_39_HINT;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2013-07-14 18:29:34 +00:00
|
|
|
if(hint & DecoderFormat_CODE_93)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::CODE_93_HINT;
|
2013-07-14 18:29:34 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_CODE_128)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::CODE_128_HINT;
|
2013-07-14 18:29:34 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_DATA_MATRIX)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::DATA_MATRIX_HINT;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_EAN_8)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::EAN_8_HINT;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_EAN_13)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::EAN_13_HINT;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2013-07-14 18:29:34 +00:00
|
|
|
if(hint & DecoderFormat_ITF)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::ITF_HINT;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2013-07-14 18:29:34 +00:00
|
|
|
if(hint & DecoderFormat_MAXICODE)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::MAXICODE_HINT;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2013-07-14 18:29:34 +00:00
|
|
|
if(hint & DecoderFormat_PDF_417)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::PDF_417_HINT;
|
2011-11-16 13:40:46 +00:00
|
|
|
|
2013-07-14 18:29:34 +00:00
|
|
|
if(hint & DecoderFormat_QR_CODE)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::QR_CODE_HINT;
|
2013-07-14 18:29:34 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_RSS_14)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::RSS_14_HINT;
|
2013-07-14 18:29:34 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_RSS_EXPANDED)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::RSS_EXPANDED_HINT;
|
2013-07-14 18:29:34 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_UPC_A)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::UPC_A_HINT;
|
2013-07-14 18:29:34 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_UPC_E)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::UPC_E_HINT;
|
2013-07-14 18:29:34 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_UPC_EAN_EXTENSION)
|
2013-07-28 19:28:58 +00:00
|
|
|
newHints |= DecodeHints::UPC_EAN_EXTENSION_HINT;
|
2012-05-13 20:47:37 +00:00
|
|
|
|
2013-07-28 17:29:54 +00:00
|
|
|
enabledDecoders = newHints;
|
|
|
|
|
|
|
|
emit enabledFormatsChanged();
|
2011-11-16 13:40:46 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 16:57:45 +00:00
|
|
|
QString QZXing::decodeImage(QImage image, int maxWidth, int maxHeight, bool smoothTransformation)
|
2011-11-16 13:40:46 +00:00
|
|
|
{
|
2013-08-02 16:52:30 +00:00
|
|
|
QTime t;
|
|
|
|
t.start();
|
|
|
|
Ref<Result> res;
|
|
|
|
emit decodingStarted();
|
2013-08-02 11:44:27 +00:00
|
|
|
|
2013-08-02 16:52:30 +00:00
|
|
|
if(image.isNull())
|
2013-07-30 16:57:45 +00:00
|
|
|
{
|
2013-08-02 16:52:30 +00:00
|
|
|
emit decodingFinished(false);
|
|
|
|
processingTime = -1;
|
2013-07-30 16:57:45 +00:00
|
|
|
return "";
|
|
|
|
}
|
2013-07-28 17:29:54 +00:00
|
|
|
|
2013-08-02 16:52:30 +00:00
|
|
|
try{
|
|
|
|
CameraImageWrapper* ciw;
|
|
|
|
|
|
|
|
if(maxWidth > 0 || maxHeight > 0)
|
2013-07-30 16:57:45 +00:00
|
|
|
{
|
2013-08-02 16:52:30 +00:00
|
|
|
ciw = new CameraImageWrapper();
|
|
|
|
ciw->setSmoothTransformation(smoothTransformation);
|
|
|
|
ciw->setImage(image, maxWidth, maxHeight);
|
2013-07-30 16:57:45 +00:00
|
|
|
}
|
2013-08-02 16:52:30 +00:00
|
|
|
else
|
|
|
|
ciw = new CameraImageWrapper(image);
|
2013-07-30 16:57:45 +00:00
|
|
|
|
2013-08-02 16:52:30 +00:00
|
|
|
Ref<LuminanceSource> imageRef(ciw);
|
|
|
|
GlobalHistogramBinarizer* binz = new GlobalHistogramBinarizer(imageRef);
|
2013-07-30 17:59:51 +00:00
|
|
|
|
2013-08-02 16:52:30 +00:00
|
|
|
Ref<Binarizer> bz (binz);
|
|
|
|
BinaryBitmap* bb = new BinaryBitmap(bz);
|
2013-07-30 17:59:51 +00:00
|
|
|
|
2013-08-02 16:52:30 +00:00
|
|
|
Ref<BinaryBitmap> ref(bb);
|
2013-07-30 17:59:51 +00:00
|
|
|
|
2013-08-02 16:52:30 +00:00
|
|
|
res = ((MultiFormatReader*)decoder)->decode(ref, DecodeHints((int)enabledDecoders));
|
2013-07-30 17:59:51 +00:00
|
|
|
|
2013-08-02 16:52:30 +00:00
|
|
|
QString string = QString(res->getText()->getText().c_str());
|
|
|
|
processingTime = t.elapsed();
|
|
|
|
emit tagFound(string);
|
|
|
|
emit decodingFinished(true);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
catch(zxing::Exception& e)
|
|
|
|
{
|
|
|
|
emit decodingFinished(false);
|
|
|
|
processingTime = -1;
|
|
|
|
return "";
|
2011-11-16 13:40:46 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-14 14:01:57 +00:00
|
|
|
|
2013-07-30 16:57:45 +00:00
|
|
|
QString QZXing::decodeImageFromFile(QString imageFilePath, int maxWidth, int maxHeight, bool smoothTransformation)
|
2013-07-28 17:29:54 +00:00
|
|
|
{
|
2013-08-02 16:52:30 +00:00
|
|
|
//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.
|
2013-07-30 16:57:45 +00:00
|
|
|
return decodeImage(QImage(imageFilePath), maxWidth, maxHeight, smoothTransformation);
|
2013-07-28 17:29:54 +00:00
|
|
|
}
|
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
QString QZXing::decodeImageQML(QObject *item)
|
|
|
|
{
|
|
|
|
return decodeSubImageQML(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QZXing::decodeSubImageQML(QObject* item,
|
|
|
|
const double offsetX, const double offsetY,
|
|
|
|
const double width, const double height)
|
|
|
|
{
|
|
|
|
if(item == NULL)
|
|
|
|
{
|
|
|
|
emit decodingFinished(false);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage img = ((ImageHandler*)imageHandler)->extractQImage(item, offsetX, offsetY, width, height);
|
|
|
|
|
|
|
|
return decodeImage(img);
|
|
|
|
}
|
|
|
|
|
2013-07-28 17:29:54 +00:00
|
|
|
int QZXing::getProcessTimeOfLastDecoding()
|
|
|
|
{
|
|
|
|
return processingTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint QZXing::getEnabledFormats() const
|
|
|
|
{
|
|
|
|
return enabledDecoders;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-14 14:01:57 +00:00
|
|
|
|