5 * Copyright 2010 ZXing authors All rights reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include "Code128Reader.h"
21 #include <zxing/oned/OneDResultPoint.h>
22 #include <zxing/common/Array.h>
23 #include <zxing/ReaderException.h>
31 const int CODE_PATTERNS_LENGTH = 107;
32 const int countersLength = 6;
33 static const int CODE_PATTERNS[CODE_PATTERNS_LENGTH][countersLength] = {
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[countersLength] = {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 unsigned int bestVariance = MAX_AVG_VARIANCE;
171 for (int startCode = CODE_START_A; startCode <= CODE_START_C; startCode++) {
172 unsigned int variance = patternMatchVariance(counters, sizeof(counters) / sizeof(int),
173 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,
183 int* resultValue = new int[3];
184 resultValue[0] = patternStart;
186 resultValue[2] = bestMatch;
190 patternStart += counters[0] + counters[1];
191 for (int y = 2; y < patternLength; y++) {
192 counters[y - 2] = counters[y];
194 counters[patternLength - 2] = 0;
195 counters[patternLength - 1] = 0;
200 counters[counterPosition] = 1;
204 throw ReaderException("");
207 int Code128Reader::decodeCode(Ref<BitArray> row, int counters[], int countersCount,
209 if (!recordPattern(row, rowOffset, counters, countersCount)) {
210 throw ReaderException("");
212 unsigned int bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
214 for (int d = 0; d < CODE_PATTERNS_LENGTH; d++) {
215 int pattern[countersLength];
217 for(int ind = 0; ind< countersLength; ind++){
218 pattern[ind] = CODE_PATTERNS[d][ind];
220 // memcpy(pattern, CODE_PATTERNS[d], countersLength);
221 unsigned int variance = patternMatchVariance(counters, countersCount, pattern,
222 MAX_INDIVIDUAL_VARIANCE);
223 if (variance < bestVariance) {
224 bestVariance = variance;
228 // TODO We're overlooking the fact that the STOP pattern has 7 values, not 6.
229 if (bestMatch >= 0) {
232 throw ReaderException("");
236 Ref<Result> Code128Reader::decodeRow(int rowNumber, Ref<BitArray> row) {
237 int* startPatternInfo = NULL;
239 startPatternInfo = findStartPattern(row);
240 int startCode = startPatternInfo[2];
244 codeSet = CODE_CODE_A;
247 codeSet = CODE_CODE_B;
250 codeSet = CODE_CODE_C;
253 throw ReaderException("");
257 bool isNextShifted = false;
259 std::string tmpResultString;
260 std::stringstream tmpResultSStr; // used if its Code 128C
262 int lastStart = startPatternInfo[0];
263 int nextStart = startPatternInfo[1];
264 int counters[countersLength] = {0,0,0,0,0,0};
268 int checksumTotal = startCode;
270 bool lastCharacterWasPrintable = true;
273 bool unshift = isNextShifted;
274 isNextShifted = false;
276 // Save off last code
279 // Decode another code from image
281 code = decodeCode(row, counters, sizeof(counters)/sizeof(int), nextStart);
282 } catch (ReaderException re) {
286 // Remember whether the last code was printable or not (excluding CODE_STOP)
287 if (code != CODE_STOP) {
288 lastCharacterWasPrintable = true;
291 // Add to checksum computation (if not CODE_STOP of course)
292 if (code != CODE_STOP) {
294 checksumTotal += multiplier * code;
297 // Advance to where the next code will to start
298 lastStart = nextStart;
299 int _countersLength = sizeof(counters) / sizeof(int);
300 for (int i = 0; i < _countersLength; i++) {
301 nextStart += counters[i];
304 // Take care of illegal start codes
309 throw ReaderException("");
316 tmpResultString.append(1, (char) (' ' + code));
317 } else if (code < 96) {
318 tmpResultString.append(1, (char) (code - 64));
320 // Don't let CODE_STOP, which always appears, affect whether whether we think the
321 // last code was printable or not.
322 if (code != CODE_STOP) {
323 lastCharacterWasPrintable = false;
333 isNextShifted = true;
334 codeSet = CODE_CODE_B;
337 codeSet = CODE_CODE_B;
340 codeSet = CODE_CODE_C;
350 tmpResultString.append(1, (char) (' ' + code));
352 if (code != CODE_STOP) {
353 lastCharacterWasPrintable = false;
363 isNextShifted = true;
364 codeSet = CODE_CODE_C;
367 codeSet = CODE_CODE_A;
370 codeSet = CODE_CODE_C;
379 // the code read in this case is the number encoded directly
382 tmpResultSStr << '0';
383 tmpResultSStr << code;
385 if (code != CODE_STOP) {
386 lastCharacterWasPrintable = false;
393 codeSet = CODE_CODE_A;
396 codeSet = CODE_CODE_B;
406 // Unshift back to another code set if we were shifted
410 codeSet = CODE_CODE_C;
413 codeSet = CODE_CODE_A;
416 codeSet = CODE_CODE_B;
423 // Check for ample whitespace following pattern, but, to do this we first need to remember that
424 // we fudged decoding CODE_STOP since it actually has 7 bars, not 6. There is a black bar left
425 // to read off. Would be slightly better to properly read. Here we just skip it:
426 int width = row->getSize();
427 while (nextStart < width && row->get(nextStart)) {
430 if (!row->isRange(nextStart, fminl(width, nextStart + (nextStart - lastStart) / 2), false)) {
431 throw ReaderException("");
434 // Pull out from sum the value of the penultimate check code
435 checksumTotal -= multiplier * lastCode;
436 // lastCode is the checksum then:
437 if (checksumTotal % 103 != lastCode) {
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 if (tmpResultString.length() == 0) {
458 // Almost surely a false positive
459 throw ReaderException("");
462 float left = (float) (startPatternInfo[1] + startPatternInfo[0]) / 2.0f;
463 float right = (float) (nextStart + lastStart) / 2.0f;
465 std::vector< Ref<ResultPoint> > resultPoints(2);
466 Ref<OneDResultPoint> resultPoint1(new OneDResultPoint(left, (float) rowNumber));
467 Ref<OneDResultPoint> resultPoint2(new OneDResultPoint(right, (float) rowNumber));
468 resultPoints[0] = resultPoint1;
469 resultPoints[1] = resultPoint2;
471 delete [] startPatternInfo;
472 ArrayRef<unsigned char> resultBytes(1);
473 return Ref<Result>(new Result(resultString, resultBytes, resultPoints,
474 BarcodeFormat_CODE_128));
475 } catch (ReaderException const& re) {
476 delete [] startPatternInfo;
477 return Ref<Result>();
481 void Code128Reader::append(char* s, char c){
487 Code128Reader::~Code128Reader(){