Skip to content

Commit 9e8606b

Browse files
committed
maxicode detector should return found rotation
1 parent e2b7981 commit 9e8606b

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

src/maxicode/detector.rs

+36-16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ const ROW_SCAN_SKIP: u32 = 2;
1616
pub struct MaxicodeDetectionResult {
1717
bits: BitMatrix,
1818
points: Vec<RXingResultPoint>,
19+
rotation: f32,
20+
}
21+
22+
impl MaxicodeDetectionResult {
23+
pub fn rotation(&self) -> f32 {
24+
self.rotation
25+
}
1926
}
2027

2128
impl DetectorRXingResult for MaxicodeDetectionResult {
@@ -339,7 +346,7 @@ pub fn detect(image: &BitMatrix, try_harder: bool) -> Result<MaxicodeDetectionRe
339346
};
340347
let grid_sampler = DefaultGridSampler::default();
341348

342-
let [tl, bl, tr, br] = &symbol_box;
349+
let [tl, bl, tr, br] = &symbol_box.0;
343350

344351
let target_width = MathUtils::distance_float(tl.0, tl.1, tr.0, tr.1);
345352
let target_height = MathUtils::distance_float(br.0, br.1, tr.0, tr.1);
@@ -377,9 +384,11 @@ pub fn detect(image: &BitMatrix, try_harder: bool) -> Result<MaxicodeDetectionRe
377384
return Ok(MaxicodeDetectionResult {
378385
bits,
379386
points: symbol_box
387+
.0
380388
.iter()
381389
.map(|p| RXingResultPoint { x: p.0, y: p.1 })
382390
.collect(),
391+
rotation: symbol_box.1,
383392
});
384393
}
385394

@@ -702,7 +711,10 @@ const LEFT_SHIFT_PERCENT_ADJUST: f32 = 0.03;
702711
const RIGHT_SHIFT_PERCENT_ADJUST: f32 = 0.03;
703712
const ACCEPTED_SCALES: [f64; 5] = [0.065, 0.069, 0.07, 0.075, 0.08];
704713

705-
fn box_symbol(image: &BitMatrix, circle: &mut Circle) -> Result<[(f32, f32); 4], Exceptions> {
714+
fn box_symbol(
715+
image: &BitMatrix,
716+
circle: &mut Circle,
717+
) -> Result<([(f32, f32); 4], f32), Exceptions> {
706718
let (left_boundary, right_boundary, top_boundary, bottom_boundary) =
707719
calculate_simple_boundary(circle, Some(image), None, false);
708720

@@ -725,19 +737,24 @@ fn box_symbol(image: &BitMatrix, circle: &mut Circle) -> Result<[(f32, f32); 4],
725737
return Err(Exceptions::NotFoundException(None));
726738
}
727739

740+
let mut final_rotation = 0.0;
741+
728742
for scale in ACCEPTED_SCALES {
729743
if let Some(found_rotation) = attempt_rotation_box(image, circle, &naive_box, scale) {
730-
result_box = found_rotation;
744+
(result_box, final_rotation) = found_rotation;
731745
break;
732746
}
733747
}
734748

735-
Ok([
736-
(result_box[0].x, result_box[0].y),
737-
(result_box[1].x, result_box[1].y),
738-
(result_box[2].x, result_box[2].y),
739-
(result_box[3].x, result_box[3].y),
740-
])
749+
Ok((
750+
[
751+
(result_box[0].x, result_box[0].y),
752+
(result_box[1].x, result_box[1].y),
753+
(result_box[2].x, result_box[2].y),
754+
(result_box[3].x, result_box[3].y),
755+
],
756+
final_rotation,
757+
))
741758
}
742759

743760
fn calculate_simple_boundary(
@@ -794,7 +811,7 @@ fn attempt_rotation_box(
794811
circle: &mut Circle,
795812
naive_box: &[RXingResultPoint; 4],
796813
center_scale: f64,
797-
) -> Option<[RXingResultPoint; 4]> {
814+
) -> Option<([RXingResultPoint; 4], f32)> {
798815
// update our circle with a more accurate center point
799816
circle.calculate_high_accuracy_center();
800817

@@ -938,12 +955,15 @@ fn attempt_rotation_box(
938955
final_rotation,
939956
);
940957

941-
Some([
942-
RXingResultPoint::new(new_1.0, new_1.1),
943-
RXingResultPoint::new(new_2.0, new_2.1),
944-
RXingResultPoint::new(new_3.0, new_3.1),
945-
RXingResultPoint::new(new_4.0, new_4.1),
946-
])
958+
Some((
959+
[
960+
RXingResultPoint::new(new_1.0, new_1.1),
961+
RXingResultPoint::new(new_2.0, new_2.1),
962+
RXingResultPoint::new(new_3.0, new_3.1),
963+
RXingResultPoint::new(new_4.0, new_4.1),
964+
],
965+
final_rotation,
966+
))
947967
} else {
948968
// panic!("couldn't find");
949969
None

src/maxicode/maxi_code_reader.rs

+13
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ impl Reader for MaxiCodeReader {
6868
Some(DecodeHintValue::TryHarder(true))
6969
);
7070

71+
let mut rotation = None;
72+
7173
let decoderRXingResult = if try_harder {
7274
let result = detector::detect(image.getBlackMatrixMut(), try_harder)?;
75+
rotation = Some(result.rotation());
7376
let parsed_result = detector::read_bits(result.getBits())?;
7477
maxicode_decoder::decode_with_hints(&parsed_result, hints)?
7578
} else {
@@ -93,6 +96,16 @@ impl Reader for MaxiCodeReader {
9396
crate::RXingResultMetadataValue::ErrorCorrectionLevel(ecLevel.to_owned()),
9497
);
9598
}
99+
100+
if let Some(rot) = rotation {
101+
if rot > 0.0 {
102+
result.putMetadata(
103+
RXingResultMetadataType::ORIENTATION,
104+
crate::RXingResultMetadataValue::Orientation(rot as i32),
105+
)
106+
}
107+
}
108+
96109
Ok(result)
97110
}
98111

0 commit comments

Comments
 (0)