2 * Copyright 2008 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.oned;
19 import com.google.zxing.BlackPointEstimationMethod;
20 import com.google.zxing.DecodeHintType;
21 import com.google.zxing.MonochromeBitmapSource;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24 import com.google.zxing.common.BitArray;
26 import java.util.Hashtable;
29 * <p>Encapsulates functionality and implementation that is common to all families
30 * of one-dimensional barcodes.</p>
32 * @author dswitkin@google.com (Daniel Switkin)
33 * @author srowen@google.com (Sean Owen)
35 public abstract class AbstractOneDReader implements OneDReader {
37 public final Result decode(MonochromeBitmapSource image) throws ReaderException {
38 return decode(image, null);
41 public final Result decode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
43 boolean tryHarder = hints != null && hints.contains(DecodeHintType.TRY_HARDER);
45 int width = image.getWidth();
46 int height = image.getHeight();
48 BitArray row = new BitArray(width);
50 // We're going to examine rows from the middle outward, searching alternately above and below the middle,
51 // and farther out each time. rowStep is the number of rows between each successive attempt above and below
52 // the middle. So we'd scan row middle, then middle - rowStep, then middle + rowStep,
53 // then middle - 2*rowStep, etc.
54 // rowStep is bigger as the image is taller, but is always at least 1. We've somewhat arbitrarily decided
55 // that moving up and down by about 1/16 of the image is pretty good.
56 int middle = height >> 1;
57 int rowStep = Math.max(1, height >> 4);
58 int maxLines = tryHarder ? 15 : 7;
59 for (int x = 0; x < maxLines; x++) {
61 int rowStepsAboveOrBelow = (x + 1) >> 1;
62 boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
63 int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
64 if (rowNumber < 0 || rowNumber >= height) {
68 image.estimateBlackPoint(BlackPointEstimationMethod.ROW_SAMPLING, rowNumber);
69 image.getBlackRow(rowNumber, row, 0, width);
72 return decodeRow(rowNumber, row, hints);
73 } catch (ReaderException re) {
75 row.reverse(); // try scanning the row backwards
77 return decodeRow(rowNumber, row, hints);
78 } catch (ReaderException re2) {
86 throw new ReaderException("No barcode found");
89 protected static void recordPattern(BitArray row, int start, int[] counters) throws ReaderException {
90 int numCounters = counters.length;
91 for (int i = 0; i < numCounters; i++) {
94 int end = row.getSize();
96 throw new ReaderException("Couldn't fully read a pattern");
98 boolean isWhite = !row.get(start);
99 int counterPosition = 0;
102 boolean pixel = row.get(i);
103 if ((!pixel && isWhite) || (pixel && !isWhite)) {
104 counters[counterPosition]++;
107 if (counterPosition == numCounters) {
110 counters[counterPosition] = 1;
116 // If we read fully the last section of pixels and filled up our counters -- or filled
117 // the last counter but ran off the side of the image, OK. Otherwise, a problem.
118 if (!(counterPosition == numCounters || (counterPosition == numCounters - 1 && i == end))) {
119 throw new ReaderException("Couldn't fully read a pattern");
124 * Determines how closely a set of observed counts of runs of black/white values matches a given
125 * target pattern. This is reported as the ratio of the total variance from the expected pattern proportions
126 * across all pattern elements, to the length of the pattern.
128 * @param counters observed counters
129 * @param pattern expected pattern
130 * @return average variance between counters and pattern
132 protected static float patternMatchVariance(int[] counters, int[] pattern) {
134 int numCounters = counters.length;
135 int patternLength = 0;
136 for (int i = 0; i < numCounters; i++) {
137 total += counters[i];
138 patternLength += pattern[i];
140 float unitBarWidth = (float) total / (float) patternLength;
142 float totalVariance = 0.0f;
143 for (int x = 0; x < numCounters; x++) {
144 float scaledCounter = (float) counters[x] / unitBarWidth;
145 float width = pattern[x];
146 float abs = scaledCounter > width ? scaledCounter - width : width - scaledCounter;
147 totalVariance += abs;
149 return totalVariance / (float) patternLength;
155 * @return argument rounded to nearest int
157 protected static int round(float f) {
158 return (int) (f + 0.5f);