From 1b9d5451eded4badf508e071ef4cb54d67e7a398 Mon Sep 17 00:00:00 2001 From: Christoph Oberhofer Date: Mon, 18 Apr 2016 23:50:44 +0200 Subject: [PATCH] Reorganizing functions/methods --- src/reader/barcode_reader.js | 5 ++--- src/reader/code_128_reader.js | 36 +++++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/reader/barcode_reader.js b/src/reader/barcode_reader.js index c3fc411..d834190 100644 --- a/src/reader/barcode_reader.js +++ b/src/reader/barcode_reader.js @@ -58,8 +58,7 @@ BarcodeReader.prototype._normalize = function(counter, correction) { modulo = self.MODULO; if (correction) { - correct(counter, correction.bar, [0, 2, 4]); - correct(counter, correction.space, [1, 3, 5]); + self._correct(counter, correction); } for (i = 0; i < counter.length; i++) { sum += counter[i]; @@ -72,7 +71,7 @@ BarcodeReader.prototype._normalize = function(counter, correction) { return normalized; }; -function correct(counter, correction, indices) { +BarcodeReader.prototype._correctBars = function(counter, correction, indices) { var length = indices.length, tmp = 0; while(length--) { diff --git a/src/reader/code_128_reader.js b/src/reader/code_128_reader.js index 5c7607e..d17c372 100644 --- a/src/reader/code_128_reader.js +++ b/src/reader/code_128_reader.js @@ -125,7 +125,8 @@ var properties = { ]}, SINGLE_CODE_ERROR: {value: 0.64}, AVG_CODE_ERROR: {value: 0.30}, - FORMAT: {value: "code_128", writeable: false} + FORMAT: {value: "code_128", writeable: false}, + MODULE_INDICES: {value: {bar: [0, 2, 4], space: [1, 3, 5]}} }; Code128Reader.prototype = Object.create(BarcodeReader.prototype, properties); @@ -172,9 +173,11 @@ Code128Reader.prototype._decodeCode = function(start, correction) { } if (self.CODE_PATTERN[bestMatch.code]) { bestMatch.correction.bar = calculateCorrection( - self.CODE_PATTERN[bestMatch.code], normalized, [0, 2, 4]); + self.CODE_PATTERN[bestMatch.code], normalized, + this.MODULE_INDICES.bar); bestMatch.correction.space = calculateCorrection( - self.CODE_PATTERN[bestMatch.code], normalized, [1, 3, 5]); + self.CODE_PATTERN[bestMatch.code], normalized, + this.MODULE_INDICES.space); } return bestMatch; } @@ -188,17 +191,10 @@ Code128Reader.prototype._decodeCode = function(start, correction) { return null; }; -function calculateCorrection(expected, normalized, indices) { - var length = indices.length, - sumNormalized = 0, - sumExpected = 0; - - while(length--) { - sumExpected += expected[indices[length]]; - sumNormalized += normalized[indices[length]]; - } - return sumExpected/sumNormalized; -} +Code128Reader.prototype._correct = function(counter, correction) { + this._correctBars(counter, correction.bar, this.MODULE_INDICES.bar); + this._correctBars(counter, correction.space, this.MODULE_INDICES.space); +}; Code128Reader.prototype._findStart = function() { var counter = [0, 0, 0, 0, 0, 0], @@ -456,4 +452,16 @@ BarcodeReader.prototype._verifyTrailingWhitespace = function(endInfo) { return null; }; +function calculateCorrection(expected, normalized, indices) { + var length = indices.length, + sumNormalized = 0, + sumExpected = 0; + + while(length--) { + sumExpected += expected[indices[length]]; + sumNormalized += normalized[indices[length]]; + } + return sumExpected/sumNormalized; +} + export default Code128Reader;