Skip to content

Commit

Permalink
fix: resolve an issue with multiple barcodes #48
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimke committed Jan 30, 2024
1 parent 12eedd1 commit 2a32bb2
Showing 1 changed file with 53 additions and 28 deletions.
81 changes: 53 additions & 28 deletions src/multi/generic_multiple_barcode_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
use std::collections::HashMap;

use crate::{
common::Result, point_f, Binarizer, BinaryBitmap, DecodingHintDictionary, Exceptions, Point,
RXingResult, Reader,
common::{cpp_essentials::QuadrilateralIsPlausibleSquare, Quadrilateral, Result},
point_f, Binarizer, BinaryBitmap, DecodingHintDictionary, Exceptions, Point, RXingResult,
Reader,
};

use super::MultipleBarcodeReader;
Expand Down Expand Up @@ -55,15 +56,16 @@ impl<T: Reader> MultipleBarcodeReader for GenericMultipleBarcodeReader<T> {
) -> Result<Vec<RXingResult>> {
let mut results = Vec::new();
self.do_decode_multiple(image, hints, &mut results, 0, 0, 0);

if results.is_empty() {
return Err(Exceptions::NOT_FOUND);
}
Ok(results)
}
}
impl<T: Reader> GenericMultipleBarcodeReader<T> {
const MIN_DIMENSION_TO_RECUR: f32 = 100.0;
const MAX_DEPTH: u32 = 4;
const MIN_DIMENSION_TO_RECUR: f32 = 2.0;
const MAX_DEPTH: u32 = 8;

pub fn new(delegate: T) -> Self {
Self(delegate)
Expand All @@ -87,18 +89,51 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {
return;
};

let mut alreadyFound = false;
for existingRXingResult in results.iter() {
if existingRXingResult.getText() == result.getText() {
alreadyFound = true;
break;
}
}
// let alreadyFound = results.iter().any(|r| r.getText() == result.getText() && r.getBarcodeFormat() == result.getBarcodeFormat());

let resultPoints = result.getPoints().clone();

if !alreadyFound {
results.push(Self::translatePoints(result, xOffset, yOffset));
let possible_new_result = Self::translatePoints(result, xOffset, yOffset);

let already_found = if possible_new_result.getPoints().len() >= 4 {
let q1 = Quadrilateral::new(
possible_new_result.getPoints()[0],
possible_new_result.getPoints()[1],
possible_new_result.getPoints()[2],
possible_new_result.getPoints()[3],
);
results.iter().any(|e| {
if e.getPoints().len() >= 4 {
let q2 = Quadrilateral::new(
e.getPoints()[0],
e.getPoints()[1],
e.getPoints()[2],
e.getPoints()[3],
);
Quadrilateral::have_intersecting_bounding_boxes(&q1, &q2)
} else {
e.getPoints().iter().any(|p| q1.is_inside(*p))
}
})
} else {
results.iter().any(|e| {
if e.getPoints().len() >= 4 {
let q2 = Quadrilateral::new(
e.getPoints()[0],
e.getPoints()[1],
e.getPoints()[2],
e.getPoints()[3],
);
e.getPoints().iter().any(|p| q2.is_inside(*p))
} else {
e.getText() == possible_new_result.getText()
&& e.getBarcodeFormat() == possible_new_result.getBarcodeFormat()
}
})
};

if !already_found {
results.push(possible_new_result);
}

if resultPoints.is_empty() {
Expand All @@ -112,23 +147,13 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {
let mut maxX: f32 = 0.0;
let mut maxY: f32 = 0.0;
for point in &resultPoints {
// if (point == null) {
// continue;
// }
let x = point.x;
let y = point.y;
if x < minX {
minX = x;
}
if y < minY {
minY = y;
}
if x > maxX {
maxX = x;
}
if y > maxY {
maxY = y;
}

minX = f32::min(x, minX);
minY = f32::min(y, minY);
maxX = f32::max(x, maxX);
maxY = f32::max(y, maxY);
}

// Decode left of barcode
Expand Down

0 comments on commit 2a32bb2

Please sign in to comment.