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. If
101 // this check would run off the left edge of the image, do not accept this barcode, as it is
102 // 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) throws ReaderException {
112 return decodeRow(rowNumber, row, findStartGuardPattern(row));
115 public final Result decodeRow(int rowNumber, BitArray row, int[] startGuardRange) throws ReaderException {
116 StringBuffer result = decodeRowStringBuffer;
118 int endStart = decodeMiddle(row, startGuardRange, result);
119 int[] endRange = decodeEnd(row, endStart);
121 // Make sure there is a quiet zone at least as big as the end pattern after the barcode. The
122 // spec might want more whitespace, but in practice this is the maximum we can count on.
123 int end = endRange[1];
124 int quietEnd = end + (end - endRange[0]);
125 if (quietEnd >= row.getSize() || !row.isRange(end, quietEnd, false)) {
126 throw ReaderException.getInstance();
129 String resultString = result.toString();
130 if (!checkChecksum(resultString)) {
131 throw ReaderException.getInstance();
134 float left = (float) (startGuardRange[1] + startGuardRange[0]) / 2.0f;
135 float right = (float) (endRange[1] + endRange[0]) / 2.0f;
136 return new Result(resultString,
137 null, // no natural byte representation for these barcodes
139 new ResultPoint(left, (float) rowNumber),
140 new ResultPoint(right, (float) rowNumber)},
144 abstract BarcodeFormat getBarcodeFormat();
147 * @return {@link #checkStandardUPCEANChecksum(String)}
149 boolean checkChecksum(String s) throws ReaderException {
150 return checkStandardUPCEANChecksum(s);
154 * Computes the UPC/EAN checksum on a string of digits, and reports
155 * whether the checksum is correct or not.
157 * @param s string of digits to check
158 * @return true iff string of digits passes the UPC/EAN checksum algorithm
159 * @throws ReaderException if the string does not contain only digits
161 public static boolean checkStandardUPCEANChecksum(String s) throws ReaderException {
162 int length = s.length();
168 for (int i = length - 2; i >= 0; i -= 2) {
169 int digit = (int) s.charAt(i) - (int) '0';
170 if (digit < 0 || digit > 9) {
171 throw ReaderException.getInstance();
176 for (int i = length - 1; i >= 0; i -= 2) {
177 int digit = (int) s.charAt(i) - (int) '0';
178 if (digit < 0 || digit > 9) {
179 throw ReaderException.getInstance();
183 return sum % 10 == 0;
187 * Subclasses override this to decode the portion of a barcode between the start and end guard patterns.
189 * @param row row of black/white values to search
190 * @param startRange start/end offset of start guard pattern
191 * @param resultString {@link StringBuffer} to append decoded chars to
192 * @return horizontal offset of first pixel after the "middle" that was decoded
193 * @throws ReaderException if decoding could not complete successfully
195 protected abstract int decodeMiddle(BitArray row, int[] startRange, StringBuffer resultString)
196 throws ReaderException;
198 int[] decodeEnd(BitArray row, int endStart) throws ReaderException {
199 return findGuardPattern(row, endStart, false, START_END_PATTERN);
203 * @param row row of black/white values to search
204 * @param rowOffset position to start search
205 * @param whiteFirst if true, indicates that the pattern specifies white/black/white/...
206 * pixel counts, otherwise, it is interpreted as black/white/black/...
207 * @param pattern pattern of counts of number of black and white pixels that are being
208 * searched for as a pattern
209 * @return start/end horizontal offset of guard pattern, as an array of two ints
210 * @throws ReaderException if pattern is not found
212 static int[] findGuardPattern(BitArray row, int rowOffset, boolean whiteFirst, int[] pattern)
213 throws ReaderException {
214 int patternLength = pattern.length;
215 int[] counters = new int[patternLength];
216 int width = row.getSize();
217 boolean isWhite = false;
218 while (rowOffset < width) {
219 isWhite = !row.get(rowOffset);
220 if (whiteFirst == isWhite) {
226 int counterPosition = 0;
227 int patternStart = rowOffset;
228 for (int x = rowOffset; x < width; x++) {
229 boolean pixel = row.get(x);
230 if (pixel ^ isWhite) {
231 counters[counterPosition]++;
233 if (counterPosition == patternLength - 1) {
234 if (patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
235 return new int[]{patternStart, x};
237 patternStart += counters[0] + counters[1];
238 for (int y = 2; y < patternLength; y++) {
239 counters[y - 2] = counters[y];
241 counters[patternLength - 2] = 0;
242 counters[patternLength - 1] = 0;
247 counters[counterPosition] = 1;
248 isWhite ^= true; // isWhite = !isWhite;
251 throw ReaderException.getInstance();
255 * Attempts to decode a single UPC/EAN-encoded digit.
257 * @param row row of black/white values to decode
258 * @param counters the counts of runs of observed black/white/black/... values
259 * @param rowOffset horizontal offset to start decoding from
260 * @param patterns the set of patterns to use to decode -- sometimes different encodings
261 * for the digits 0-9 are used, and this indicates the encodings for 0 to 9 that should
263 * @return horizontal offset of first pixel beyond the decoded digit
264 * @throws ReaderException if digit cannot be decoded
266 static int decodeDigit(BitArray row, int[] counters, int rowOffset, int[][] patterns)
267 throws ReaderException {
268 recordPattern(row, rowOffset, counters);
269 int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
271 int max = patterns.length;
272 for (int i = 0; i < max; i++) {
273 int[] pattern = patterns[i];
274 int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
275 if (variance < bestVariance) {
276 bestVariance = variance;
280 if (bestMatch >= 0) {
283 throw ReaderException.getInstance();