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;
24 import com.google.zxing.common.GenericResultPoint;
26 import java.util.Hashtable;
29 * <p>Decodes Code 128 barcodes.</p>
31 * @author srowen@google.com (Sean Owen)
33 public final class Code128Reader extends AbstractOneDReader {
35 private static final int[][] CODE_PATTERNS = {
36 {2, 1, 2, 2, 2, 2}, // 0
41 {1, 3, 1, 2, 2, 2}, // 5
46 {2, 2, 1, 3, 1, 2}, // 10
51 {1, 1, 3, 2, 2, 2}, // 15
56 {2, 2, 1, 2, 3, 1}, // 20
61 {3, 2, 1, 1, 2, 2}, // 25
66 {2, 1, 2, 1, 2, 3}, // 30
71 {1, 3, 1, 3, 2, 1}, // 35
76 {2, 3, 1, 1, 1, 3}, // 40
81 {1, 1, 3, 1, 2, 3}, // 45
86 {2, 3, 1, 1, 3, 1}, // 50
91 {3, 1, 1, 3, 2, 1}, // 55
96 {3, 1, 4, 1, 1, 1}, // 60
101 {1, 2, 1, 1, 2, 4}, // 65
106 {1, 1, 2, 4, 1, 2}, // 70
111 {2, 4, 1, 2, 1, 1}, // 75
116 {1, 1, 1, 2, 4, 2}, // 80
121 {1, 2, 4, 2, 1, 1}, // 85
126 {2, 1, 4, 1, 2, 1}, // 90
131 {1, 1, 4, 1, 1, 3}, // 95
136 {1, 1, 4, 1, 3, 1}, // 100
141 {2, 1, 1, 2, 3, 2}, // 105
142 {2, 3, 3, 1, 1, 1, 2}
145 private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.25f);
146 private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.7f);
148 private static final int CODE_SHIFT = 98;
150 private static final int CODE_CODE_C = 99;
151 private static final int CODE_CODE_B = 100;
152 private static final int CODE_CODE_A = 101;
154 private static final int CODE_FNC_1 = 102;
155 private static final int CODE_FNC_2 = 97;
156 private static final int CODE_FNC_3 = 96;
157 private static final int CODE_FNC_4_A = 101;
158 private static final int CODE_FNC_4_B = 100;
160 private static final int CODE_START_A = 103;
161 private static final int CODE_START_B = 104;
162 private static final int CODE_START_C = 105;
163 private static final int CODE_STOP = 106;
165 private static int[] findStartPattern(BitArray row) throws ReaderException {
166 int width = row.getSize();
168 while (rowOffset < width) {
169 if (row.get(rowOffset)) {
175 int counterPosition = 0;
176 int[] counters = new int[6];
177 int patternStart = rowOffset;
178 boolean isWhite = false;
179 int patternLength = counters.length;
181 for (int i = rowOffset; i < width; i++) {
182 boolean pixel = row.get(i);
183 if ((!pixel && isWhite) || (pixel && !isWhite)) {
184 counters[counterPosition]++;
186 if (counterPosition == patternLength - 1) {
187 int bestVariance = MAX_AVG_VARIANCE;
189 for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
190 int variance = patternMatchVariance(counters, CODE_PATTERNS[startCode], MAX_INDIVIDUAL_VARIANCE);
191 if (variance < bestVariance) {
192 bestVariance = variance;
193 bestMatch = startCode;
196 if (bestMatch >= 0) {
197 // Look for whitespace before start pattern, >= 50% of width of start pattern
198 if (row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
199 return new int[]{patternStart, i, bestMatch};
202 patternStart += counters[0] + counters[1];
203 for (int y = 2; y < patternLength; y++) {
204 counters[y - 2] = counters[y];
206 counters[patternLength - 2] = 0;
207 counters[patternLength - 1] = 0;
212 counters[counterPosition] = 1;
216 throw new ReaderException("Can't find pattern");
219 private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException {
220 recordPattern(row, rowOffset, counters);
221 int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
223 for (int d = 0; d < CODE_PATTERNS.length; d++) {
224 int[] pattern = CODE_PATTERNS[d];
225 int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
226 if (variance < bestVariance) {
227 bestVariance = variance;
231 // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6
232 if (bestMatch >= 0) {
235 throw new ReaderException("Could not match any code pattern");
239 public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
241 int[] startPatternInfo = findStartPattern(row);
242 int startCode = startPatternInfo[2];
246 codeSet = CODE_CODE_A;
249 codeSet = CODE_CODE_B;
252 codeSet = CODE_CODE_C;
255 throw new ReaderException("Illegal start code");
258 boolean done = false;
259 boolean isNextShifted = false;
261 StringBuffer result = new StringBuffer();
262 int lastStart = startPatternInfo[0];
263 int nextStart = startPatternInfo[1];
264 int[] counters = new int[6];
268 int checksumTotal = startCode;
270 boolean lastCharacterWasPrintable = true;
274 boolean unshift = isNextShifted;
275 isNextShifted = false;
277 // Save off last code
280 // Decode another code from image
281 code = decodeCode(row, counters, nextStart);
283 // Remember whether the last code was printable or not (excluding CODE_STOP)
284 if (code != CODE_STOP) {
285 lastCharacterWasPrintable = true;
288 // Add to checksum computation (if not CODE_STOP of course)
289 if (code != CODE_STOP) {
291 checksumTotal += multiplier * code;
294 // Advance to where the next code will to start
295 lastStart = nextStart;
296 for (int i = 0; i < counters.length; i++) {
297 nextStart += counters[i];
300 // Take care of illegal start codes
305 throw new ReaderException("Unexpected start code");
312 result.append((char) (' ' + code));
313 } else if (code < 96) {
314 result.append((char) (code - 64));
316 // Don't let CODE_STOP, which always appears, affect whether whether we think the last code
317 // was printable or not
318 if (code != CODE_STOP) {
319 lastCharacterWasPrintable = false;
329 isNextShifted = true;
330 codeSet = CODE_CODE_B;
333 codeSet = CODE_CODE_B;
336 codeSet = CODE_CODE_C;
346 result.append((char) (' ' + code));
348 if (code != CODE_STOP) {
349 lastCharacterWasPrintable = false;
359 isNextShifted = true;
360 codeSet = CODE_CODE_C;
363 codeSet = CODE_CODE_A;
366 codeSet = CODE_CODE_C;
381 if (code != CODE_STOP) {
382 lastCharacterWasPrintable = false;
389 codeSet = CODE_CODE_A;
392 codeSet = CODE_CODE_B;
402 // Unshift back to another code set if we were shifted
406 codeSet = CODE_CODE_C;
409 codeSet = CODE_CODE_A;
412 codeSet = CODE_CODE_B;
419 // Check for ample whitespice following pattern, but, to do this we first need to remember that we
420 // fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left to read off.
421 // Would be slightly better to properly read. Here we just skip it:
422 while (row.get(nextStart)) {
425 if (!row.isRange(nextStart, Math.min(row.getSize(), nextStart + (nextStart - lastStart) / 2), false)) {
426 throw new ReaderException("Pattern not followed by whitespace");
429 // Pull out from sum the value of the penultimate check code
430 checksumTotal -= multiplier * lastCode;
431 // lastCode is the checksum then:
432 if (checksumTotal % 103 != lastCode) {
433 throw new ReaderException("Checksum failed");
436 // Need to pull out the check digits from string
437 int resultLength = result.length();
438 // Only bother if, well, the result had at least one character, and if the checksum digit happened
439 // to be a printable character. If it was just interpreted as a control code, nothing to remove
440 if (resultLength > 0 && lastCharacterWasPrintable) {
441 if (codeSet == CODE_CODE_C) {
442 result.delete(resultLength - 2, resultLength);
444 result.delete(resultLength - 1, resultLength);
448 String resultString = result.toString();
450 if (resultString.length() == 0) {
451 // Almost surely a false positive
452 throw new ReaderException("Empty barcode found; assuming a false positive");
455 float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
456 float right = (float) (nextStart + lastStart) / 2.0f;
461 new GenericResultPoint(left, (float) rowNumber),
462 new GenericResultPoint(right, (float) rowNumber)},
463 BarcodeFormat.CODE_128);