Skip to content

Commit f0a7886

Browse files
committed
code cleanup in QZXing.cpp to properly respect 'tryHarder' operation and avoid executing extra decoding operations unless instacted. ftylitak#151
1 parent 6e14a11 commit f0a7886

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

src/QZXing.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -417,43 +417,34 @@ QString QZXing::decodeImage(const QImage &image, int maxWidth, int maxHeight, bo
417417
lastDecodeOperationSucceded_ = true;
418418
} catch(zxing::Exception &/*e*/){}
419419

420-
if(!lastDecodeOperationSucceded_)
420+
if(!lastDecodeOperationSucceded_ && tryHarder_)
421421
{
422422
hints.setTryHarder(true);
423+
if(hints.containsFormat(BarcodeFormat::UPC_EAN_EXTENSION) &&
424+
!allowedExtensions_.empty() &&
425+
!(hints & DecodeHints::PRODUCT_HINT).isEmpty() )
426+
hints.setAllowedEanExtensions(std::set<int>());
423427

424428
try {
425429
res = decoder->decode(bb, hints);
426430
processingTime = t.elapsed();
427431
lastDecodeOperationSucceded_ = true;
428432
} catch(zxing::Exception &/*e*/) {}
433+
}
429434

430-
if (!lastDecodeOperationSucceded_ &&
431-
hints.containsFormat(BarcodeFormat::UPC_EAN_EXTENSION) &&
432-
!allowedExtensions_.empty() &&
433-
!(hints & DecodeHints::PRODUCT_HINT).isEmpty() ) {
434-
hints.setAllowedEanExtensions(std::set<int>());
435+
if (!lastDecodeOperationSucceded_&& tryHarder_ && bb->isRotateSupported()) {
436+
Ref<BinaryBitmap> bbTmp = bb;
437+
438+
for (int i=0; (i<3 && !lastDecodeOperationSucceded_); i++) {
439+
Ref<BinaryBitmap> rotatedImage(bbTmp->rotateCounterClockwise());
440+
bbTmp = rotatedImage;
435441

436442
try {
437-
res = decoder->decode(bb, hints);
443+
res = decoder->decode(rotatedImage, hints);
438444
processingTime = t.elapsed();
439445
lastDecodeOperationSucceded_ = true;
440446
} catch(zxing::Exception &/*e*/) {}
441447
}
442-
443-
if (tryHarder_ && bb->isRotateSupported()) {
444-
Ref<BinaryBitmap> bbTmp = bb;
445-
446-
for (int i=0; (i<3 && !lastDecodeOperationSucceded_); i++) {
447-
Ref<BinaryBitmap> rotatedImage(bbTmp->rotateCounterClockwise());
448-
bbTmp = rotatedImage;
449-
450-
try {
451-
res = decoder->decode(rotatedImage, hints);
452-
processingTime = t.elapsed();
453-
lastDecodeOperationSucceded_ = true;
454-
} catch(zxing::Exception &/*e*/) {}
455-
}
456-
}
457448
}
458449

459450
if (lastDecodeOperationSucceded_) {
@@ -604,11 +595,11 @@ QImage QZXing::encodeData(const QString &data, const QZXingEncoderConfig &encode
604595
data.toStdWString(),
605596
encoderConfig.errorCorrectionLevel == EncodeErrorCorrectionLevel_H ?
606597
qrcode::ErrorCorrectionLevel::H :
607-
(encoderConfig.errorCorrectionLevel == EncodeErrorCorrectionLevel_Q ?
608-
qrcode::ErrorCorrectionLevel::Q :
609-
(encoderConfig.errorCorrectionLevel == EncodeErrorCorrectionLevel_M ?
610-
qrcode::ErrorCorrectionLevel::M :
611-
qrcode::ErrorCorrectionLevel::L)));
598+
(encoderConfig.errorCorrectionLevel == EncodeErrorCorrectionLevel_Q ?
599+
qrcode::ErrorCorrectionLevel::Q :
600+
(encoderConfig.errorCorrectionLevel == EncodeErrorCorrectionLevel_M ?
601+
qrcode::ErrorCorrectionLevel::M :
602+
qrcode::ErrorCorrectionLevel::L)));
612603

613604
Ref<qrcode::ByteMatrix> bytesRef = barcode->getMatrix();
614605
const std::vector< std::vector <zxing::byte> >& bytes = bytesRef->getArray();

0 commit comments

Comments
 (0)