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>Decodes Code 128 barcodes.</p>
32 public final class Code128Reader extends OneDReader {
34 private static final int[][] CODE_PATTERNS = {
35 {2, 1, 2, 2, 2, 2}, // 0
40 {1, 3, 1, 2, 2, 2}, // 5
45 {2, 2, 1, 3, 1, 2}, // 10
50 {1, 1, 3, 2, 2, 2}, // 15
55 {2, 2, 1, 2, 3, 1}, // 20
60 {3, 2, 1, 1, 2, 2}, // 25
65 {2, 1, 2, 1, 2, 3}, // 30
70 {1, 3, 1, 3, 2, 1}, // 35
75 {2, 3, 1, 1, 1, 3}, // 40
80 {1, 1, 3, 1, 2, 3}, // 45
85 {2, 3, 1, 1, 3, 1}, // 50
90 {3, 1, 1, 3, 2, 1}, // 55
95 {3, 1, 4, 1, 1, 1}, // 60
100 {1, 2, 1, 1, 2, 4}, // 65
105 {1, 1, 2, 4, 1, 2}, // 70
110 {2, 4, 1, 2, 1, 1}, // 75
115 {1, 1, 1, 2, 4, 2}, // 80
120 {1, 2, 4, 2, 1, 1}, // 85
125 {2, 1, 4, 1, 2, 1}, // 90
130 {1, 1, 4, 1, 1, 3}, // 95
135 {1, 1, 4, 1, 3, 1}, // 100
140 {2, 1, 1, 2, 3, 2}, // 105
141 {2, 3, 3, 1, 1, 1, 2}
144 private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.25f);
145 private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.7f);
147 private static final int CODE_SHIFT = 98;
149 private static final int CODE_CODE_C = 99;
150 private static final int CODE_CODE_B = 100;
151 private static final int CODE_CODE_A = 101;
153 private static final int CODE_FNC_1 = 102;
154 private static final int CODE_FNC_2 = 97;
155 private static final int CODE_FNC_3 = 96;
156 private static final int CODE_FNC_4_A = 101;
157 private static final int CODE_FNC_4_B = 100;
159 private static final int CODE_START_A = 103;
160 private static final int CODE_START_B = 104;
161 private static final int CODE_START_C = 105;
162 private static final int CODE_STOP = 106;
164 private static int[] findStartPattern(BitArray row) throws ReaderException {
165 int width = row.getSize();
167 while (rowOffset < width) {
168 if (row.get(rowOffset)) {
174 int counterPosition = 0;
175 int[] counters = new int[6];
176 int patternStart = rowOffset;
177 boolean isWhite = false;
178 int patternLength = counters.length;
180 for (int i = rowOffset; i < width; i++) {
181 boolean pixel = row.get(i);
182 if (pixel ^ isWhite) {
183 counters[counterPosition]++;
185 if (counterPosition == patternLength - 1) {
186 int bestVariance = MAX_AVG_VARIANCE;
188 for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
189 int variance = patternMatchVariance(counters, CODE_PATTERNS[startCode],
190 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,
200 return new int[]{patternStart, i, bestMatch};
203 patternStart += counters[0] + counters[1];
204 for (int y = 2; y < patternLength; y++) {
205 counters[y - 2] = counters[y];
207 counters[patternLength - 2] = 0;
208 counters[patternLength - 1] = 0;
213 counters[counterPosition] = 1;
214 isWhite ^= true; // isWhite = !isWhite;
217 throw ReaderException.getInstance();
220 private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException {
221 recordPattern(row, rowOffset, counters);
222 int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
224 for (int d = 0; d < CODE_PATTERNS.length; d++) {
225 int[] pattern = CODE_PATTERNS[d];
226 int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
227 if (variance < bestVariance) {
228 bestVariance = variance;
232 // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6.
233 if (bestMatch >= 0) {
236 throw ReaderException.getInstance();
240 public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
242 int[] startPatternInfo = findStartPattern(row);
243 int startCode = startPatternInfo[2];
247 codeSet = CODE_CODE_A;
250 codeSet = CODE_CODE_B;
253 codeSet = CODE_CODE_C;
256 throw ReaderException.getInstance();
259 boolean done = false;
260 boolean isNextShifted = false;
262 StringBuffer result = new StringBuffer(20);
263 int lastStart = startPatternInfo[0];
264 int nextStart = startPatternInfo[1];
265 int[] counters = new int[6];
269 int checksumTotal = startCode;
271 boolean lastCharacterWasPrintable = true;
275 boolean unshift = isNextShifted;
276 isNextShifted = false;
278 // Save off last code
281 // Decode another code from image
282 code = decodeCode(row, counters, nextStart);
284 // Remember whether the last code was printable or not (excluding CODE_STOP)
285 if (code != CODE_STOP) {
286 lastCharacterWasPrintable = true;
289 // Add to checksum computation (if not CODE_STOP of course)
290 if (code != CODE_STOP) {
292 checksumTotal += multiplier * code;
295 // Advance to where the next code will to start
296 lastStart = nextStart;
297 for (int i = 0; i < counters.length; i++) {
298 nextStart += counters[i];
301 // Take care of illegal start codes
306 throw ReaderException.getInstance();
313 result.append((char) (' ' + code));
314 } else if (code < 96) {
315 result.append((char) (code - 64));
317 // Don't let CODE_STOP, which always appears, affect whether whether we think the last
318 // code was printable or not.
319 if (code != CODE_STOP) {
320 lastCharacterWasPrintable = false;
330 isNextShifted = true;
331 codeSet = CODE_CODE_B;
334 codeSet = CODE_CODE_B;
337 codeSet = CODE_CODE_C;
347 result.append((char) (' ' + code));
349 if (code != CODE_STOP) {
350 lastCharacterWasPrintable = false;
360 isNextShifted = true;
361 codeSet = CODE_CODE_C;
364 codeSet = CODE_CODE_A;
367 codeSet = CODE_CODE_C;
382 if (code != CODE_STOP) {
383 lastCharacterWasPrintable = false;
390 codeSet = CODE_CODE_A;
393 codeSet = CODE_CODE_B;
403 // Unshift back to another code set if we were shifted
407 codeSet = CODE_CODE_C;
410 codeSet = CODE_CODE_A;
413 codeSet = CODE_CODE_B;
420 // Check for ample whitespace following pattern, but, to do this we first need to remember that
421 // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
422 // to read off. Would be slightly better to properly read. Here we just skip it:
423 int width = row.getSize();
424 while (nextStart < width && row.get(nextStart)) {
427 if (!row.isRange(nextStart, Math.min(width, 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 ResultPoint(left, (float) rowNumber),
465 new ResultPoint(right, (float) rowNumber)},
466 BarcodeFormat.CODE_128);