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