/* * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use crate::{common::BitMatrix, Exceptions, RXingResultPointCallback}; use super::AlignmentPattern; /** *
This class attempts to find alignment patterns in a QR Code. Alignment patterns look like finder * patterns but are smaller and appear at regular intervals throughout the image.
* *At the moment this only looks for the bottom-right alignment pattern.
* *This is mostly a simplified copy of {@link FinderPatternFinder}. It is copied, * pasted and stripped down here for maximum performance but does unfortunately duplicate * some code.
* *This class is thread-safe but not reentrant. Each thread must allocate its own object.
* * @author Sean Owen */ pub struct AlignmentPatternFinder { image: BitMatrix, possibleCenters: VecCreates a finder that will look in a portion of the whole image.
* * @param image image to search * @param startX left column from which to start searching * @param startY top row from which to start searching * @param width width of region to search * @param height height of region to search * @param moduleSize estimated module size so far */ pub fn new( image: BitMatrix, startX: u32, startY: u32, width: u32, height: u32, moduleSize: f32, resultPointCallback: OptionThis method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since * it's pretty performance-critical and so is written to be fast foremost.
* * @return {@link AlignmentPattern} if found * @throws NotFoundException if not found */ pub fn find(&mut self) -> ResultAfter a horizontal scan finds a potential alignment pattern, this method * "cross-checks" by scanning down vertically through the center of the possible * alignment pattern to see if the same proportion is detected.
* * @param startI row where an alignment pattern was detected * @param centerJ center of the section that appears to cross an alignment pattern * @param maxCount maximum reasonable number of modules that should be * observed in any reading state, based on the results of the horizontal scan * @return vertical center of alignment pattern, or {@link Float#NaN} if not found */ fn crossCheckVertical( &mut self, startI: u32, centerJ: u32, maxCount: u32, originalStateCountTotal: u32, ) -> f32 { let image = &self.image; let maxI = image.getHeight(); // let mut stateCount = &self.crossCheckStateCount; self.crossCheckStateCount[0] = 0; self.crossCheckStateCount[1] = 0; self.crossCheckStateCount[2] = 0; // Start counting up from center let mut i = startI; while i >= 0 && image.get(centerJ, i) && self.crossCheckStateCount[1] <= maxCount { self.crossCheckStateCount[1] += 1; i -= 1; } // If already too many modules in this state or ran off the edge: if i < 0 || self.crossCheckStateCount[1] > maxCount { return f32::NAN; } while i >= 0 && !image.get(centerJ, i) && self.crossCheckStateCount[0] <= maxCount { self.crossCheckStateCount[0] += 1; i -= 1; } if self.crossCheckStateCount[0] > maxCount { return f32::NAN; } // Now also count down from center i = startI + 1; while i < maxI && image.get(centerJ, i) && self.crossCheckStateCount[1] <= maxCount { self.crossCheckStateCount[1] += 1; i += 1; } if i == maxI || self.crossCheckStateCount[1] > maxCount { return f32::NAN; } while i < maxI && !image.get(centerJ, i) && self.crossCheckStateCount[2] <= maxCount { self.crossCheckStateCount[2] += 1; i += 1; } if self.crossCheckStateCount[2] > maxCount { return f32::NAN; } let stateCountTotal = self.crossCheckStateCount[0] + self.crossCheckStateCount[1] + self.crossCheckStateCount[2]; if 5 * (stateCountTotal as i64 - originalStateCountTotal as i64).abs() as u32 >= 2 * originalStateCountTotal { return f32::NAN; } if self.foundPatternCross(&self.crossCheckStateCount) { Self::centerFromEnd(&self.crossCheckStateCount, i) } else { f32::NAN } } /** *This is called when a horizontal scan finds a possible alignment pattern. It will * cross check with a vertical scan, and if successful, will see if this pattern had been * found on a previous horizontal scan. If so, we consider it confirmed and conclude we have * found the alignment pattern.
* * @param stateCount reading state module counts from horizontal scan * @param i row where alignment pattern may be found * @param j end of possible alignment pattern in row * @return {@link AlignmentPattern} if we have found the same pattern twice, or null if not */ fn handlePossibleCenter( &mut self, stateCount: &[u32], i: u32, j: u32, ) -> Option