5 * Created by Lukasz Warchol on 10-01-15.
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 "Code128Reader.h"
22 #include <zxing/oned/OneDResultPoint.h>
23 #include <zxing/common/Array.h>
24 #include <zxing/ReaderException.h>
31 const int CODE_PATTERNS_LENGHT = 107;
32 const int countersLenght = 6;
33 static const int CODE_PATTERNS[CODE_PATTERNS_LENGHT][countersLenght] = {
34 {2, 1, 2, 2, 2, 2}, /* 0 */
39 {1, 3, 1, 2, 2, 2}, /* 5 */
44 {2, 2, 1, 3, 1, 2}, /* 10 */
49 {1, 1, 3, 2, 2, 2}, /* 15 */
54 {2, 2, 1, 2, 3, 1}, /* 20 */
59 {3, 2, 1, 1, 2, 2}, /* 25 */
64 {2, 1, 2, 1, 2, 3}, /* 30 */
69 {1, 3, 1, 3, 2, 1}, /* 35 */
74 {2, 3, 1, 1, 1, 3}, /* 40 */
79 {1, 1, 3, 1, 2, 3}, /* 45 */
84 {2, 3, 1, 1, 3, 1}, /* 50 */
89 {3, 1, 1, 3, 2, 1}, /* 55 */
94 {3, 1, 4, 1, 1, 1}, /* 60 */
99 {1, 2, 1, 1, 2, 4}, /* 65 */
104 {1, 1, 2, 4, 1, 2}, /* 70 */
109 {2, 4, 1, 2, 1, 1}, /* 75 */
114 {1, 1, 1, 2, 4, 2}, /* 80 */
119 {1, 2, 4, 2, 1, 1}, /* 85 */
124 {2, 1, 4, 1, 2, 1}, /* 90 */
129 {1, 1, 4, 1, 1, 3}, /* 95 */
134 {1, 1, 4, 1, 3, 1}, /* 100 */
139 {2, 1, 1, 2, 3, 2}, /* 105 */
144 Code128Reader::Code128Reader(){
147 int* Code128Reader::findStartPattern(Ref<BitArray> row){
148 int width = row->getSize();
150 while (rowOffset < width) {
151 if (row->get(rowOffset)) {
157 int counterPosition = 0;
158 int counters[countersLenght] = {0,0,0,0,0,0};
159 int patternStart = rowOffset;
160 bool isWhite = false;
161 int patternLength = sizeof(counters) / sizeof(int);
163 for (int i = rowOffset; i < width; i++) {
164 bool pixel = row->get(i);
165 if (pixel ^ isWhite) {
166 counters[counterPosition]++;
168 if (counterPosition == patternLength - 1) {
169 int bestVariance = MAX_AVG_VARIANCE;
171 for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
172 int variance = patternMatchVariance(counters, sizeof(counters)/sizeof(int), CODE_PATTERNS[startCode], MAX_INDIVIDUAL_VARIANCE);
173 if (variance < bestVariance) {
174 bestVariance = variance;
175 bestMatch = startCode;
178 if (bestMatch >= 0) {
179 // Look for whitespace before start pattern, >= 50% of width of start pattern
180 if (row->isRange(fmaxl(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
181 int* resultValue = new int[3];
182 resultValue[0] = patternStart;
184 resultValue[2] = bestMatch;
188 patternStart += counters[0] + counters[1];
189 for (int y = 2; y < patternLength; y++) {
190 counters[y - 2] = counters[y];
192 counters[patternLength - 2] = 0;
193 counters[patternLength - 1] = 0;
198 counters[counterPosition] = 1;
202 throw ReaderException("");
205 int Code128Reader::decodeCode(Ref<BitArray> row, int counters[], int countersCount, int rowOffset){
206 recordPattern(row, rowOffset, counters, countersCount);
207 int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
209 for (int d = 0; d < CODE_PATTERNS_LENGHT; d++) {
210 int pattern[countersLenght];
212 for(int ind = 0; ind< countersLenght; ind++){
213 pattern[ind] = CODE_PATTERNS[d][ind];
215 // memcpy(pattern, CODE_PATTERNS[d], countersLenght);
216 int variance = patternMatchVariance(counters, countersCount, pattern, MAX_INDIVIDUAL_VARIANCE);
217 if (variance < bestVariance) {
218 bestVariance = variance;
222 // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6.
223 if (bestMatch >= 0) {
226 throw ReaderException("");
230 Ref<Result> Code128Reader::decodeRow(int rowNumber, Ref<BitArray> row){
232 int* startPatternInfo = findStartPattern(row);
233 int startCode = startPatternInfo[2];
237 codeSet = CODE_CODE_A;
240 codeSet = CODE_CODE_B;
243 codeSet = CODE_CODE_C;
246 throw ReaderException("");
250 bool isNextShifted = false;
252 std::string tmpResultString;
255 int lastStart = startPatternInfo[0];
256 int nextStart = startPatternInfo[1];
257 int counters[countersLenght] = {0,0,0,0,0,0};
261 int checksumTotal = startCode;
263 bool lastCharacterWasPrintable = true;
267 bool unshift = isNextShifted;
268 isNextShifted = false;
270 // Save off last code
273 // Decode another code from image
274 code = decodeCode(row, counters, sizeof(counters)/sizeof(int), nextStart);
276 // Remember whether the last code was printable or not (excluding CODE_STOP)
277 if (code != CODE_STOP) {
278 lastCharacterWasPrintable = true;
281 // Add to checksum computation (if not CODE_STOP of course)
282 if (code != CODE_STOP) {
284 checksumTotal += multiplier * code;
287 // Advance to where the next code will to start
288 lastStart = nextStart;
289 int _countersLenght = sizeof(counters) / sizeof(int);
290 for (int i = 0; i < _countersLenght; i++) {
291 nextStart += counters[i];
294 // Take care of illegal start codes
299 throw ReaderException("");
306 tmpResultString.append(1, (char) (' ' + code));
307 } else if (code < 96) {
308 tmpResultString.append(1, (char) (code - 64));
310 // Don't let CODE_STOP, which always appears, affect whether whether we think the last
311 // code was printable or not.
312 if (code != CODE_STOP) {
313 lastCharacterWasPrintable = false;
323 isNextShifted = true;
324 codeSet = CODE_CODE_B;
327 codeSet = CODE_CODE_B;
330 codeSet = CODE_CODE_C;
340 tmpResultString.append(1, (char) (' ' + code));
342 if (code != CODE_STOP) {
343 lastCharacterWasPrintable = false;
353 isNextShifted = true;
354 codeSet = CODE_CODE_C;
357 codeSet = CODE_CODE_A;
360 codeSet = CODE_CODE_C;
371 tmpResultString.append(1, '0');
373 tmpResultString.append(1, code);
375 if (code != CODE_STOP) {
376 lastCharacterWasPrintable = false;
383 codeSet = CODE_CODE_A;
386 codeSet = CODE_CODE_B;
396 // Unshift back to another code set if we were shifted
400 codeSet = CODE_CODE_C;
403 codeSet = CODE_CODE_A;
406 codeSet = CODE_CODE_B;
413 // Check for ample whitespace following pattern, but, to do this we first need to remember that
414 // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
415 // to read off. Would be slightly better to properly read. Here we just skip it:
416 int width = row->getSize();
417 while (nextStart < width && row->get(nextStart)) {
420 if (!row->isRange(nextStart, fminl(width, nextStart + (nextStart - lastStart) / 2), false)) {
421 throw ReaderException("");
424 // Pull out from sum the value of the penultimate check code
425 checksumTotal -= multiplier * lastCode;
426 // lastCode is the checksum then:
427 if (checksumTotal % 103 != lastCode) {
428 throw ReaderException("");
431 // Need to pull out the check digits from string
432 int resultLength = tmpResultString.length();
433 // Only bother if the result had at least one character, and if the checksum digit happened to
434 // be a printable character. If it was just interpreted as a control code, nothing to remove.
435 if (resultLength > 0 && lastCharacterWasPrintable) {
436 if (codeSet == CODE_CODE_C) {
437 tmpResultString.erase(resultLength - 2, resultLength);
439 tmpResultString.erase(resultLength - 1, resultLength);
443 Ref<String> resultString(new String(tmpResultString));
444 // String resultString(tmpResultString);
446 if (tmpResultString.length() == 0) {
447 // Almost surely a false positive
448 throw ReaderException("");
451 float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
452 float right = (float) (nextStart + lastStart) / 2.0f;
454 std::vector< Ref<ResultPoint> > resultPoints(2);
455 Ref<OneDResultPoint> resultPoint1(new OneDResultPoint(left, (float) rowNumber));
456 Ref<OneDResultPoint> resultPoint2(new OneDResultPoint(right, (float) rowNumber));
457 resultPoints[0] = resultPoint1;
458 resultPoints[1] = resultPoint2;
460 ArrayRef<unsigned char> resultBytes(1);
462 delete [] startPatternInfo;
463 startPatternInfo = NULL;
465 Ref<Result> res(new Result(resultString, resultBytes, resultPoints, BarcodeFormat_CODE_128));
469 void Code128Reader::append(char* s, char c){
475 Code128Reader::~Code128Reader(){