2 * Copyright 2008 ZXing authors
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.BarcodeFormat;
20 import com.google.zxing.ReaderException;
21 import com.google.zxing.Result;
22 import com.google.zxing.ResultPoint;
23 import com.google.zxing.common.BitArray;
25 import java.util.Hashtable;
28 * <p>Encapsulates functionality and implementation that is common to UPC and EAN families
29 * of one-dimensional barcodes.</p>
31 * @author dswitkin@google.com (Daniel Switkin)
33 * @author alasdair@google.com (Alasdair Mackintosh)
35 public abstract class AbstractUPCEANReader extends AbstractOneDReader implements UPCEANReader {
37 private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.42f);
38 private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.7f);
41 * Start/end guard pattern.
43 static final int[] START_END_PATTERN = {1, 1, 1,};
46 * Pattern marking the middle of a UPC/EAN pattern, separating the two halves.
48 static final int[] MIDDLE_PATTERN = {1, 1, 1, 1, 1};
51 * "Odd", or "L" patterns used to encode UPC/EAN digits.
53 static final int[][] L_PATTERNS = {
67 * As above but also including the "even", or "G" patterns used to encode UPC/EAN digits.
69 static final int[][] L_AND_G_PATTERNS;
72 L_AND_G_PATTERNS = new int[20][];
73 for (int i = 0; i < 10; i++) {
74 L_AND_G_PATTERNS[i] = L_PATTERNS[i];
76 for (int i = 10; i < 20; i++) {
77 int[] widths = L_PATTERNS[i - 10];
78 int[] reversedWidths = new int[widths.length];
79 for (int j = 0; j < widths.length; j++) {
80 reversedWidths[j] = widths[widths.length - j - 1];
82 L_AND_G_PATTERNS[i] = reversedWidths;
86 private final StringBuffer decodeRowStringBuffer;
88 protected AbstractUPCEANReader() {
89 decodeRowStringBuffer = new StringBuffer(20);
92 static int[] findStartGuardPattern(BitArray row) throws ReaderException {
93 boolean foundStart = false;
94 int[] startRange = null;
97 startRange = findGuardPattern(row, nextStart, false, START_END_PATTERN);
98 int start = startRange[0];
99 nextStart = startRange[1];
100 // Make sure there is a quiet zone at least as big as the start pattern before the barcode.
101 // If this check would run off the left edge of the image, do not accept this barcode,
102 // as it is very likely to be a false positive.
103 int quietStart = start - (nextStart - start);
104 if (quietStart >= 0) {
105 foundStart = row.isRange(quietStart, start, false);
111 public final Result decodeRow(int rowNumber, BitArray row, Hashtable hints)
112 throws ReaderException {
113 return decodeRow(rowNumber, row, findStartGuardPattern(row));
116 public final Result decodeRow(int rowNumber, BitArray row, int[] startGuardRange)
117 throws ReaderException {
118 StringBuffer result = decodeRowStringBuffer;
120 int endStart = decodeMiddle(row, startGuardRange, result);
121 int[] endRange = decodeEnd(row, endStart);
123 // Make sure there is a quiet zone at least as big as the end pattern after the barcode. The
124 // spec might want more whitespace, but in practice this is the maximum we can count on.
125 int end = endRange[1];
126 int quietEnd = end + (end - endRange[0]);
127 if (quietEnd >= row.getSize() || !row.isRange(end, quietEnd, false)) {
128 throw ReaderException.getInstance();
131 String resultString = result.toString();
132 if (!checkChecksum(resultString)) {
133 throw ReaderException.getInstance();
136 float left = (float) (startGuardRange[1] + startGuardRange[0]) / 2.0f;
137 float right = (float) (endRange[1] + endRange[0]) / 2.0f;
138 return new Result(resultString,
139 null, // no natural byte representation for these barcodes
141 new ResultPoint(left, (float) rowNumber),
142 new ResultPoint(right, (float) rowNumber)},
146 abstract BarcodeFormat getBarcodeFormat();
149 * @return {@link #checkStandardUPCEANChecksum(String)}
151 boolean checkChecksum(String s) throws ReaderException {
152 return checkStandardUPCEANChecksum(s);
156 * Computes the UPC/EAN checksum on a string of digits, and reports
157 * whether the checksum is correct or not.
159 * @param s string of digits to check
160 * @return true iff string of digits passes the UPC/EAN checksum algorithm
161 * @throws ReaderException if the string does not contain only digits
163 public static boolean checkStandardUPCEANChecksum(String s) throws ReaderException {
164 int length = s.length();
170 for (int i = length - 2; i >= 0; i -= 2) {
171 int digit = (int) s.charAt(i) - (int) '0';
172 if (digit < 0 || digit > 9) {
173 throw ReaderException.getInstance();
178 for (int i = length - 1; i >= 0; i -= 2) {
179 int digit = (int) s.charAt(i) - (int) '0';
180 if (digit < 0 || digit > 9) {
181 throw ReaderException.getInstance();
185 return sum % 10 == 0;
189 * Subclasses override this to decode the portion of a barcode between the start
190 * and end guard patterns.
192 * @param row row of black/white values to search
193 * @param startRange start/end offset of start guard pattern
194 * @param resultString {@link StringBuffer} to append decoded chars to
195 * @return horizontal offset of first pixel after the "middle" that was decoded
196 * @throws ReaderException if decoding could not complete successfully
198 protected abstract int decodeMiddle(BitArray row, int[] startRange, StringBuffer resultString)
199 throws ReaderException;
201 int[] decodeEnd(BitArray row, int endStart) throws ReaderException {
202 return findGuardPattern(row, endStart, false, START_END_PATTERN);
206 * @param row row of black/white values to search
207 * @param rowOffset position to start search
208 * @param whiteFirst if true, indicates that the pattern specifies white/black/white/...
209 * pixel counts, otherwise, it is interpreted as black/white/black/...
210 * @param pattern pattern of counts of number of black and white pixels that are being
211 * searched for as a pattern
212 * @return start/end horizontal offset of guard pattern, as an array of two ints
213 * @throws ReaderException if pattern is not found
215 static int[] findGuardPattern(BitArray row, int rowOffset, boolean whiteFirst, int[] pattern)
216 throws ReaderException {
217 int patternLength = pattern.length;
218 int[] counters = new int[patternLength];
219 int width = row.getSize();
220 boolean isWhite = false;
221 while (rowOffset < width) {
222 isWhite = !row.get(rowOffset);
223 if (whiteFirst == isWhite) {
229 int counterPosition = 0;
230 int patternStart = rowOffset;
231 for (int x = rowOffset; x < width; x++) {
232 boolean pixel = row.get(x);
233 if (pixel ^ isWhite) {
234 counters[counterPosition]++;
236 if (counterPosition == patternLength - 1) {
237 if (patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
238 return new int[]{patternStart, x};
240 patternStart += counters[0] + counters[1];
241 for (int y = 2; y < patternLength; y++) {
242 counters[y - 2] = counters[y];
244 counters[patternLength - 2] = 0;
245 counters[patternLength - 1] = 0;
250 counters[counterPosition] = 1;
251 isWhite ^= true; // isWhite = !isWhite;
254 throw ReaderException.getInstance();
258 * Attempts to decode a single UPC/EAN-encoded digit.
260 * @param row row of black/white values to decode
261 * @param counters the counts of runs of observed black/white/black/... values
262 * @param rowOffset horizontal offset to start decoding from
263 * @param patterns the set of patterns to use to decode -- sometimes different encodings
264 * for the digits 0-9 are used, and this indicates the encodings for 0 to 9 that should
266 * @return horizontal offset of first pixel beyond the decoded digit
267 * @throws ReaderException if digit cannot be decoded
269 static int decodeDigit(BitArray row, int[] counters, int rowOffset, int[][] patterns)
270 throws ReaderException {
271 recordPattern(row, rowOffset, counters);
272 int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
274 int max = patterns.length;
275 for (int i = 0; i < max; i++) {
276 int[] pattern = patterns[i];
277 int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
278 if (variance < bestVariance) {
279 bestVariance = variance;
283 if (bestMatch >= 0) {
286 throw ReaderException.getInstance();