Major refactoring of 1D barcode code. Moved into com.google.zxing.oned package. Misc...
[zxing.git] / core / src / com / google / zxing / oned / UPCEReader.java
1 /*
2  * Copyright 2008 Google Inc.
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.ReaderException;
20 import com.google.zxing.common.BitArray;
21
22 /**
23  * <p>Implements decoding of the UPC-E format.</p>
24  * <p/>
25  * <p><a href="http://www.barcodeisland.com/upce.phtml">This</a> is a great reference for
26  * UPC-E information.</p>
27  *
28  * @author srowen@google.com (Sean Owen)
29  */
30 public final class UPCEReader extends AbstractUPCEANReader {
31
32   /**
33    * The pattern that marks the middle, and end, of a UPC-E pattern.
34    * There is no "second half" to a UPC-E barcode.
35    */
36   private static final int[] MIDDLE_END_PATTERN = {1, 1, 1, 1, 1, 1};
37
38   /**
39    * See {@link #L_AND_G_PATTERNS}; these values similarly represent patterns of
40    * even-odd parity encodings of digits that imply both the number system (0 or 1)
41    * used, and the check digit.
42    */
43   private static final int[][] NUMSYS_AND_CHECK_DIGIT_PATTERNS = {
44       {0x38, 0x34, 0x32, 0x31, 0x2C, 0x26, 0x23, 0x2A, 0x29, 0x25},
45       {0x07, 0x0B, 0x0D, 0x0E, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A}
46   };
47
48   protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer result) throws ReaderException {
49
50     int[] counters = new int[4];
51     int end = row.getSize();
52     int rowOffset = startRange[1];
53
54     int lgPatternFound = 0;
55
56     for (int x = 0; x < 6 && rowOffset < end; x++) {
57       int bestMatch = decodeDigit(row, counters, rowOffset, L_AND_G_PATTERNS);
58       result.append((char) ('0' + bestMatch % 10));
59       for (int i = 0; i < counters.length; i++) {
60         rowOffset += counters[i];
61       }
62       if (bestMatch >= 10) {
63         lgPatternFound |= 1 << (5 - x);
64       }
65     }
66
67     determineNumSysAndCheckDigit(result, lgPatternFound);
68
69     return rowOffset;
70   }
71
72   protected int[] decodeEnd(BitArray row, int endStart) throws ReaderException {
73     return findGuardPattern(row, endStart, true, MIDDLE_END_PATTERN);
74   }
75
76   protected boolean checkChecksum(String s) throws ReaderException {
77     return super.checkChecksum(convertUPCEtoUPCA(s));
78   }
79
80   private static void determineNumSysAndCheckDigit(StringBuffer resultString, int lgPatternFound)
81       throws ReaderException {
82
83     for (int numSys = 0; numSys <= 1; numSys++) {
84       for (int d = 0; d < 10; d++) {
85         if (lgPatternFound == NUMSYS_AND_CHECK_DIGIT_PATTERNS[numSys][d]) {
86           resultString.insert(0, (char) ('0' + numSys));
87           resultString.append((char) ('0' + d));
88           return;
89         }
90       }
91     }
92     throw new ReaderException("Unable to determine number system and check digit");
93   }
94
95   /**
96    * Expands a UPC-E value back into its full, equivalent UPC-A code value.
97    *
98    * @param upce UPC-E code as string of digits
99    * @return equivalent UPC-A code as string of digits
100    */
101   private static String convertUPCEtoUPCA(String upce) {
102     char[] upceChars = new char[6];
103     upce.getChars(1, 7, upceChars, 0);
104     StringBuffer result = new StringBuffer(12);
105     result.append(upce.charAt(0));
106     char lastChar = upceChars[5];
107     switch (lastChar) {
108       case '0':
109       case '1':
110       case '2':
111         result.append(upceChars, 0, 2);
112         result.append(lastChar);
113         result.append("0000");
114         result.append(upceChars, 2, 3);
115         break;
116       case '3':
117         result.append(upceChars, 0, 3);
118         result.append("00000");
119         result.append(upceChars, 3, 2);
120         break;
121       case '4':
122         result.append(upceChars, 0, 4);
123         result.append("00000");
124         result.append(upceChars[4]);
125         break;
126       default:
127         result.append(upceChars, 0, 5);
128         result.append("0000");
129         result.append(lastChar);
130         break;
131     }
132     result.append(upce.charAt(7));
133     return result.toString();
134   }
135
136 }