slight performance improvement, detector

This commit is contained in:
Henry Schimke
2022-12-31 17:01:10 -06:00
parent a111776032
commit a6727220fa
11 changed files with 31 additions and 30 deletions

View File

@@ -70,7 +70,7 @@ fn test_error_in_parameter_locator_not_compact() {
#[test] #[test]
fn test_aztec_rxing_result_sample() { fn test_aztec_rxing_result_sample() {
let matrix = BitMatrix::parse_strings(TEST_BARCODE, "X ", " ").expect("string parse success"); let matrix = BitMatrix::parse_strings(TEST_BARCODE, "X ", " ").expect("string parse success");
let r = Detector::new(matrix).detect(false); let r = Detector::new(&matrix).detect(false);
assert!(r.is_ok()); assert!(r.is_ok());
let r = r.expect("result already tested as ok"); let r = r.expect("result already tested as ok");
let res = decoder::decode(&r).expect("decode success"); let res = decoder::decode(&r).expect("decode success");
@@ -114,7 +114,7 @@ fn test_error_in_parameter_locator(data: &str) {
// dbg!(copy.to_string()); // dbg!(copy.to_string());
// dbg!(make_larger(&copy, 3).to_string()); // dbg!(make_larger(&copy, 3).to_string());
// The detector doesn't seem to work when matrix bits are only 1x1. So magnify. // The detector doesn't seem to work when matrix bits are only 1x1. So magnify.
let r = Detector::new(make_larger(&copy, 3)).detect(isMirror); let r = Detector::new(&make_larger(&copy, 3)).detect(isMirror);
assert!(r.is_ok()); assert!(r.is_ok());
let r = r.expect("result already tested as ok"); let r = r.expect("result already tested as ok");
assert_eq!(r.getNbLayers(), layers); assert_eq!(r.getNbLayers(), layers);
@@ -144,14 +144,14 @@ fn test_error_in_parameter_locator(data: &str) {
// ); // );
} }
// try { // try {
if let Err(res) = detector::Detector::new(make_larger(&copy, 3)).detect(false) { if let Err(res) = detector::Detector::new(&make_larger(&copy, 3)).detect(false) {
if let Exceptions::NotFoundException(_msg) = res { if let Exceptions::NotFoundException(_msg) = res {
// all ok // all ok
} else { } else {
panic!("Only Exceptions::NotFoundException allowed, got {}", res); panic!("Only Exceptions::NotFoundException allowed, got {}", res);
} }
} else { } else {
let r = Detector::new(make_larger(&copy, 3)).detect(false); let r = Detector::new(&make_larger(&copy, 3)).detect(false);
assert!(r.is_ok()); assert!(r.is_ok());
let r = r.expect("result already tested as ok"); let r = r.expect("result already tested as ok");
assert_eq!(r.getNbLayers(), layers); assert_eq!(r.getNbLayers(), layers);

View File

@@ -51,7 +51,7 @@ impl Reader for AztecReader {
) -> Result<RXingResult, Exceptions> { ) -> Result<RXingResult, Exceptions> {
// let notFoundException = None; // let notFoundException = None;
// let formatException = None; // let formatException = None;
let mut detector = Detector::new(image.getBlackMatrix().clone()); let mut detector = Detector::new(image.getBlackMatrix());
let points; let points;
let decoderRXingResult: DecoderRXingResult; let decoderRXingResult: DecoderRXingResult;
// try { // try {

View File

@@ -42,8 +42,8 @@ const EXPECTED_CORNER_BITS: [u32; 4] = [
* @author David Olivier * @author David Olivier
* @author Frank Yellin * @author Frank Yellin
*/ */
pub struct Detector { pub struct Detector<'a> {
image: BitMatrix, image: &'a BitMatrix,
compact: bool, compact: bool,
nb_layers: u32, nb_layers: u32,
@@ -52,9 +52,9 @@ pub struct Detector {
shift: u32, shift: u32,
} }
impl Detector { impl<'a> Detector<'_> {
pub fn new(image: BitMatrix) -> Self { pub fn new(image: &'a BitMatrix) -> Detector<'a> {
Self { Detector {
image, image,
compact: false, compact: false,
nb_layers: 0, nb_layers: 0,

View File

@@ -77,7 +77,7 @@ impl Reader for DataMatrixReader {
decoderRXingResult = DECODER.decode(&bits)?; decoderRXingResult = DECODER.decode(&bits)?;
points.clear(); points.clear();
} else { } else {
let detectorRXingResult = Detector::new(image.getBlackMatrix().clone())?.detect()?; let detectorRXingResult = Detector::new(image.getBlackMatrix())?.detect()?;
decoderRXingResult = DECODER.decode(detectorRXingResult.getBits())?; decoderRXingResult = DECODER.decode(detectorRXingResult.getBits())?;
points = detectorRXingResult.getPoints().clone(); points = detectorRXingResult.getPoints().clone();
} }

View File

@@ -27,14 +27,14 @@ use super::DatamatrixDetectorResult;
* *
* @author Sean Owen * @author Sean Owen
*/ */
pub struct Detector { pub struct Detector<'a> {
image: BitMatrix, image: &'a BitMatrix,
rectangleDetector: WhiteRectangleDetector, rectangleDetector: WhiteRectangleDetector,
} }
impl Detector { impl<'a> Detector<'_> {
pub fn new(image: BitMatrix) -> Result<Self, Exceptions> { pub fn new(image: &'a BitMatrix) -> Result<Detector<'a>, Exceptions> {
Ok(Self { Ok(Detector {
rectangleDetector: WhiteRectangleDetector::new_from_image(&image)?, rectangleDetector: WhiteRectangleDetector::new_from_image(image)?,
image, image,
}) })
} }

View File

@@ -29,10 +29,10 @@ use super::MultiFinderPatternFinder;
* @author Sean Owen * @author Sean Owen
* @author Hannes Erven * @author Hannes Erven
*/ */
pub struct MultiDetector(Detector); pub struct MultiDetector<'a>(Detector<'a>);
impl MultiDetector { impl<'a> MultiDetector<'_> {
pub fn new(image: BitMatrix) -> Self { pub fn new(image: &'a BitMatrix) -> MultiDetector<'a> {
Self(Detector::new(image)) MultiDetector(Detector::new(image))
} }
// private static final DetectorRXingResult[] EMPTY_DETECTOR_RESULTS = new DetectorRXingResult[0]; // private static final DetectorRXingResult[] EMPTY_DETECTOR_RESULTS = new DetectorRXingResult[0];

View File

@@ -50,7 +50,7 @@ impl MultipleBarcodeReader for QRCodeMultiReader {
) -> Result<Vec<crate::RXingResult>, crate::Exceptions> { ) -> Result<Vec<crate::RXingResult>, crate::Exceptions> {
let mut results = Vec::new(); let mut results = Vec::new();
let detectorRXingResults = let detectorRXingResults =
MultiDetector::new(image.getBlackMatrix().clone()).detectMulti(hints)?; MultiDetector::new(image.getBlackMatrix()).detectMulti(hints)?;
for detectorRXingResult in detectorRXingResults { for detectorRXingResult in detectorRXingResults {
let mut proc = || -> Result<(), Exceptions> { let mut proc = || -> Result<(), Exceptions> {
let decoderRXingResult = decoder::decoder::decode_bitmatrix_with_hints( let decoderRXingResult = decoder::decoder::decode_bitmatrix_with_hints(

View File

@@ -36,14 +36,14 @@ use super::{
* *
* @author Sean Owen * @author Sean Owen
*/ */
pub struct Detector { pub struct Detector<'a> {
image: BitMatrix, image: &'a BitMatrix,
resultPointCallback: Option<RXingResultPointCallback>, resultPointCallback: Option<RXingResultPointCallback>,
} }
impl Detector { impl<'a> Detector<'_> {
pub fn new(image: BitMatrix) -> Self { pub fn new(image: &'a BitMatrix) -> Detector<'a> {
Self { Detector {
image, image,
resultPointCallback: None, resultPointCallback: None,
} }

View File

@@ -32,7 +32,8 @@ fn test_encode_decode(value: &str) {
let byt_matrix = qr_code.getMatrix().as_ref().unwrap().clone(); let byt_matrix = qr_code.getMatrix().as_ref().unwrap().clone();
// dbg!(BitMatrix::from(byt_matrix.clone()).to_string()); // dbg!(BitMatrix::from(byt_matrix.clone()).to_string());
// let mut detector = Detector::new(make_larger(&byt_matrix.into(),5)); // let mut detector = Detector::new(make_larger(&byt_matrix.into(),5));
let mut detector = Detector::new(byt_matrix.into()); let new_matrix : &BitMatrix = &byt_matrix.into();
let mut detector = Detector::new(new_matrix);
let detected_points = detector.detect().expect("must detect"); let detected_points = detector.detect().expect("must detect");
let decoded = decoder::decode_bitmatrix(detected_points.getBits()).expect("must decode"); let decoded = decoder::decode_bitmatrix(detected_points.getBits()).expect("must decode");
assert_eq!(decoded.getText(), value); assert_eq!(decoded.getText(), value);

View File

@@ -15,4 +15,4 @@ pub use finder_pattern_info::*;
pub use qrcode_detector_result::*; pub use qrcode_detector_result::*;
#[cfg(test)] #[cfg(test)]
mod cetector_test; mod detector_test;

View File

@@ -67,7 +67,7 @@ impl Reader for QRCodeReader {
points = Vec::new(); points = Vec::new();
} else { } else {
let detectorRXingResult = let detectorRXingResult =
Detector::new(image.getBlackMatrix().clone()).detect_with_hints(&hints)?; Detector::new(image.getBlackMatrix()).detect_with_hints(&hints)?;
decoderRXingResult = decoderRXingResult =
decoder::decode_bitmatrix_with_hints(detectorRXingResult.getBits(), &hints)?; decoder::decode_bitmatrix_with_hints(detectorRXingResult.getBits(), &hints)?;
points = detectorRXingResult.getPoints().clone(); points = detectorRXingResult.getPoints().clone();