Fix bug that would cut out part of result string if final checksum value happened...
[zxing.git] / core / src / com / google / zxing / oned / Code128Reader.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.oned;
18
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;
25
26 import java.util.Hashtable;
27
28 /**
29  * <p>Decodes Code 128 barcodes.</p>
30  *
31  * @author srowen@google.com (Sean Owen)
32  */
33 public final class Code128Reader extends AbstractOneDReader {
34
35   private static final int[][] CODE_PATTERNS = {
36       {2, 1, 2, 2, 2, 2}, // 0
37       {2, 2, 2, 1, 2, 2},
38       {2, 2, 2, 2, 2, 1},
39       {1, 2, 1, 2, 2, 3},
40       {1, 2, 1, 3, 2, 2},
41       {1, 3, 1, 2, 2, 2}, // 5
42       {1, 2, 2, 2, 1, 3},
43       {1, 2, 2, 3, 1, 2},
44       {1, 3, 2, 2, 1, 2},
45       {2, 2, 1, 2, 1, 3},
46       {2, 2, 1, 3, 1, 2}, // 10
47       {2, 3, 1, 2, 1, 2},
48       {1, 1, 2, 2, 3, 2},
49       {1, 2, 2, 1, 3, 2},
50       {1, 2, 2, 2, 3, 1},
51       {1, 1, 3, 2, 2, 2}, // 15
52       {1, 2, 3, 1, 2, 2},
53       {1, 2, 3, 2, 2, 1},
54       {2, 2, 3, 2, 1, 1},
55       {2, 2, 1, 1, 3, 2},
56       {2, 2, 1, 2, 3, 1}, // 20
57       {2, 1, 3, 2, 1, 2},
58       {2, 2, 3, 1, 1, 2},
59       {3, 1, 2, 1, 3, 1},
60       {3, 1, 1, 2, 2, 2},
61       {3, 2, 1, 1, 2, 2}, // 25
62       {3, 2, 1, 2, 2, 1},
63       {3, 1, 2, 2, 1, 2},
64       {3, 2, 2, 1, 1, 2},
65       {3, 2, 2, 2, 1, 1},
66       {2, 1, 2, 1, 2, 3}, // 30
67       {2, 1, 2, 3, 2, 1},
68       {2, 3, 2, 1, 2, 1},
69       {1, 1, 1, 3, 2, 3},
70       {1, 3, 1, 1, 2, 3},
71       {1, 3, 1, 3, 2, 1}, // 35
72       {1, 1, 2, 3, 1, 3},
73       {1, 3, 2, 1, 1, 3},
74       {1, 3, 2, 3, 1, 1},
75       {2, 1, 1, 3, 1, 3},
76       {2, 3, 1, 1, 1, 3}, // 40
77       {2, 3, 1, 3, 1, 1},
78       {1, 1, 2, 1, 3, 3},
79       {1, 1, 2, 3, 3, 1},
80       {1, 3, 2, 1, 3, 1},
81       {1, 1, 3, 1, 2, 3}, // 45
82       {1, 1, 3, 3, 2, 1},
83       {1, 3, 3, 1, 2, 1},
84       {3, 1, 3, 1, 2, 1},
85       {2, 1, 1, 3, 3, 1},
86       {2, 3, 1, 1, 3, 1}, // 50
87       {2, 1, 3, 1, 1, 3},
88       {2, 1, 3, 3, 1, 1},
89       {2, 1, 3, 1, 3, 1},
90       {3, 1, 1, 1, 2, 3},
91       {3, 1, 1, 3, 2, 1}, // 55
92       {3, 3, 1, 1, 2, 1},
93       {3, 1, 2, 1, 1, 3},
94       {3, 1, 2, 3, 1, 1},
95       {3, 3, 2, 1, 1, 1},
96       {3, 1, 4, 1, 1, 1}, // 60
97       {2, 2, 1, 4, 1, 1},
98       {4, 3, 1, 1, 1, 1},
99       {1, 1, 1, 2, 2, 4},
100       {1, 1, 1, 4, 2, 2},
101       {1, 2, 1, 1, 2, 4}, // 65
102       {1, 2, 1, 4, 2, 1},
103       {1, 4, 1, 1, 2, 2},
104       {1, 4, 1, 2, 2, 1},
105       {1, 1, 2, 2, 1, 4},
106       {1, 1, 2, 4, 1, 2}, // 70
107       {1, 2, 2, 1, 1, 4},
108       {1, 2, 2, 4, 1, 1},
109       {1, 4, 2, 1, 1, 2},
110       {1, 4, 2, 2, 1, 1},
111       {2, 4, 1, 2, 1, 1}, // 75
112       {2, 2, 1, 1, 1, 4},
113       {4, 1, 3, 1, 1, 1},
114       {2, 4, 1, 1, 1, 2},
115       {1, 3, 4, 1, 1, 1},
116       {1, 1, 1, 2, 4, 2}, // 80
117       {1, 2, 1, 1, 4, 2},
118       {1, 2, 1, 2, 4, 1},
119       {1, 1, 4, 2, 1, 2},
120       {1, 2, 4, 1, 1, 2},
121       {1, 2, 4, 2, 1, 1}, // 85
122       {4, 1, 1, 2, 1, 2},
123       {4, 2, 1, 1, 1, 2},
124       {4, 2, 1, 2, 1, 1},
125       {2, 1, 2, 1, 4, 1},
126       {2, 1, 4, 1, 2, 1}, // 90
127       {4, 1, 2, 1, 2, 1},
128       {1, 1, 1, 1, 4, 3},
129       {1, 1, 1, 3, 4, 1},
130       {1, 3, 1, 1, 4, 1},
131       {1, 1, 4, 1, 1, 3}, // 95
132       {1, 1, 4, 3, 1, 1},
133       {4, 1, 1, 1, 1, 3},
134       {4, 1, 1, 3, 1, 1},
135       {1, 1, 3, 1, 4, 1},
136       {1, 1, 4, 1, 3, 1}, // 100
137       {3, 1, 1, 1, 4, 1},
138       {4, 1, 1, 1, 3, 1},
139       {2, 1, 1, 4, 1, 2},
140       {2, 1, 1, 2, 1, 4},
141       {2, 1, 1, 2, 3, 2}, // 105
142       {2, 3, 3, 1, 1, 1, 2}
143   };
144
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);
147
148   private static final int CODE_SHIFT = 98;
149
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;
153
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;
159
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;
164
165   private static int[] findStartPattern(BitArray row) throws ReaderException {
166     int width = row.getSize();
167     int rowOffset = 0;
168     while (rowOffset < width) {
169       if (row.get(rowOffset)) {
170         break;
171       }
172       rowOffset++;
173     }
174
175     int counterPosition = 0;
176     int[] counters = new int[6];
177     int patternStart = rowOffset;
178     boolean isWhite = false;
179     int patternLength = counters.length;
180
181     for (int i = rowOffset; i < width; i++) {
182       boolean pixel = row.get(i);
183       if ((!pixel && isWhite) || (pixel && !isWhite)) {
184         counters[counterPosition]++;
185       } else {
186         if (counterPosition == patternLength - 1) {
187           int bestVariance = MAX_AVG_VARIANCE;
188           int bestMatch = -1;
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;
194             }
195           }
196           if (bestMatch >= 0) {
197             return new int[]{patternStart, i, bestMatch};
198           }
199           patternStart += counters[0] + counters[1];
200           for (int y = 2; y < patternLength; y++) {
201             counters[y - 2] = counters[y];
202           }
203           counters[patternLength - 2] = 0;
204           counters[patternLength - 1] = 0;
205           counterPosition--;
206         } else {
207           counterPosition++;
208         }
209         counters[counterPosition] = 1;
210         isWhite = !isWhite;
211       }
212     }
213     throw new ReaderException("Can't find pattern");
214   }
215
216   private static int decodeCode(BitArray row, int[] counters, int rowOffset) throws ReaderException {
217     recordPattern(row, rowOffset, counters);
218     int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
219     int bestMatch = -1;
220     for (int d = 0; d < CODE_PATTERNS.length; d++) {
221       int[] pattern = CODE_PATTERNS[d];
222       int variance = patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
223       if (variance < bestVariance) {
224         bestVariance = variance;
225         bestMatch = d;
226       }
227     }
228     // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6
229     if (bestMatch >= 0) {
230       return bestMatch;
231     } else {
232       throw new ReaderException("Could not match any code pattern");
233     }
234   }
235
236   public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
237
238     int[] startPatternInfo = findStartPattern(row);
239     int startCode = startPatternInfo[2];
240     int codeSet;
241     switch (startCode) {
242       case CODE_START_A:
243         codeSet = CODE_CODE_A;
244         break;
245       case CODE_START_B:
246         codeSet = CODE_CODE_B;
247         break;
248       case CODE_START_C:
249         codeSet = CODE_CODE_C;
250         break;
251       default:
252         throw new ReaderException("Illegal start code");
253     }
254
255     boolean done = false;
256     boolean isNextShifted = false;
257
258     StringBuffer result = new StringBuffer();
259     int lastStart = startPatternInfo[0];
260     int nextStart = startPatternInfo[1];
261     int[] counters = new int[6];
262
263     int lastCode = 0;
264     int code = 0;
265     int checksumTotal = startCode;
266     int multiplier = 0;
267     boolean lastCharacterWasPrintable = true;
268
269     while (!done) {
270
271       lastCharacterWasPrintable = true;
272
273       boolean unshift = isNextShifted;
274       isNextShifted = false;
275
276       lastCode = code;
277       code = decodeCode(row, counters, nextStart);
278       if (code != CODE_STOP) {
279         multiplier++;
280         checksumTotal += multiplier * code;
281       }
282
283       lastStart = nextStart;
284       for (int i = 0; i < counters.length; i++) {
285         nextStart += counters[i];
286       }
287
288       // Take care of illegal start codes
289       switch (code) {
290         case CODE_START_A:
291         case CODE_START_B:
292         case CODE_START_C:
293           throw new ReaderException("Unexpected start code");
294       }
295
296       switch (codeSet) {
297
298         case CODE_CODE_A:
299           if (code < 64) {
300             result.append((char) (' ' + code));
301           } else if (code < 96) {
302             result.append((char) (code - 64));
303           } else {
304             lastCharacterWasPrintable = false;
305             switch (code) {
306               case CODE_FNC_1:
307               case CODE_FNC_2:
308               case CODE_FNC_3:
309               case CODE_FNC_4_A:
310                 // do nothing?
311                 break;
312               case CODE_SHIFT:
313                 isNextShifted = true;
314                 codeSet = CODE_CODE_B;
315                 break;
316               case CODE_CODE_B:
317                 codeSet = CODE_CODE_B;
318                 break;
319               case CODE_CODE_C:
320                 codeSet = CODE_CODE_C;
321                 break;
322               case CODE_STOP:
323                 done = true;
324                 break;
325             }
326           }
327           break;
328         case CODE_CODE_B:
329           if (code < 96) {
330             result.append((char) (' ' + code));
331           } else {
332             lastCharacterWasPrintable = false;
333             switch (code) {
334               case CODE_FNC_1:
335               case CODE_FNC_2:
336               case CODE_FNC_3:
337               case CODE_FNC_4_B:
338                 // do nothing?
339                 break;
340               case CODE_SHIFT:
341                 isNextShifted = true;
342                 codeSet = CODE_CODE_C;
343                 break;
344               case CODE_CODE_A:
345                 codeSet = CODE_CODE_A;
346                 break;
347               case CODE_CODE_C:
348                 codeSet = CODE_CODE_C;
349                 break;
350               case CODE_STOP:
351                 done = true;
352                 break;
353             }
354           }
355           break;
356         case CODE_CODE_C:
357           if (code < 100) {
358             if (code < 10) {
359               result.append('0');
360             }
361             result.append(code);
362           } else {
363             lastCharacterWasPrintable = false;
364             switch (code) {
365               case CODE_FNC_1:
366                 // do nothing?
367                 break;
368               case CODE_CODE_A:
369                 codeSet = CODE_CODE_A;
370                 break;
371               case CODE_CODE_B:
372                 codeSet = CODE_CODE_B;
373                 break;
374               case CODE_STOP:
375                 done = true;
376                 break;
377             }
378           }
379           break;
380       }
381
382       if (unshift) {
383         switch (codeSet) {
384           case CODE_CODE_A:
385             codeSet = CODE_CODE_C;
386             break;
387           case CODE_CODE_B:
388             codeSet = CODE_CODE_A;
389             break;
390           case CODE_CODE_C:
391             codeSet = CODE_CODE_B;
392             break;
393         }
394       }
395
396     }
397
398     // Pull out from sum the value of the penultimate check code
399     checksumTotal -= multiplier * lastCode;
400     if (checksumTotal % 103 != lastCode) {
401       throw new ReaderException("Checksum failed");
402     }
403
404     // Need to pull out the check digits from string
405     int resultLength = result.length();
406     if (resultLength > 0 && lastCharacterWasPrintable) {
407       if (codeSet == CODE_CODE_C) {
408         result.delete(resultLength - 2, resultLength);
409       } else {
410         result.delete(resultLength - 1, resultLength);
411       }
412     }
413
414     String resultString = result.toString();
415
416     if (resultString.length() == 0) {
417       // Almost surely a false positive
418       throw new ReaderException("Empty barcode found; assuming a false positive");
419     }
420
421     float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
422     float right = (float) (nextStart + lastStart) / 2.0f;
423     return new Result(
424         resultString,
425         null,
426         new ResultPoint[]{
427             new GenericResultPoint(left, (float) rowNumber),
428             new GenericResultPoint(right, (float) rowNumber)},
429         BarcodeFormat.CODE_128);
430
431   }
432
433 }