For backward compatibility, after merging pull request #3, the original tagFoundAdvanced signal has been reimplemented. Also added try catch clause around the new singal since in a lot of cases it fails to detect the decoded barcode's rectangle. To be improved.

This commit is contained in:
favoritas37 2016-09-27 02:06:54 +03:00
parent a44cce230c
commit d5e5c895d8
2 changed files with 43 additions and 39 deletions

View File

@ -168,8 +168,9 @@ public slots:
signals:
void decodingStarted();
void decodingFinished(bool succeeded);
void tagFound(QString tag);
void enabledFormatsChanged();
void tagFound(QString tag);
void tagFoundAdvanced(const QString &tag, const QString &format, const QString &charSet) const;
void tagFoundAdvanced(const QString &tag, const QString &format, const QString &charSet, const QRectF &rect) const;
void error(QString msg);

View File

@ -197,61 +197,61 @@ void QZXing::setDecoder(const uint &hint)
QRectF getTagRect(const ArrayRef<Ref<ResultPoint> > &resultPoints, const Ref<BitMatrix> &bitMatrix)
{
if (resultPoints->size() < 2)
return QRectF();
return QRectF();
int matrixWidth = bitMatrix->getWidth();
int matrixHeight = bitMatrix->getHeight();
// 1D barcode
if (resultPoints->size() == 2) {
WhiteRectangleDetector detector(bitMatrix);
std::vector<Ref<ResultPoint> > resultRectPoints = detector.detect();
WhiteRectangleDetector detector(bitMatrix);
std::vector<Ref<ResultPoint> > resultRectPoints = detector.detect();
if (resultRectPoints.size() != 4)
return QRectF();
if (resultRectPoints.size() != 4)
return QRectF();
qreal xMin = resultPoints[0]->getX();
qreal xMin = resultPoints[0]->getX();
qreal xMax = xMin;
for (unsigned int i = 1; i < resultPoints->size(); ++i) {
for (unsigned int i = 1; i < resultPoints->size(); ++i) {
qreal x = resultPoints[i]->getX();
if (x < xMin)
xMin = x;
if (x > xMax)
xMax = x;
}
xMin = x;
if (x > xMax)
xMax = x;
}
qreal yMin = resultRectPoints[0]->getY();
qreal yMax = yMin;
for (unsigned int i = 1; i < resultRectPoints.size(); ++i) {
qreal yMin = resultRectPoints[0]->getY();
qreal yMax = yMin;
for (unsigned int i = 1; i < resultRectPoints.size(); ++i) {
qreal y = resultRectPoints[i]->getY();
if (y < yMin)
yMin = y;
if (y > yMax)
yMax = y;
}
yMin = y;
if (y > yMax)
yMax = y;
}
return QRectF(QPointF(xMin / matrixWidth, yMax / matrixHeight), QPointF(xMax / matrixWidth, yMin / matrixHeight));
return QRectF(QPointF(xMin / matrixWidth, yMax / matrixHeight), QPointF(xMax / matrixWidth, yMin / matrixHeight));
}
// 2D QR code
if (resultPoints->size() == 4) {
qreal xMin = resultPoints[0]->getX();
qreal xMin = resultPoints[0]->getX();
qreal xMax = xMin;
qreal yMin = resultPoints[0]->getY();
qreal yMax = yMin;
for (unsigned int i = 1; i < resultPoints->size(); ++i) {
qreal yMin = resultPoints[0]->getY();
qreal yMax = yMin;
for (unsigned int i = 1; i < resultPoints->size(); ++i) {
qreal x = resultPoints[i]->getX();
qreal y = resultPoints[i]->getY();
qreal y = resultPoints[i]->getY();
if (x < xMin)
xMin = x;
if (x > xMax)
xMax = x;
if (y < yMin)
yMin = y;
if (y > yMax)
yMax = y;
}
xMin = x;
if (x > xMax)
xMax = x;
if (y < yMin)
yMin = y;
if (y > yMax)
yMax = y;
}
return QRectF(QPointF(xMin / matrixWidth, yMax / matrixHeight), QPointF(xMax / matrixWidth, yMin / matrixHeight));
return QRectF(QPointF(xMin / matrixWidth, yMax / matrixHeight), QPointF(xMax / matrixWidth, yMin / matrixHeight));
}
return QRectF();
@ -310,6 +310,7 @@ QString QZXing::decodeImage(const QImage &image, int maxWidth, int maxHeight, bo
try {
res = decoder->decode(rotatedImage, hints);
processingTime = t.elapsed();
hasSucceded = true;
} catch(zxing::Exception &e) {}
}
@ -328,11 +329,13 @@ QString QZXing::decodeImage(const QImage &image, int maxWidth, int maxHeight, bo
string = codec->toUnicode(res->getText()->getText().c_str());
}
emit tagFound(string);
emit tagFoundAdvanced(string, foundedFmt, charSet_);
const QRectF rect = getTagRect(res->getResultPoints(), binz->getBlackMatrix());
emit tagFoundAdvanced(string, foundedFmt, charSet_, rect);
try {
const QRectF rect = getTagRect(res->getResultPoints(), binz->getBlackMatrix());
emit tagFoundAdvanced(string, foundedFmt, charSet_, rect);
}catch(zxing::Exception &/*e*/){}
}
processingTime = t.elapsed();
emit decodingFinished(true);
return string;
}