Commit e3ad226a authored by Volker Krause's avatar Volker Krause
Browse files

Add the option to ignore barcode aspect ratio heuristics

This allows to run barcode decoding on images that contain a barcode only
as a much smaller element. This is slower of course, but in some cases is
the only way we can get to the barcode.
parent da3368f4
......@@ -90,18 +90,23 @@ void BarcodeDecoder::clearCache()
m_cache.clear();
}
bool BarcodeDecoder::isPlausibleSize(int width, int height)
bool BarcodeDecoder::isPlausibleSize(int width, int height, BarcodeDecoder::BarcodeTypes hint)
{
// normalize to landscape
if (height > width) {
std::swap(width, height);
}
return width > MinSourceImageWidth && height > MinSourceImageHeight && width < MaxSourceImageWidth && height < MaxSourceImageHeight;
return width > MinSourceImageWidth && height > MinSourceImageHeight
&& ((width < MaxSourceImageWidth && height < MaxSourceImageHeight) || (hint & IgnoreAspectRatio));
}
bool BarcodeDecoder::isPlausibleAspectRatio(int width, int height, BarcodeDecoder::BarcodeTypes hint)
{
if (hint & IgnoreAspectRatio) {
return true;
}
// normalize to landscape
if (height > width) {
std::swap(width, height);
......@@ -129,7 +134,7 @@ bool BarcodeDecoder::isPlausibleAspectRatio(int width, int height, BarcodeDecode
bool BarcodeDecoder::maybeBarcode(int width, int height, BarcodeDecoder::BarcodeTypes hint)
{
return isPlausibleSize(width, height) && isPlausibleAspectRatio(width, height, hint);
return isPlausibleSize(width, height, hint) && isPlausibleAspectRatio(width, height, hint);
}
#ifdef HAVE_ZXING
......@@ -272,6 +277,11 @@ void BarcodeDecoder::decodeIfNeeded(const QImage &img, BarcodeDecoder::BarcodeTy
return;
}
if (hint & IgnoreAspectRatio) {
decodeZxing(img, hint, result);
return;
}
const auto aspectRatio = img.width() < img.height() ?
(float)img.height() / (float)img.width() :
(float)img.width() / (float)img.height();
......
......@@ -41,6 +41,7 @@ public:
Code39 = 16,
Code93 = 32,
Code128 = 64,
IgnoreAspectRatio = 128, /// search for barcodes anywhere in the image, rather than assuming the image is primarily containing the barcode
AnySquare = Aztec | QRCode | DataMatrix,
Any2D = AnySquare | PDF417,
Any1D = Code39 | Code93 | Code128,
......@@ -66,7 +67,7 @@ public:
* to perform them manually if a cheaper way to obtain the image dimension exists
* that does not require a full QImage creation.
*/
static bool isPlausibleSize(int width, int height);
static bool isPlausibleSize(int width, int height, BarcodeTypes hint);
/** Checks if the given image dimensions are a barcode of type @p hint.
* See above.
......
......@@ -22,7 +22,7 @@ bool PdfBarcodeUtil::maybeBarcode(const PdfImage &img, BarcodeDecoder::BarcodeTy
const auto w = img.width();
const auto h = img.height();
if (!BarcodeDecoder::isPlausibleSize(img.sourceWidth(), img.sourceHeight()) || !BarcodeDecoder::isPlausibleAspectRatio(w, h, hint)) {
if (!BarcodeDecoder::isPlausibleSize(img.sourceWidth(), img.sourceHeight(), hint) || !BarcodeDecoder::isPlausibleAspectRatio(w, h, hint)) {
return false;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment