5 * Created by Lukasz Warchol on 10-01-26.
6 * Copyright 2010 ZXing authors All rights reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 #include "UPCEReader.h"
22 #include <zxing/ReaderException.h>
28 * The pattern that marks the middle, and end, of a UPC-E pattern.
29 * There is no "second half" to a UPC-E barcode.
31 static const int MIDDLE_END_PATTERN[6] = {1, 1, 1, 1, 1, 1};
34 * See {@link #L_AND_G_PATTERNS}; these values similarly represent patterns of
35 * even-odd parity encodings of digits that imply both the number system (0 or 1)
36 * used, and the check digit.
38 static const int NUMSYS_AND_CHECK_DIGIT_PATTERNS[2][10] = {
39 {0x38, 0x34, 0x32, 0x31, 0x2C, 0x26, 0x23, 0x2A, 0x29, 0x25},
40 {0x07, 0x0B, 0x0D, 0x0E, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A}
43 UPCEReader::UPCEReader(){}
45 int UPCEReader::decodeMiddle(Ref<BitArray> row, int startRange[], int startRangeLen, std::string& resultString){
46 const int countersLen = 4;
47 int counters[countersLen] = { 0, 0, 0, 0 };
49 int end = row->getSize();
50 int rowOffset = startRange[1];
52 int lgPatternFound = 0;
54 for (int x = 0; x < 6 && rowOffset < end; x++) {
55 int bestMatch = decodeDigit(row, counters, countersLen, rowOffset, UPC_EAN_PATTERNS_L_AND_G_PATTERNS);
56 resultString.append(1, (char) ('0' + bestMatch % 10));
57 for (int i = 0; i < countersLen; i++) {
58 rowOffset += counters[i];
60 if (bestMatch >= 10) {
61 lgPatternFound |= 1 << (5 - x);
65 determineNumSysAndCheckDigit(resultString, lgPatternFound);
70 int* UPCEReader::decodeEnd(Ref<BitArray> row, int endStart){
71 return findGuardPattern(row, endStart, true, MIDDLE_END_PATTERN, sizeof(MIDDLE_END_PATTERN)/sizeof(int));
74 bool UPCEReader::checkChecksum(std::string s){
75 return UPCEANReader::checkChecksum(convertUPCEtoUPCA(s));
79 void UPCEReader::determineNumSysAndCheckDigit(std::string& resultString, int lgPatternFound){
80 for (int numSys = 0; numSys <= 1; numSys++) {
81 for (int d = 0; d < 10; d++) {
82 if (lgPatternFound == NUMSYS_AND_CHECK_DIGIT_PATTERNS[numSys][d]) {
83 resultString.insert(0, 1, (char) ('0' + numSys));
84 resultString.append(1, (char) ('0' + d));
89 throw ReaderException("determineNumSysAndCheckDigit exception");
93 * Expands a UPC-E value back into its full, equivalent UPC-A code value.
95 * @param upce UPC-E code as string of digits
96 * @return equivalent UPC-A code as string of digits
98 std::string UPCEReader::convertUPCEtoUPCA(std::string upce) {
100 result.append(1, upce[0]);
101 char lastChar = upce[6];
106 result.append(upce.substr(1,2));
107 result.append(1, lastChar);
108 result.append("0000");
109 result.append(upce.substr(3,3));
112 result.append(upce.substr(1,3));
113 result.append("00000");
114 result.append(upce.substr(4,2));
117 result.append(upce.substr(1,4));
118 result.append("00000");
119 result.append(1, upce[5]);
122 result.append(upce.substr(1,5));
123 result.append("0000");
124 result.append(1, lastChar);
127 result.append(1, upce[7]);
132 BarcodeFormat UPCEReader::getBarcodeFormat(){
133 return BarcodeFormat_UPC_E;