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>
32 const int CODE_PATTERNS_LENGTH = 107;
33 const int countersLength = 6;
34 static const int CODE_PATTERNS[CODE_PATTERNS_LENGTH][countersLength] = {
35 {2, 1, 2, 2, 2, 2}, /* 0 */
40 {1, 3, 1, 2, 2, 2}, /* 5 */
45 {2, 2, 1, 3, 1, 2}, /* 10 */
50 {1, 1, 3, 2, 2, 2}, /* 15 */
55 {2, 2, 1, 2, 3, 1}, /* 20 */
60 {3, 2, 1, 1, 2, 2}, /* 25 */
65 {2, 1, 2, 1, 2, 3}, /* 30 */
70 {1, 3, 1, 3, 2, 1}, /* 35 */
75 {2, 3, 1, 1, 1, 3}, /* 40 */
80 {1, 1, 3, 1, 2, 3}, /* 45 */
85 {2, 3, 1, 1, 3, 1}, /* 50 */
90 {3, 1, 1, 3, 2, 1}, /* 55 */
95 {3, 1, 4, 1, 1, 1}, /* 60 */
100 {1, 2, 1, 1, 2, 4}, /* 65 */
105 {1, 1, 2, 4, 1, 2}, /* 70 */
110 {2, 4, 1, 2, 1, 1}, /* 75 */
115 {1, 1, 1, 2, 4, 2}, /* 80 */
120 {1, 2, 4, 2, 1, 1}, /* 85 */
125 {2, 1, 4, 1, 2, 1}, /* 90 */
130 {1, 1, 4, 1, 1, 3}, /* 95 */
135 {1, 1, 4, 1, 3, 1}, /* 100 */
140 {2, 1, 1, 2, 3, 2}, /* 105 */
145 Code128Reader::Code128Reader(){
148 int* Code128Reader::findStartPattern(Ref<BitArray> row){
149 int width = row->getSize();
151 while (rowOffset < width) {
152 if (row->get(rowOffset)) {
158 int counterPosition = 0;
159 int counters[countersLength] = {0,0,0,0,0,0};
160 int patternStart = rowOffset;
161 bool isWhite = false;
162 int patternLength = sizeof(counters) / sizeof(int);
164 for (int i = rowOffset; i < width; i++) {
165 bool pixel = row->get(i);
166 if (pixel ^ isWhite) {
167 counters[counterPosition]++;
169 if (counterPosition == patternLength - 1) {
170 unsigned int bestVariance = MAX_AVG_VARIANCE;
172 for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
173 unsigned int variance = patternMatchVariance(counters, sizeof(counters)/sizeof(int), CODE_PATTERNS[startCode], MAX_INDIVIDUAL_VARIANCE);
174 if (variance < bestVariance) {
175 bestVariance = variance;
176 bestMatch = startCode;
179 if (bestMatch >= 0) {
180 // Look for whitespace before start pattern, >= 50% of width of start pattern
181 if (row->isRange(fmaxl(0, patternStart - (i - patternStart) / 2), patternStart, false)) {
182 int* resultValue = new int[3];
183 resultValue[0] = patternStart;
185 resultValue[2] = bestMatch;
189 patternStart += counters[0] + counters[1];
190 for (int y = 2; y < patternLength; y++) {
191 counters[y - 2] = counters[y];
193 counters[patternLength - 2] = 0;
194 counters[patternLength - 1] = 0;
199 counters[counterPosition] = 1;
203 throw ReaderException("");
206 int Code128Reader::decodeCode(Ref<BitArray> row, int counters[], int countersCount, int rowOffset){
207 recordPattern(row, rowOffset, counters, countersCount);
208 unsigned int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
210 for (int d = 0; d < CODE_PATTERNS_LENGTH; d++) {
211 int pattern[countersLength];
213 for(int ind = 0; ind< countersLength; ind++){
214 pattern[ind] = CODE_PATTERNS[d][ind];
216 // memcpy(pattern, CODE_PATTERNS[d], countersLength);
217 unsigned int variance = patternMatchVariance(counters, countersCount, pattern, MAX_INDIVIDUAL_VARIANCE);
218 if (variance < bestVariance) {
219 bestVariance = variance;
223 // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6.
224 if (bestMatch >= 0) {
227 throw ReaderException("");
231 Ref<Result> Code128Reader::decodeRow(int rowNumber, Ref<BitArray> row){
233 int* startPatternInfo = findStartPattern(row);
234 int startCode = startPatternInfo[2];
238 codeSet = CODE_CODE_A;
241 codeSet = CODE_CODE_B;
244 codeSet = CODE_CODE_C;
247 delete [] startPatternInfo;
248 throw ReaderException("");
252 bool isNextShifted = false;
254 std::string tmpResultString;
255 std::stringstream tmpResultSStr; // used if its Code 128C
257 int lastStart = startPatternInfo[0];
258 int nextStart = startPatternInfo[1];
259 int counters[countersLength] = {0,0,0,0,0,0};
263 int checksumTotal = startCode;
265 bool lastCharacterWasPrintable = true;
269 bool unshift = isNextShifted;
270 isNextShifted = false;
272 // Save off last code
275 // Decode another code from image
277 code = decodeCode(row, counters, sizeof(counters)/sizeof(int), nextStart);
278 } catch (ReaderException re) {
279 delete [] startPatternInfo;
283 // Remember whether the last code was printable or not (excluding CODE_STOP)
284 if (code != CODE_STOP) {
285 lastCharacterWasPrintable = true;
288 // Add to checksum computation (if not CODE_STOP of course)
289 if (code != CODE_STOP) {
291 checksumTotal += multiplier * code;
294 // Advance to where the next code will to start
295 lastStart = nextStart;
296 int _countersLength = sizeof(counters) / sizeof(int);
297 for (int i = 0; i < _countersLength; i++) {
298 nextStart += counters[i];
301 // Take care of illegal start codes
306 delete [] startPatternInfo;
307 throw ReaderException("");
314 tmpResultString.append(1, (char) (' ' + code));
315 } else if (code < 96) {
316 tmpResultString.append(1, (char) (code - 64));
318 // Don't let CODE_STOP, which always appears, affect whether whether we think the last
319 // code was printable or not.
320 if (code != CODE_STOP) {
321 lastCharacterWasPrintable = false;
331 isNextShifted = true;
332 codeSet = CODE_CODE_B;
335 codeSet = CODE_CODE_B;
338 codeSet = CODE_CODE_C;
348 tmpResultString.append(1, (char) (' ' + code));
350 if (code != CODE_STOP) {
351 lastCharacterWasPrintable = false;
361 isNextShifted = true;
362 codeSet = CODE_CODE_C;
365 codeSet = CODE_CODE_A;
368 codeSet = CODE_CODE_C;
377 // the code read in this case is the number encoded directly
380 tmpResultSStr << '0';
381 tmpResultSStr << code;
383 if (code != CODE_STOP) {
384 lastCharacterWasPrintable = false;
391 codeSet = CODE_CODE_A;
394 codeSet = CODE_CODE_B;
404 // Unshift back to another code set if we were shifted
408 codeSet = CODE_CODE_C;
411 codeSet = CODE_CODE_A;
414 codeSet = CODE_CODE_B;
421 // Check for ample whitespace following pattern, but, to do this we first need to remember that
422 // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
423 // to read off. Would be slightly better to properly read. Here we just skip it:
424 int width = row->getSize();
425 while (nextStart < width && row->get(nextStart)) {
428 if (!row->isRange(nextStart, fminl(width, nextStart + (nextStart - lastStart) / 2), false)) {
429 delete [] startPatternInfo;
430 throw ReaderException("");
433 // Pull out from sum the value of the penultimate check code
434 checksumTotal -= multiplier * lastCode;
435 // lastCode is the checksum then:
436 if (checksumTotal % 103 != lastCode) {
437 delete [] startPatternInfo;
438 throw ReaderException("");
441 if (codeSet == CODE_CODE_C)
442 tmpResultString.append(tmpResultSStr.str());
444 // Need to pull out the check digits from string
445 int resultLength = tmpResultString.length();
446 // Only bother if the result had at least one character, and if the checksum digit happened to
447 // be a printable character. If it was just interpreted as a control code, nothing to remove.
448 if (resultLength > 0 && lastCharacterWasPrintable) {
449 if (codeSet == CODE_CODE_C) {
450 tmpResultString.erase(resultLength - 2, resultLength);
452 tmpResultString.erase(resultLength - 1, resultLength);
456 Ref<String> resultString(new String(tmpResultString));
457 // String resultString(tmpResultString);
459 if (tmpResultString.length() == 0) {
460 delete [] startPatternInfo;
461 // Almost surely a false positive
462 throw ReaderException("");
465 float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
466 float right = (float) (nextStart + lastStart) / 2.0f;
468 std::vector< Ref<ResultPoint> > resultPoints(2);
469 Ref<OneDResultPoint> resultPoint1(new OneDResultPoint(left, (float) rowNumber));
470 Ref<OneDResultPoint> resultPoint2(new OneDResultPoint(right, (float) rowNumber));
471 resultPoints[0] = resultPoint1;
472 resultPoints[1] = resultPoint2;
474 ArrayRef<unsigned char> resultBytes(1);
476 delete [] startPatternInfo;
477 startPatternInfo = NULL;
479 Ref<Result> res(new Result(resultString, resultBytes, resultPoints, BarcodeFormat_CODE_128));
483 void Code128Reader::append(char* s, char c){
489 Code128Reader::~Code128Reader(){