Added a project written on Qt framework for Symbian and added tutorials for both...
[zxing.git] / symbian / QQrDecoder / zxing / oned / UPCEReader.cpp
1 /*
2  *  UPCEReader.cpp
3  *  ZXing
4  *
5  *  Created by Lukasz Warchol on 10-01-26.
6  *  Copyright 2010 ZXing authors All rights reserved.
7  *
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #include "UPCEReader.h"
22 #include <zxing/ReaderException.h>
23
24 namespace zxing {
25         namespace oned {
26                 
27                 /**
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.
30                  */
31                 static const int MIDDLE_END_PATTERN[6] = {1, 1, 1, 1, 1, 1};
32                 
33                 /**
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.
37                  */
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}
41                 };
42                 
43                 UPCEReader::UPCEReader(){
44                         decodeMiddleCounters = new int[4];
45                         for (int i=0; i<4; i++) {
46                                 decodeMiddleCounters[i] = 0;
47                         }
48                 }
49                 
50                 int UPCEReader::decodeMiddle(Ref<BitArray> row, int startRange[], int startRangeLen, std::string& resultString){
51                         int countersLen = 4;
52                         int* counters = decodeMiddleCounters;
53                         counters[0] = 0;
54                         counters[1] = 0;
55                         counters[2] = 0;
56                         counters[3] = 0;
57                         
58                         
59                         int end = row->getSize();
60                         int rowOffset = startRange[1];
61                         
62                         int lgPatternFound = 0;
63                                                 
64                         for (int x = 0; x < 6 && rowOffset < end; x++) {
65                                 int bestMatch = decodeDigit(row, counters, countersLen, rowOffset, UPC_EAN_PATTERNS_L_AND_G_PATTERNS);
66                                 resultString.append(1, (char) ('0' + bestMatch % 10));
67                                 for (int i = 0; i < countersLen; i++) {
68                                         rowOffset += counters[i];
69                                 }
70                                 if (bestMatch >= 10) {
71                                         lgPatternFound |= 1 << (5 - x);
72                                 }
73                         }
74                         
75                         determineNumSysAndCheckDigit(resultString, lgPatternFound);
76                         
77                         return rowOffset;
78                 }
79                 
80                 int* UPCEReader::decodeEnd(Ref<BitArray> row, int endStart){
81                         return findGuardPattern(row, endStart, true, MIDDLE_END_PATTERN, sizeof(MIDDLE_END_PATTERN)/sizeof(int));
82                 }
83                 
84                 bool UPCEReader::checkChecksum(std::string s){
85                         return UPCEANReader::checkChecksum(convertUPCEtoUPCA(s));
86                 }
87                 
88                 
89                 void UPCEReader::determineNumSysAndCheckDigit(std::string& resultString, int lgPatternFound){
90                         for (int numSys = 0; numSys <= 1; numSys++) {
91                                 for (int d = 0; d < 10; d++) {
92                                         if (lgPatternFound == NUMSYS_AND_CHECK_DIGIT_PATTERNS[numSys][d]) {
93                                                 resultString.insert((size_t)0, (size_t)1, (char) ('0' + numSys));
94                                                 resultString.append(1, (char) ('0' + d));
95                                                 return;
96                                         }
97                                 }
98                         }
99                         throw ReaderException("determineNumSysAndCheckDigit exception");
100                 }
101                 
102                 /**
103                  * Expands a UPC-E value back into its full, equivalent UPC-A code value.
104                  *
105                  * @param upce UPC-E code as string of digits
106                  * @return equivalent UPC-A code as string of digits
107                  */
108                 std::string& UPCEReader::convertUPCEtoUPCA(std::string upce) {                  
109                         std::string result;
110                         result.append(1, upce[0]);
111                         char lastChar = upce[6];
112                         switch (lastChar) {
113                                 case '0':
114                                 case '1':
115                                 case '2':
116                                         result.append(upce.substr(1,2));
117                                         result.append(1, lastChar);
118                                         result.append("0000");
119                                         result.append(upce.substr(3,3));
120                                         break;
121                                 case '3':
122                                         result.append(upce.substr(1,3));
123                                         result.append("00000");
124                                         result.append(upce.substr(4,2));
125                                         break;
126                                 case '4':
127                                         result.append(upce.substr(1,4));
128                                         result.append("00000");
129                                         result.append(1, upce[5]);
130                                         break;
131                                 default:
132                                         result.append(upce.substr(1,5));
133                                         result.append("0000");
134                                         result.append(1, lastChar);
135                                         break;
136                         }
137                         result.append(1, upce[7]);
138                         std::string& returnResult = result;
139                         return returnResult;
140                 }
141                 
142                 
143                 BarcodeFormat UPCEReader::getBarcodeFormat(){
144                         return BarcodeFormat_UPC_E;
145                 }
146                 UPCEReader::~UPCEReader(){
147                         delete [] decodeMiddleCounters;
148                 }
149         }
150 }