2014-02-16 12:46:57 +00:00
|
|
|
#include "qzxing.h"
|
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
#include <QtCore>
|
2014-02-16 12:46:57 +00:00
|
|
|
#include <zxing/common/GlobalHistogramBinarizer.h>
|
|
|
|
#include <zxing/Binarizer.h>
|
|
|
|
#include <zxing/BinaryBitmap.h>
|
|
|
|
#include <zxing/MultiFormatReader.h>
|
|
|
|
#include <zxing/DecodeHints.h>
|
|
|
|
#include "CameraImageWrapper.h"
|
|
|
|
|
|
|
|
using namespace zxing;
|
|
|
|
|
|
|
|
QZXing::QZXing(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
decoder = new MultiFormatReader();
|
2014-07-01 14:09:14 +00:00
|
|
|
setDecoder(DecoderFormat_QR_CODE);
|
2014-02-16 12:46:57 +00:00
|
|
|
/*setDecoder(DecoderFormat_QR_CODE |
|
|
|
|
DecoderFormat_DATA_MATRIX |
|
|
|
|
DecoderFormat_UPC_E |
|
|
|
|
DecoderFormat_UPC_A |
|
|
|
|
DecoderFormat_EAN_8 |
|
|
|
|
DecoderFormat_EAN_13 |
|
|
|
|
DecoderFormat_CODE_128 |
|
|
|
|
DecoderFormat_CODE_39 |
|
|
|
|
DecoderFormat_ITF |
|
|
|
|
DecoderFormat_Aztec);*/
|
|
|
|
}
|
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
QZXing::~QZXing() {
|
|
|
|
delete (MultiFormatReader*)decoder;
|
|
|
|
decoder = 0;
|
2014-02-16 12:46:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
void QZXing::setDecoder(DecoderFormatType hint)
|
2014-02-16 12:46:57 +00:00
|
|
|
{
|
2014-07-01 14:09:14 +00:00
|
|
|
DecodeHints newHints;
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
if(hint & DecoderFormat_QR_CODE)
|
|
|
|
newHints.addFormat((BarcodeFormat)BarcodeFormat_QR_CODE);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
if(hint & DecoderFormat_DATA_MATRIX)
|
|
|
|
newHints.addFormat((BarcodeFormat)BarcodeFormat_DATA_MATRIX);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
if(hint & DecoderFormat_UPC_E)
|
|
|
|
newHints.addFormat((BarcodeFormat)BarcodeFormat_UPC_E);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
if(hint & DecoderFormat_UPC_A)
|
|
|
|
newHints.addFormat((BarcodeFormat)BarcodeFormat_UPC_A);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_EAN_8)
|
2014-07-01 14:09:14 +00:00
|
|
|
newHints.addFormat((BarcodeFormat)BarcodeFormat_EAN_8);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
|
|
|
if(hint & DecoderFormat_EAN_13)
|
2014-07-01 14:09:14 +00:00
|
|
|
newHints.addFormat((BarcodeFormat)BarcodeFormat_EAN_13);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
if(hint & DecoderFormat_CODE_128)
|
|
|
|
newHints.addFormat((BarcodeFormat)BarcodeFormat_CODE_128);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
if(hint & DecoderFormat_CODE_39)
|
|
|
|
newHints.addFormat((BarcodeFormat)BarcodeFormat_CODE_39);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
if(hint & DecoderFormat_ITF)
|
|
|
|
newHints.addFormat((BarcodeFormat)BarcodeFormat_ITF);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
if(hint & DecoderFormat_Aztec)
|
|
|
|
newHints.addFormat((BarcodeFormat)BarcodeFormat_AZTEC);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
supportedFormats = newHints.getCurrentHint();
|
|
|
|
}
|
2014-02-16 12:46:57 +00:00
|
|
|
|
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
QString QZXing::decodeImage(QImage image)
|
|
|
|
{
|
|
|
|
Ref<Result> result;
|
|
|
|
emit decodingStarted();
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
try {
|
|
|
|
Ref<LuminanceSource> source(new CameraImageWrapper(image));
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
Ref<Binarizer> binarizer;
|
|
|
|
binarizer = new GlobalHistogramBinarizer(source);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
Ref<BinaryBitmap> binary(new BinaryBitmap(binarizer));
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
DecodeHints hints((int)supportedFormats);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
result = ((MultiFormatReader*)decoder)->decode(binary, hints);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
QString string = QString(result->getText()->getText().c_str());
|
|
|
|
emit tagFound(string);
|
|
|
|
emit decodingFinished(true);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
catch(zxing::Exception& e)
|
2014-02-16 12:46:57 +00:00
|
|
|
{
|
2014-07-01 14:09:14 +00:00
|
|
|
qDebug() << "[decodeImage()] Exception:" << e.what();
|
|
|
|
emit decodingFinished(false);
|
|
|
|
return "";
|
2014-02-16 12:46:57 +00:00
|
|
|
}
|
2014-07-01 14:09:14 +00:00
|
|
|
}
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
QVariantHash QZXing::decodeImageEx(QImage image)
|
|
|
|
{
|
|
|
|
QVariantHash resultMap;
|
|
|
|
Ref<Result> result;
|
|
|
|
emit decodingStarted();
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
try {
|
|
|
|
Ref<LuminanceSource> source(new CameraImageWrapper(image));
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
Ref<Binarizer> binarizer;
|
|
|
|
binarizer = new GlobalHistogramBinarizer(source);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
Ref<BinaryBitmap> binary(new BinaryBitmap(binarizer));
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
DecodeHints hints((int)supportedFormats);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
result = ((MultiFormatReader*)decoder)->decode(binary, hints);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
QString string = QString(result->getText()->getText().c_str());
|
|
|
|
QList<QVariant> points;
|
2014-02-16 12:46:57 +00:00
|
|
|
emit tagFound(string);
|
|
|
|
emit decodingFinished(true);
|
2014-07-01 14:09:14 +00:00
|
|
|
resultMap.insert("content", string);
|
|
|
|
|
|
|
|
std::vector<Ref<ResultPoint> > resultPoints = result->getResultPoints();
|
|
|
|
for (unsigned int i = 0; i < resultPoints.size(); i++) {
|
|
|
|
points.append(QPoint(resultPoints[i]->getX(), resultPoints[i]->getY()));
|
|
|
|
}
|
|
|
|
resultMap.insert("points", points);
|
2014-02-16 12:46:57 +00:00
|
|
|
}
|
|
|
|
catch(zxing::Exception& e)
|
|
|
|
{
|
2014-07-01 14:09:14 +00:00
|
|
|
qDebug() << "[decodeImage()] Exception:" << e.what();
|
2014-02-16 12:46:57 +00:00
|
|
|
emit decodingFinished(false);
|
2014-07-01 14:09:14 +00:00
|
|
|
resultMap.insert("content", QString(""));
|
|
|
|
resultMap.insert("points", QList<QVariant>());
|
2014-02-16 12:46:57 +00:00
|
|
|
}
|
2014-07-01 14:09:14 +00:00
|
|
|
return resultMap;
|
2014-02-16 12:46:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
QString QZXing::decodeImageFromFile(QString imageFilePath)
|
2014-02-16 12:46:57 +00:00
|
|
|
{
|
2014-07-01 14:09:14 +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.
|
|
|
|
return decodeImage(QImage(imageFilePath));
|
2014-02-16 12:46:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
QString QZXing::decodeImageQML(const QUrl &imageUrl)
|
2014-02-16 12:46:57 +00:00
|
|
|
{
|
2014-07-01 14:09:14 +00:00
|
|
|
return decodeSubImageQML(imageUrl);
|
2014-02-16 12:46:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
QString QZXing::decodeSubImageQML(const QUrl &imageUrl,
|
2014-02-16 12:46:57 +00:00
|
|
|
const double offsetX, const double offsetY,
|
|
|
|
const double width, const double height)
|
|
|
|
{
|
2014-07-01 14:09:14 +00:00
|
|
|
|
|
|
|
QString imagePath = imageUrl.path();
|
|
|
|
imagePath = imagePath.trimmed();
|
|
|
|
QFile file(imagePath);
|
|
|
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
qDebug() << "[decodeSubImageQML()] The file" << file.fileName() << "does not exist.";
|
2014-02-16 12:46:57 +00:00
|
|
|
emit decodingFinished(false);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
QImage img(imageUrl.path());
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
if(!(offsetX == 0 && offsetY == 0 && width == 0 && height == 0)) {
|
|
|
|
img = img.copy(offsetX, offsetY, width, height);
|
|
|
|
}
|
2014-02-16 12:46:57 +00:00
|
|
|
|
2014-07-01 14:09:14 +00:00
|
|
|
return decodeImage(img);
|
2014-02-16 12:46:57 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|