2 * Copyright 2008 ZXing authors
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.ReaderException;
20 import com.google.zxing.BarcodeFormat;
21 import com.google.zxing.common.BitArray;
24 * <p>Implements decoding of the UPC-E format.</p>
26 * <p><a href="http://www.barcodeisland.com/upce.phtml">This</a> is a great reference for
27 * UPC-E information.</p>
29 * @author srowen@google.com (Sean Owen)
31 public final class UPCEReader extends AbstractUPCEANReader {
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.
37 private static final int[] MIDDLE_END_PATTERN = {1, 1, 1, 1, 1, 1};
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.
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}
49 protected int decodeMiddle(BitArray row, int[] startRange, StringBuffer result) throws ReaderException {
51 int[] counters = new int[4];
52 int end = row.getSize();
53 int rowOffset = startRange[1];
55 int lgPatternFound = 0;
57 for (int x = 0; x < 6 && rowOffset < end; x++) {
58 int bestMatch = decodeDigit(row, counters, rowOffset, L_AND_G_PATTERNS);
59 result.append((char) ('0' + bestMatch % 10));
60 for (int i = 0; i < counters.length; i++) {
61 rowOffset += counters[i];
63 if (bestMatch >= 10) {
64 lgPatternFound |= 1 << (5 - x);
68 determineNumSysAndCheckDigit(result, lgPatternFound);
73 protected int[] decodeEnd(BitArray row, int endStart) throws ReaderException {
74 return findGuardPattern(row, endStart, true, MIDDLE_END_PATTERN);
77 protected boolean checkChecksum(String s) throws ReaderException {
78 return super.checkChecksum(convertUPCEtoUPCA(s));
81 private static void determineNumSysAndCheckDigit(StringBuffer resultString, int lgPatternFound)
82 throws ReaderException {
84 for (int numSys = 0; numSys <= 1; numSys++) {
85 for (int d = 0; d < 10; d++) {
86 if (lgPatternFound == NUMSYS_AND_CHECK_DIGIT_PATTERNS[numSys][d]) {
87 resultString.insert(0, (char) ('0' + numSys));
88 resultString.append((char) ('0' + d));
93 throw new ReaderException("Unable to determine number system and check digit");
96 BarcodeFormat getBarcodeFormat() {
97 return BarcodeFormat.UPC_E;
101 * Expands a UPC-E value back into its full, equivalent UPC-A code value.
103 * @param upce UPC-E code as string of digits
104 * @return equivalent UPC-A code as string of digits
106 private static String convertUPCEtoUPCA(String upce) {
107 char[] upceChars = new char[6];
108 upce.getChars(1, 7, upceChars, 0);
109 StringBuffer result = new StringBuffer(12);
110 result.append(upce.charAt(0));
111 char lastChar = upceChars[5];
116 result.append(upceChars, 0, 2);
117 result.append(lastChar);
118 result.append("0000");
119 result.append(upceChars, 2, 3);
122 result.append(upceChars, 0, 3);
123 result.append("00000");
124 result.append(upceChars, 3, 2);
127 result.append(upceChars, 0, 4);
128 result.append("00000");
129 result.append(upceChars[4]);
132 result.append(upceChars, 0, 5);
133 result.append("0000");
134 result.append(lastChar);
137 result.append(upce.charAt(7));
138 return result.toString();