5 * Created by Lukasz Warchol on 10-01-21.
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 "UPCEANReader.h"
22 #include <zxing/oned/OneDResultPoint.h>
23 #include <zxing/ReaderException.h>
28 * Start/end guard pattern.
30 static const int START_END_PATTERN[3] = {1, 1, 1};
33 * Pattern marking the middle of a UPC/EAN pattern, separating the two halves.
35 static const int MIDDLE_PATTERN_LEN = 5;
36 static const int MIDDLE_PATTERN[MIDDLE_PATTERN_LEN] = {1, 1, 1, 1, 1};
39 * "Odd", or "L" patterns used to encode UPC/EAN digits.
41 const int L_PATTERNS_LEN = 10;
42 const int L_PATTERNS_SUB_LEN = 4;
43 const int L_PATTERNS[10][4] = {
57 * As above but also including the "even", or "G" patterns used to encode UPC/EAN digits.
59 const int L_AND_G_PATTERNS_LEN = 20;
60 const int L_AND_G_PATTERNS_SUB_LEN = 4;
61 const int L_AND_G_PATTERNS[L_AND_G_PATTERNS_LEN][L_AND_G_PATTERNS_SUB_LEN] = {
72 {1, 1, 2, 3}, // 10 reversed 0
73 {1, 2, 2, 2}, // 11 reversed 1
74 {2, 2, 1, 2}, // 12 reversed 2
75 {1, 1, 4, 1}, // 13 reversed 3
76 {2, 3, 1, 1}, // 14 reversed 4
77 {1, 3, 2, 1}, // 15 reversed 5
78 {4, 1, 1, 1}, // 16 reversed 6
79 {2, 1, 3, 1}, // 17 reversed 7
80 {3, 1, 2, 1}, // 18 reversed 8
81 {2, 1, 1, 3} // 19 reversed 9
85 const int UPCEANReader::getMIDDLE_PATTERN_LEN(){
86 return MIDDLE_PATTERN_LEN;
88 const int* UPCEANReader::getMIDDLE_PATTERN(){
89 return MIDDLE_PATTERN;
92 UPCEANReader::UPCEANReader(){
96 Ref<Result> UPCEANReader::decodeRow(int rowNumber, Ref<BitArray> row){
97 return decodeRow(rowNumber, row, findStartGuardPattern(row));
99 Ref<Result> UPCEANReader::decodeRow(int rowNumber, Ref<BitArray> row, int startGuardRange[]){
101 std::string tmpResultString;
102 std::string& tmpResultStringRef = tmpResultString;
103 int endStart = decodeMiddle(row, startGuardRange, 2 /*reference findGuardPattern*/ , tmpResultStringRef);
105 int* endRange = decodeEnd(row, endStart);
107 #pragma mark QuietZone needs some change
108 // Make sure there is a quiet zone at least as big as the end pattern after the barcode. The
109 // spec might want more whitespace, but in practice this is the maximum we can count on.
110 // int end = endRange[1];
111 // int quietEnd = end + (end - endRange[0]);
112 // if (quietEnd >= row->getSize() || !row->isRange(end, quietEnd, false)) {
113 // throw ReaderException("Quiet zone asserrt fail.");
116 if (!checkChecksum(tmpResultString)) {
117 throw ReaderException("Checksum fail.");
120 Ref<String> resultString(new String(tmpResultString));
122 float left = (float) (startGuardRange[1] + startGuardRange[0]) / 2.0f;
123 float right = (float) (endRange[1] + endRange[0]) / 2.0f;
125 std::vector< Ref<ResultPoint> > resultPoints(2);
126 Ref<OneDResultPoint> resultPoint1(new OneDResultPoint(left, (float) rowNumber));
127 Ref<OneDResultPoint> resultPoint2(new OneDResultPoint(right, (float) rowNumber));
128 resultPoints[0] = resultPoint1;
129 resultPoints[1] = resultPoint2;
131 ArrayRef<unsigned char> resultBytes(1);
133 if (startGuardRange!=NULL) {
134 delete [] startGuardRange;
135 startGuardRange = NULL;
137 if (endRange!=NULL) {
142 Ref<Result> res(new Result(resultString, resultBytes, resultPoints, getBarcodeFormat()));
146 int* UPCEANReader::findStartGuardPattern(Ref<BitArray> row){
147 bool foundStart = false;
149 int* startRange = NULL;
151 while (!foundStart) {
152 startRange = findGuardPattern(row, nextStart, false, START_END_PATTERN, sizeof(START_END_PATTERN)/sizeof(int));
153 int start = startRange[0];
154 nextStart = startRange[1];
155 // Make sure there is a quiet zone at least as big as the start pattern before the barcode.
156 // If this check would run off the left edge of the image, do not accept this barcode,
157 // as it is very likely to be a false positive.
158 int quietStart = start - (nextStart - start);
159 if (quietStart >= 0) {
160 foundStart = row->isRange(quietStart, start, false);
166 int* UPCEANReader::findGuardPattern(Ref<BitArray> row, int rowOffset, bool whiteFirst, const int pattern[], int patternLen){
167 int patternLength = patternLen;
169 //int counters[patternLength];
170 int* counters = new int(patternLength);
171 int countersCount = sizeof(counters)/sizeof(int);
172 for (int i=0; i<countersCount ; i++) {
175 int width = row->getSize();
176 bool isWhite = false;
177 while (rowOffset < width) {
178 isWhite = !row->get(rowOffset);
179 if (whiteFirst == isWhite) {
185 int counterPosition = 0;
186 int patternStart = rowOffset;
187 for (int x = rowOffset; x < width; x++) {
188 bool pixel = row->get(x);
189 if (pixel ^ isWhite) {
190 counters[counterPosition]++;
192 if (counterPosition == patternLength - 1) {
193 if (patternMatchVariance(counters, countersCount, pattern, MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) {
194 int* resultValue = new int[2];
195 resultValue[0] = patternStart;
199 patternStart += counters[0] + counters[1];
200 for (int y = 2; y < patternLength; y++) {
201 counters[y - 2] = counters[y];
203 counters[patternLength - 2] = 0;
204 counters[patternLength - 1] = 0;
209 counters[counterPosition] = 1;
214 throw ReaderException("findGuardPattern");
217 int* UPCEANReader::decodeEnd(Ref<BitArray> row, int endStart){
218 return findGuardPattern(row, endStart, false, START_END_PATTERN, sizeof(START_END_PATTERN)/sizeof(int));
221 // int UPCEANReader::decodeDigit(Ref<BitArray> row, int counters[], int countersLen, int rowOffset, int** patterns/*[][]*/, int paterns1Len, int paterns2Len)
222 int UPCEANReader::decodeDigit(Ref<BitArray> row, int counters[], int countersLen, int rowOffset, UPC_EAN_PATTERNS paternType){
223 recordPattern(row, rowOffset, counters, countersLen);
224 int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
228 switch (paternType) {
229 case UPC_EAN_PATTERNS_L_PATTERNS:
230 max = L_PATTERNS_LEN;
231 for (int i = 0; i < max; i++) {
232 //int pattern[countersLen];
233 int* pattern = new int(countersLen);
234 for(int j = 0; j< countersLen; j++){
235 pattern[j] = L_PATTERNS[i][j];
238 int variance = patternMatchVariance(counters, countersLen, pattern, MAX_INDIVIDUAL_VARIANCE);
239 if (variance < bestVariance) {
240 bestVariance = variance;
246 case UPC_EAN_PATTERNS_L_AND_G_PATTERNS:
247 max = L_AND_G_PATTERNS_LEN;
248 for (int i = 0; i < max; i++) {
249 //int pattern[countersLen];
250 int* pattern = new int(countersLen);
251 for(int j = 0; j< countersLen; j++){
252 pattern[j] = L_AND_G_PATTERNS[i][j];
255 int variance = patternMatchVariance(counters, countersLen, pattern, MAX_INDIVIDUAL_VARIANCE);
256 if (variance < bestVariance) {
257 bestVariance = variance;
266 if (bestMatch >= 0) {
269 throw ReaderException("UPCEANReader::decodeDigit: No best mach");
275 * @return {@link #checkStandardUPCEANChecksum(String)}
277 bool UPCEANReader::checkChecksum(std::string s){
278 return checkStandardUPCEANChecksum(s);
282 * Computes the UPC/EAN checksum on a string of digits, and reports
283 * whether the checksum is correct or not.
285 * @param s string of digits to check
286 * @return true iff string of digits passes the UPC/EAN checksum algorithm
287 * @throws ReaderException if the string does not contain only digits
289 bool UPCEANReader::checkStandardUPCEANChecksum(std::string s){
290 int length = s.length();
296 for (int i = length - 2; i >= 0; i -= 2) {
297 int digit = (int) s[i] - (int) '0';
298 if (digit < 0 || digit > 9) {
299 throw ReaderException("checkStandardUPCEANChecksum");
304 for (int i = length - 1; i >= 0; i -= 2) {
305 int digit = (int) s[i] - (int) '0';
306 if (digit < 0 || digit > 9) {
307 throw ReaderException("checkStandardUPCEANChecksum");
311 return sum % 10 == 0;
313 UPCEANReader::~UPCEANReader(){