2 * Copyright 2007 Google Inc.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package com.google.zxing.qrcode.detector;
\r
19 import com.google.zxing.MonochromeBitmapSource;
\r
20 import com.google.zxing.ReaderException;
\r
21 import com.google.zxing.common.BitArray;
\r
23 import java.util.Vector;
\r
26 * <p>This class attempts to find alignment patterns in a QR Code. Alignment patterns look like finder
\r
27 * patterns but are smaller and appear at regular intervals throughout the image.</p>
\r
29 * <p>At the moment this only looks for the bottom-right alignment pattern.</p>
\r
31 * <p>This is mostly a simplified copy of {@link FinderPatternFinder}. It is copied,
\r
32 * pasted and stripped down here for maximum performance but does unfortunately duplicate
\r
35 * <p>This class is not thread-safe.</p>
\r
37 * @author srowen@google.com (Sean Owen)
\r
39 final class AlignmentPatternFinder {
\r
41 private final MonochromeBitmapSource image;
\r
42 private final Vector possibleCenters;
\r
43 private final int startX;
\r
44 private final int startY;
\r
45 private final int width;
\r
46 private final int height;
\r
47 private final float moduleSize;
\r
50 * <p>Creates a finder that will look in a portion of the whole image.</p>
\r
52 * @param image image to search
\r
53 * @param startX left column from which to start searching
\r
54 * @param startY top row from which to start searching
\r
55 * @param width width of region to search
\r
56 * @param height height of region to search
\r
57 * @param moduleSize estimated module size so far
\r
59 AlignmentPatternFinder(MonochromeBitmapSource image,
\r
66 this.possibleCenters = new Vector(5);
\r
67 this.startX = startX;
\r
68 this.startY = startY;
\r
70 this.height = height;
\r
71 this.moduleSize = moduleSize;
\r
75 * <p>This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since
\r
76 * it's pretty performance-critical and so is written to be fast foremost.</p>
\r
78 * @return {@link AlignmentPattern} if found
\r
79 * @throws ReaderException if not found
\r
81 AlignmentPattern find() throws ReaderException {
\r
82 int startX = this.startX;
\r
83 int height = this.height;
\r
84 int maxJ = startX + width;
\r
85 int middleI = startY + (height >> 1);
\r
86 BitArray luminanceRow = new BitArray(width);
\r
87 // We are looking for black/white/black modules in 1:1:1 ratio;
\r
88 // this tracks the number of black/white/black modules seen so far
\r
89 int[] stateCount = new int[3];
\r
90 for (int iGen = 0; iGen < height; iGen++) {
\r
91 // Search from middle outwards
\r
92 int i = middleI + ((iGen & 0x01) == 0 ? ((iGen + 1) >> 1) : -((iGen + 1) >> 1));
\r
93 image.getBlackRow(i, luminanceRow, startX, width);
\r
97 int currentState = 0;
\r
99 // Burn off leading white pixels before anything else; if we start in the middle of
\r
100 // a white run, it doesn't make sense to count its length, since we don't know if the
\r
101 // white run continued to the left of the start point
\r
102 while (!luminanceRow.get(j - startX) && j < maxJ) {
\r
106 if (luminanceRow.get(j - startX)) {
\r
108 if (currentState == 1) { // Counting black pixels
\r
109 stateCount[currentState]++;
\r
110 } else { // Counting white pixels
\r
111 if (currentState == 2) { // A winner?
\r
112 if (foundPatternCross(stateCount)) { // Yes
\r
113 AlignmentPattern confirmed = handlePossibleCenter(stateCount, i, j);
\r
114 if (confirmed != null) {
\r
118 stateCount[0] = stateCount[2];
\r
123 stateCount[++currentState]++;
\r
126 } else { // White pixel
\r
127 if (currentState == 1) { // Counting black pixels
\r
130 stateCount[currentState]++;
\r
134 if (foundPatternCross(stateCount)) {
\r
135 AlignmentPattern confirmed = handlePossibleCenter(stateCount, i, maxJ);
\r
136 if (confirmed != null) {
\r
143 // Hmm, nothing we saw was observed and confirmed twice. If we had
\r
144 // any guess at all, return it.
\r
145 if (!possibleCenters.isEmpty()) {
\r
146 return (AlignmentPattern) possibleCenters.elementAt(0);
\r
149 throw new ReaderException("Could not find alignment pattern");
\r
153 * Given a count of black/white/black pixels just seen and an end position,
\r
154 * figures the location of the center of this black/white/black run.
\r
156 private static float centerFromEnd(int[] stateCount, int end) {
\r
157 return (float) (end - stateCount[2]) - stateCount[1] / 2.0f;
\r
161 * @param stateCount count of black/white/black pixels just read
\r
162 * @return true iff the proportions of the counts is close enough to the 1/1/1 ratios
\r
163 * used by alignment patterns to be considered a match
\r
165 private boolean foundPatternCross(int[] stateCount) {
\r
166 float moduleSize = this.moduleSize;
\r
167 float maxVariance = moduleSize / 2.5f;
\r
168 for (int i = 0; i < 3; i++) {
\r
169 if (Math.abs(moduleSize - stateCount[i]) >= maxVariance) {
\r
177 * <p>After a horizontal scan finds a potential alignment pattern, this method
\r
178 * "cross-checks" by scanning down vertically through the center of the possible
\r
179 * alignment pattern to see if the same proportion is detected.</p>
\r
181 * @param startI row where an alignment pattern was detected
\r
182 * @param centerJ center of the section that appears to cross an alignment pattern
\r
183 * @param maxCount maximum reasonable number of modules that should be
\r
184 * observed in any reading state, based on the results of the horizontal scan
\r
185 * @return vertical center of alignment pattern, or {@link Float#NaN} if not found
\r
187 private float crossCheckVertical(int startI, int centerJ, int maxCount) {
\r
188 MonochromeBitmapSource image = this.image;
\r
190 int maxI = image.getHeight();
\r
191 int[] stateCount = new int[3];
\r
193 // Start counting up from center
\r
195 while (i >= 0 && image.isBlack(centerJ, i) && stateCount[1] <= maxCount) {
\r
199 // If already too many modules in this state or ran off the edge:
\r
200 if (i < 0 || stateCount[1] > maxCount) {
\r
203 while (i >= 0 && !image.isBlack(centerJ, i) && stateCount[0] <= maxCount) {
\r
207 if (stateCount[0] > maxCount) {
\r
211 // Now also count down from center
\r
213 while (i < maxI && image.isBlack(centerJ, i) && stateCount[1] <= maxCount) {
\r
217 if (i == maxI || stateCount[1] > maxCount) {
\r
220 while (i < maxI && !image.isBlack(centerJ, i) && stateCount[2] <= maxCount) {
\r
224 if (stateCount[2] > maxCount) {
\r
228 return foundPatternCross(stateCount) ? centerFromEnd(stateCount, i) : Float.NaN;
\r
232 * <p>This is called when a horizontal scan finds a possible alignment pattern. It will
\r
233 * cross check with a vertical scan, and if successful, will see if this pattern had been
\r
234 * found on a previous horizontal scan. If so, we consider it confirmed and conclude we have
\r
235 * found the alignment pattern.</p>
\r
237 * @param stateCount reading state module counts from horizontal scan
\r
238 * @param i row where alignment pattern may be found
\r
239 * @param j end of possible alignment pattern in row
\r
240 * @return {@link AlignmentPattern} if we have found the same pattern twice, or null if not
\r
242 private AlignmentPattern handlePossibleCenter(int[] stateCount, int i, int j) {
\r
243 float centerJ = centerFromEnd(stateCount, j);
\r
244 float centerI = crossCheckVertical(i, (int) centerJ, 2 * stateCount[1]);
\r
245 if (!Float.isNaN(centerI)) {
\r
246 float estimatedModuleSize = (float) (stateCount[0] + stateCount[1] + stateCount[2]) / 3.0f;
\r
247 int max = possibleCenters.size();
\r
248 for (int index = 0; index < max; index++) {
\r
249 AlignmentPattern center = (AlignmentPattern) possibleCenters.elementAt(index);
\r
250 // Look for about the same center and module size:
\r
251 if (center.aboutEquals(estimatedModuleSize, centerI, centerJ)) {
\r
252 return new AlignmentPattern(centerJ, centerI, estimatedModuleSize);
\r
255 // Hadn't found this before; save it
\r
256 possibleCenters.addElement(new AlignmentPattern(centerJ, centerI, estimatedModuleSize));
\r