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>
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) {
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],
191 MAX_INDIVIDUAL_VARIANCE);
192 if (variance < bestVariance) {
193 bestVariance = variance;
194 bestMatch = startCode;
197 if (bestMatch >= 0) {
198 // Look for whitespace before start pattern, >= 50% of width of start pattern
199 if (row.isRange(Math.max(0, patternStart - (i - patternStart) / 2), patternStart,
201 return new int[]{patternStart, i, bestMatch};
204 patternStart += counters[0] + counters[1];
205 for (int y = 2; y < patternLength; y++) {
206 counters[y - 2] = counters[y];
208 counters[patternLength - 2] = 0;
209 counters[patternLength - 1] = 0;
214 counters[counterPosition] = 1;
215 isWhite ^= true; // isWhite = !isWhite;
218 throw ReaderException.getInstance();
221 private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException {
222 recordPattern(row, rowOffset, counters);
223 int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
225 for (int d = 0; d < CODE_PATTERNS.length; d++) {
226 int[] pattern = CODE_PATTERNS[d];
227 int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
228 if (variance < bestVariance) {
229 bestVariance = variance;
233 // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6.
234 if (bestMatch >= 0) {
237 throw ReaderException.getInstance();
241 public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
243 int[] startPatternInfo = findStartPattern(row);
244 int startCode = startPatternInfo[2];
248 codeSet = CODE_CODE_A;
251 codeSet = CODE_CODE_B;
254 codeSet = CODE_CODE_C;
257 throw ReaderException.getInstance();
260 boolean done = false;
261 boolean isNextShifted = false;
263 StringBuffer result = new StringBuffer();
264 int lastStart = startPatternInfo[0];
265 int nextStart = startPatternInfo[1];
266 int[] counters = new int[6];
270 int checksumTotal = startCode;
272 boolean lastCharacterWasPrintable = true;
276 boolean unshift = isNextShifted;
277 isNextShifted = false;
279 // Save off last code
282 // Decode another code from image
283 code = decodeCode(row, counters, nextStart);
285 // Remember whether the last code was printable or not (excluding CODE_STOP)
286 if (code != CODE_STOP) {
287 lastCharacterWasPrintable = true;
290 // Add to checksum computation (if not CODE_STOP of course)
291 if (code != CODE_STOP) {
293 checksumTotal += multiplier * code;
296 // Advance to where the next code will to start
297 lastStart = nextStart;
298 for (int i = 0; i < counters.length; i++) {
299 nextStart += counters[i];
302 // Take care of illegal start codes
307 throw ReaderException.getInstance();
314 result.append((char) (' ' + code));
315 } else if (code < 96) {
316 result.append((char) (code - 64));
318 // Don't let CODE_STOP, which always appears, affect whether whether we think the last
319 // code was printable or not.
320 if (code != CODE_STOP) {
321 lastCharacterWasPrintable = false;
331 isNextShifted = true;
332 codeSet = CODE_CODE_B;
335 codeSet = CODE_CODE_B;
338 codeSet = CODE_CODE_C;
348 result.append((char) (' ' + code));
350 if (code != CODE_STOP) {
351 lastCharacterWasPrintable = false;
361 isNextShifted = true;
362 codeSet = CODE_CODE_C;
365 codeSet = CODE_CODE_A;
368 codeSet = CODE_CODE_C;
383 if (code != CODE_STOP) {
384 lastCharacterWasPrintable = false;
391 codeSet = CODE_CODE_A;
394 codeSet = CODE_CODE_B;
404 // Unshift back to another code set if we were shifted
408 codeSet = CODE_CODE_C;
411 codeSet = CODE_CODE_A;
414 codeSet = CODE_CODE_B;
421 // Check for ample whitespice following pattern, but, to do this we first need to remember that
422 // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
423 // to read off. Would be slightly better to properly read. Here we just skip it:
424 while (row.get(nextStart)) {
427 if (!row.isRange(nextStart, Math.min(row.getSize(), nextStart + (nextStart - lastStart) / 2),
429 throw ReaderException.getInstance();
432 // Pull out from sum the value of the penultimate check code
433 checksumTotal -= multiplier * lastCode;
434 // lastCode is the checksum then:
435 if (checksumTotal % 103 != lastCode) {
436 throw ReaderException.getInstance();
439 // Need to pull out the check digits from string
440 int resultLength = result.length();
441 // Only bother if the result had at least one character, and if the checksum digit happened to
442 // be a printable character. If it was just interpreted as a control code, nothing to remove.
443 if (resultLength > 0 && lastCharacterWasPrintable) {
444 if (codeSet == CODE_CODE_C) {
445 result.delete(resultLength - 2, resultLength);
447 result.delete(resultLength - 1, resultLength);
451 String resultString = result.toString();
453 if (resultString.length() == 0) {
454 // Almost surely a false positive
455 throw ReaderException.getInstance();
458 float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
459 float right = (float) (nextStart + lastStart) / 2.0f;
464 new GenericResultPoint(left, (float) rowNumber),
465 new GenericResultPoint(right, (float) rowNumber)},
466 BarcodeFormat.CODE_128);