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