Fixed Code128 Reader

feature/71
Christoph Oberhofer 9 years ago
parent dadb8dbc9f
commit 8fa0c69bc1

@ -66,33 +66,6 @@ BarcodeReader.prototype._nextSet = function(line, offset) {
return line.length; return line.length;
}; };
BarcodeReader.prototype._normalize = function(counter, correction) {
var i,
self = this,
sum = 0,
ratio,
numOnes = 0,
normalized = new Array(counter.length),
norm = 0,
modulo = self.MODULO;
for (i = 0; i < normalized.length; i++) {
normalized[i] = counter[i] < this.minBarWidth ? counter[i] = this.minBarWidth : counter[i];
}
if (correction) {
self._correct(normalized, correction);
}
for (i = 0; i < normalized.length; i++) {
sum += normalized[i];
}
ratio = sum / (modulo - numOnes);
for (i = 0; i < normalized.length; i++) {
norm = normalized[i] === 1 ? normalized[i] : normalized[i] / ratio;
normalized[i] = norm;
}
return normalized;
};
BarcodeReader.prototype._correctBars = function(counter, correction, indices) { BarcodeReader.prototype._correctBars = function(counter, correction, indices) {
var length = indices.length, var length = indices.length,
tmp = 0; tmp = 0;

@ -13,7 +13,6 @@ var properties = {
START_CODE_B: {value: 104}, START_CODE_B: {value: 104},
START_CODE_C: {value: 105}, START_CODE_C: {value: 105},
STOP_CODE: {value: 106}, STOP_CODE: {value: 106},
MODULO: {value: 11},
CODE_PATTERN: {value: [ CODE_PATTERN: {value: [
[2, 1, 2, 2, 2, 2], [2, 1, 2, 2, 2, 2],
[2, 2, 2, 1, 2, 2], [2, 2, 2, 1, 2, 2],
@ -150,37 +149,36 @@ Code128Reader.prototype._decodeCode = function(start, correction) {
} }
}, },
code, code,
error, error;
normalized;
for ( i = offset; i < self._row.length; i++) { for ( i = offset; i < self._row.length; i++) {
if (self._row[i] ^ isWhite) { if (self._row[i] ^ isWhite) {
counter[counterPos]++; counter[counterPos]++;
} else { } else {
if (counterPos === counter.length - 1) { if (counterPos === counter.length - 1) {
normalized = self._normalize(counter, correction); if (correction) {
if (normalized) { self._correct(counter, correction);
for (code = 0; code < self.CODE_PATTERN.length; code++) { }
error = self._matchPattern(normalized, self.CODE_PATTERN[code]); for (code = 0; code < self.CODE_PATTERN.length; code++) {
if (error < bestMatch.error) { error = self._matchPattern(counter, self.CODE_PATTERN[code]);
bestMatch.code = code; if (error < bestMatch.error) {
bestMatch.error = error; bestMatch.code = code;
} bestMatch.error = error;
}
bestMatch.end = i;
if (bestMatch.code === -1 || bestMatch.error > self.AVG_CODE_ERROR) {
return null;
}
if (self.CODE_PATTERN[bestMatch.code]) {
bestMatch.correction.bar = calculateCorrection(
self.CODE_PATTERN[bestMatch.code], normalized,
this.MODULE_INDICES.bar);
bestMatch.correction.space = calculateCorrection(
self.CODE_PATTERN[bestMatch.code], normalized,
this.MODULE_INDICES.space);
} }
return bestMatch;
} }
bestMatch.end = i;
if (bestMatch.code === -1 || bestMatch.error > self.AVG_CODE_ERROR) {
return null;
}
if (self.CODE_PATTERN[bestMatch.code]) {
bestMatch.correction.bar = calculateCorrection(
self.CODE_PATTERN[bestMatch.code], counter,
this.MODULE_INDICES.bar);
bestMatch.correction.space = calculateCorrection(
self.CODE_PATTERN[bestMatch.code], counter,
this.MODULE_INDICES.space);
}
return bestMatch;
} else { } else {
counterPos++; counterPos++;
} }
@ -216,8 +214,7 @@ Code128Reader.prototype._findStart = function() {
code, code,
error, error,
j, j,
sum, sum;
normalized;
for ( i = offset; i < self._row.length; i++) { for ( i = offset; i < self._row.length; i++) {
if (self._row[i] ^ isWhite) { if (self._row[i] ^ isWhite) {
@ -228,27 +225,24 @@ Code128Reader.prototype._findStart = function() {
for ( j = 0; j < counter.length; j++) { for ( j = 0; j < counter.length; j++) {
sum += counter[j]; sum += counter[j];
} }
normalized = self._normalize(counter); for (code = self.START_CODE_A; code <= self.START_CODE_C; code++) {
if (normalized) { error = self._matchPattern(counter, self.CODE_PATTERN[code]);
for (code = self.START_CODE_A; code <= self.START_CODE_C; code++) { if (error < bestMatch.error) {
error = self._matchPattern(normalized, self.CODE_PATTERN[code]); bestMatch.code = code;
if (error < bestMatch.error) { bestMatch.error = error;
bestMatch.code = code;
bestMatch.error = error;
}
}
if (bestMatch.error < self.AVG_CODE_ERROR) {
bestMatch.start = i - sum;
bestMatch.end = i;
bestMatch.correction.bar = calculateCorrection(
self.CODE_PATTERN[bestMatch.code], normalized,
this.MODULE_INDICES.bar);
bestMatch.correction.space = calculateCorrection(
self.CODE_PATTERN[bestMatch.code], normalized,
this.MODULE_INDICES.space);
return bestMatch;
} }
} }
if (bestMatch.error < self.AVG_CODE_ERROR) {
bestMatch.start = i - sum;
bestMatch.end = i;
bestMatch.correction.bar = calculateCorrection(
self.CODE_PATTERN[bestMatch.code], counter,
this.MODULE_INDICES.bar);
bestMatch.correction.space = calculateCorrection(
self.CODE_PATTERN[bestMatch.code], counter,
this.MODULE_INDICES.space);
return bestMatch;
}
for ( j = 0; j < 4; j++) { for ( j = 0; j < 4; j++) {
counter[j] = counter[j + 2]; counter[j] = counter[j + 2];

@ -17,7 +17,6 @@ function getDefaulConfig() {
var properties = { var properties = {
CODE_L_START: {value: 0}, CODE_L_START: {value: 0},
MODULO: {value: 7},
CODE_G_START: {value: 10}, CODE_G_START: {value: 10},
START_PATTERN: {value: [1, 1, 1]}, START_PATTERN: {value: [1, 1, 1]},
STOP_PATTERN: {value: [1, 1, 1]}, STOP_PATTERN: {value: [1, 1, 1]},

@ -23,9 +23,8 @@ function getDefaulConfig() {
var N = 1, var N = 1,
W = 3, W = 3,
properties = { properties = {
MODULO: {value: 10}, START_PATTERN: {value: [N, N, N, N]},
START_PATTERN: {value: [N * 2.5, N * 2.5, N * 2.5, N * 2.5]}, STOP_PATTERN: {value: [N, N, W]},
STOP_PATTERN: {value: [N * 2, N * 2, W * 2]},
CODE_PATTERN: {value: [ CODE_PATTERN: {value: [
[N, N, W, W, N], [N, N, W, W, N],
[W, N, N, N, W], [W, N, N, N, W],
@ -110,16 +109,12 @@ I2of5Reader.prototype._findPattern = function(pattern, offset, isWhite, tryHarde
for ( j = 0; j < counter.length; j++) { for ( j = 0; j < counter.length; j++) {
sum += counter[j]; sum += counter[j];
} }
normalized = self._normalize(counter); error = self._matchPattern(counter, pattern);
if (normalized) { if (error < epsilon) {
error = self._matchPattern(normalized, pattern); bestMatch.error = error;
bestMatch.start = i - sum;
if (error < epsilon) { bestMatch.end = i;
bestMatch.error = error; return bestMatch;
bestMatch.start = i - sum;
bestMatch.end = i;
return bestMatch;
}
} }
if (tryHarder) { if (tryHarder) {
for (j = 0; j < counter.length - 2; j++) { for (j = 0; j < counter.length - 2; j++) {
@ -233,20 +228,16 @@ I2of5Reader.prototype._decodeCode = function(counter) {
for ( j = 0; j < counter.length; j++) { for ( j = 0; j < counter.length; j++) {
sum += counter[j]; sum += counter[j];
} }
normalized = self._normalize(counter); for (code = 0; code < self.CODE_PATTERN.length; code++) {
if (normalized) { error = self._matchPattern(counter, self.CODE_PATTERN[code]);
for (code = 0; code < self.CODE_PATTERN.length; code++) { if (error < bestMatch.error) {
error = self._matchPattern(normalized, self.CODE_PATTERN[code]); bestMatch.code = code;
if (error < bestMatch.error) { bestMatch.error = error;
bestMatch.code = code;
bestMatch.error = error;
}
}
if (bestMatch.error < epsilon) {
return bestMatch;
} }
} }
return null; if (bestMatch.error < epsilon) {
return bestMatch;
}
}; };
I2of5Reader.prototype._decodePayload = function(counters, result, decodedCodes) { I2of5Reader.prototype._decodePayload = function(counters, result, decodedCodes) {

Loading…
Cancel
Save