2 * Copyright 2008 Google Inc.
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_VARIANCE = 56;
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) || (pixel && !isWhite)) {
183 counters[counterPosition]++;
185 if (counterPosition == patternLength - 1) {
186 int bestVariance = MAX_VARIANCE;
188 for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
189 int variance = patternMatchVariance(counters, CODE_PATTERNS[startCode]);
190 if (variance < bestVariance) {
191 bestVariance = variance;
192 bestMatch = startCode;
195 if (bestMatch >= 0) {
196 return new int[]{patternStart, i, bestMatch};
198 patternStart += counters[0] + counters[1];
199 for (int y = 2; y < patternLength; y++) {
200 counters[y - 2] = counters[y];
202 counters[patternLength - 2] = 0;
203 counters[patternLength - 1] = 0;
208 counters[counterPosition] = 1;
212 throw new ReaderException("Can't find pattern");
215 private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException {
216 recordPattern(row, rowOffset, counters);
217 int bestVariance = MAX_VARIANCE; // worst variance we'll accept
219 for (int d = 0; d < CODE_PATTERNS.length; d++) {
220 int[] pattern = CODE_PATTERNS[d];
221 int variance = patternMatchVariance(counters, pattern);
222 if (variance < bestVariance) {
223 bestVariance = variance;
227 // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6
228 if (bestMatch >= 0) {
231 throw new ReaderException("Could not match any code pattern");
235 public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
237 int[] startPatternInfo = findStartPattern(row);
238 int startCode = startPatternInfo[2];
242 codeSet = CODE_CODE_A;
245 codeSet = CODE_CODE_B;
248 codeSet = CODE_CODE_C;
251 throw new ReaderException("Illegal start code");
254 boolean done = false;
255 boolean isNextShifted = false;
257 StringBuffer result = new StringBuffer();
258 int lastStart = startPatternInfo[0];
259 int nextStart = startPatternInfo[1];
260 int[] counters = new int[6];
264 int checksumTotal = startCode;
269 boolean unshift = isNextShifted;
270 isNextShifted = false;
273 code = decodeCode(row, counters, nextStart);
274 if (code != CODE_STOP) {
276 checksumTotal += multiplier * code;
279 lastStart = nextStart;
280 for (int i = 0; i < counters.length; i++) {
281 nextStart += counters[i];
284 // Take care of illegal start codes
289 throw new ReaderException("Unexpected start code");
296 result.append((char) (' ' + code));
297 } else if (code < 96) {
298 result.append((char) (code - 64));
308 isNextShifted = true;
309 codeSet = CODE_CODE_B;
312 codeSet = CODE_CODE_B;
315 codeSet = CODE_CODE_C;
325 result.append((char) (' ' + code));
335 isNextShifted = true;
336 codeSet = CODE_CODE_C;
339 codeSet = CODE_CODE_A;
342 codeSet = CODE_CODE_C;
362 codeSet = CODE_CODE_A;
365 codeSet = CODE_CODE_B;
378 codeSet = CODE_CODE_C;
381 codeSet = CODE_CODE_A;
384 codeSet = CODE_CODE_B;
391 // Pull out from sum the value of the penultimate check code
392 checksumTotal -= multiplier * lastCode;
393 if (checksumTotal % 103 != lastCode) {
394 throw new ReaderException("Checksum failed");
397 // Need to pull out the check digits from string
398 int resultLength = result.length();
399 if (resultLength > 0) {
400 if (codeSet == CODE_CODE_C) {
401 result.delete(resultLength - 2, resultLength);
403 result.delete(resultLength - 1, resultLength);
407 String resultString = result.toString();
408 float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
409 float right = (float) (nextStart + lastStart) / 2.0f;
414 new GenericResultPoint(left, (float) rowNumber),
415 new GenericResultPoint(right, (float) rowNumber)},
416 BarcodeFormat.CODE_128);