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.Result;
20 import com.google.zxing.ReaderException;
21 import com.google.zxing.ResultPoint;
22 import com.google.zxing.common.BitArray;
23 import com.google.zxing.common.GenericResultPoint;
26 * <p>Decodes Code 128 barcodes.</p>
28 * @author srowen@google.com (Sean Owen)
30 public final class Code128Reader extends AbstractOneDReader {
32 private static final int[][] CODE_PATTERNS = {
33 {2, 1, 2, 2, 2, 2}, // 0
38 {1, 3, 1, 2, 2, 2}, // 5
43 {2, 2, 1, 3, 1, 2}, // 10
48 {1, 1, 3, 2, 2, 2}, // 15
53 {2, 2, 1, 2, 3, 1}, // 20
58 {3, 2, 1, 1, 2, 2}, // 25
63 {2, 1, 2, 1, 2, 3}, // 30
68 {1, 3, 1, 3, 2, 1}, // 35
73 {2, 3, 1, 1, 1, 3}, // 40
78 {1, 1, 3, 1, 2, 3}, // 45
83 {2, 3, 1, 1, 3, 1}, // 50
88 {3, 1, 1, 3, 2, 1}, // 55
93 {3, 1, 4, 1, 1, 1}, // 60
98 {1, 2, 1, 1, 2, 4}, // 65
103 {1, 1, 2, 4, 1, 2}, // 70
108 {2, 4, 1, 2, 1, 1}, // 75
113 {1, 1, 1, 2, 4, 2}, // 80
118 {1, 2, 4, 2, 1, 1}, // 85
123 {2, 1, 4, 1, 2, 1}, // 90
128 {1, 1, 4, 1, 1, 3}, // 95
133 {1, 1, 4, 1, 3, 1}, // 100
138 {2, 1, 1, 2, 3, 2}, // 105
139 {2, 3, 3, 1, 1, 1, 2}
142 private static final float MAX_VARIANCE = 0.4f;
144 private static final int CODE_SHIFT = 98;
146 private static final int CODE_CODE_C = 99;
147 private static final int CODE_CODE_B = 100;
148 private static final int CODE_CODE_A = 101;
150 private static final int CODE_FNC_1 = 102;
151 private static final int CODE_FNC_2 = 97;
152 private static final int CODE_FNC_3 = 96;
153 private static final int CODE_FNC_4_A = 101;
154 private static final int CODE_FNC_4_B = 100;
156 private static final int CODE_START_A = 103;
157 private static final int CODE_START_B = 104;
158 private static final int CODE_START_C = 105;
159 private static final int CODE_STOP = 106;
161 private static int[] findStartPattern(BitArray row) throws ReaderException {
162 int width = row.getSize();
164 while (rowOffset < width) {
165 if (row.get(rowOffset)) {
171 int counterPosition = 0;
172 int[] counters = new int[6];
173 int patternStart = rowOffset;
174 boolean isWhite = false;
175 int patternLength = counters.length;
177 for (int i = rowOffset; i < width; i++) {
178 boolean pixel = row.get(i);
179 if ((!pixel && isWhite) || (pixel && !isWhite)) {
180 counters[counterPosition]++;
182 if (counterPosition == patternLength - 1) {
183 for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
184 if (patternMatchVariance(counters, CODE_PATTERNS[startCode]) < MAX_VARIANCE) {
185 return new int[] {patternStart, i, startCode};
188 patternStart += counters[0] + counters[1];
189 for (int y = 2; y < patternLength; y++) {
190 counters[y - 2] = counters[y];
196 counters[counterPosition] = 1;
200 throw new ReaderException("Can't find pattern");
203 private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException {
204 recordPattern(row, rowOffset, counters);
205 float bestVariance = 0.4f; // worst variance we'll accept
207 for (int d = 0; d < CODE_PATTERNS.length; d++) {
208 int[] pattern = CODE_PATTERNS[d];
209 float variance = patternMatchVariance(counters, pattern);
210 if (variance < bestVariance) {
211 bestVariance = variance;
215 // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6
216 if (bestMatch >= 0) {
219 throw new ReaderException("Could not match any code pattern");
223 public Result decodeRow(final int rowNumber, final BitArray row) throws ReaderException {
225 int[] startPatternInfo = findStartPattern(row);
226 int startCode = startPatternInfo[2];
230 codeSet = CODE_CODE_A;
233 codeSet = CODE_CODE_B;
236 codeSet = CODE_CODE_C;
239 throw new ReaderException("Illegal start code");
242 boolean done = false;
243 boolean isNextShifted = false;
245 StringBuffer result = new StringBuffer();
246 int lastStart = startPatternInfo[0];
247 int nextStart = startPatternInfo[1];
248 int[] counters = new int[6];
252 int checksumTotal = startCode;
257 boolean unshift = isNextShifted;
258 isNextShifted = false;
261 code = decodeCode(row, counters, nextStart);
262 if (code != CODE_STOP) {
264 checksumTotal += multiplier * code;
267 lastStart = nextStart;
268 for (int i = 0; i < counters.length; i++) {
269 nextStart += counters[i];
272 // Take care of illegal start codes
277 throw new ReaderException("Unexpected start code");
284 result.append((char) (' ' + code));
285 } else if (code < 96) {
286 result.append((char) (code - 64));
296 isNextShifted = true;
297 codeSet = CODE_CODE_B;
300 codeSet = CODE_CODE_B;
303 codeSet = CODE_CODE_C;
313 result.append((char) (' ' + code));
323 isNextShifted = true;
324 codeSet = CODE_CODE_C;
327 codeSet = CODE_CODE_A;
330 codeSet = CODE_CODE_C;
350 codeSet = CODE_CODE_A;
353 codeSet = CODE_CODE_B;
366 codeSet = CODE_CODE_C;
369 codeSet = CODE_CODE_A;
372 codeSet = CODE_CODE_B;
379 // Pull out from sum the value of the penultimate check code
380 checksumTotal -= multiplier * lastCode;
381 if (checksumTotal % 103 != lastCode) {
382 throw new ReaderException("Checksum failed");
385 // Need to pull out the check digits from string
386 int resultLength = result.length();
387 if (resultLength > 0) {
388 if (codeSet == CODE_CODE_C) {
389 result.delete(resultLength - 2, resultLength);
391 result.delete(resultLength - 1, resultLength);
395 String resultString = result.toString();
396 return new Result(resultString,
397 new ResultPoint[]{new GenericResultPoint((float) (startPatternInfo[1] - startPatternInfo[0]) / 2.0f,
399 new GenericResultPoint((float) (nextStart - lastStart) / 2.0f,
400 (float) rowNumber)});