From 5f68c6c69a9d0563c73ca33be804592bb558f8c9 Mon Sep 17 00:00:00 2001 From: Christoph Oberhofer Date: Sun, 26 Jul 2015 13:40:19 +0200 Subject: [PATCH] Experimenting on i2of5 reader regarding different weights of bars and spaces --- src/barcode_decoder.js | 20 ++++++++--- src/barcode_reader.js | 5 ++- src/i2of5_reader.js | 78 +++++++++++++++++++++++++----------------- 3 files changed, 66 insertions(+), 37 deletions(-) diff --git a/src/barcode_decoder.js b/src/barcode_decoder.js index e14f897..b4a03e4 100644 --- a/src/barcode_decoder.js +++ b/src/barcode_decoder.js @@ -89,11 +89,21 @@ define([ } function initReaders() { - var i; - for ( i = 0; i < config.readers.length; i++) { - console.log(config.readers[i]); - _barcodeReaders.push(new readers[config.readers[i]]()); - } + config.readers.forEach(function(readerConfig) { + var reader, + config = {}; + + if (typeof readerConfig === 'object') { + reader = readerConfig.format; + config = readerConfig.config; + } else if (typeof readerConfig === 'string') { + reader = readerConfig; + } + _barcodeReaders.push(new readers[reader](config)); + }); + console.log("Registered Readers:" + _barcodeReaders + .map(function(reader) {return reader.FORMAT;}) + .join(', ')); } function initConfig() { diff --git a/src/barcode_reader.js b/src/barcode_reader.js index 6670d26..ec2f5f8 100644 --- a/src/barcode_reader.js +++ b/src/barcode_reader.js @@ -5,8 +5,9 @@ define( function() { "use strict"; - function BarcodeReader() { + function BarcodeReader(config) { this._row = []; + this.config = config || {}; return this; } @@ -222,6 +223,8 @@ define( CodeNotFoundException : "Code could not be found!", PatternNotFoundException : "Pattern could not be found!" }; + + BarcodeReader.CONFIG_KEYS = {}; return (BarcodeReader); } diff --git a/src/i2of5_reader.js b/src/i2of5_reader.js index c866af2..f3e22c0 100644 --- a/src/i2of5_reader.js +++ b/src/i2of5_reader.js @@ -10,6 +10,7 @@ define( function I2of5Reader(opts) { BarcodeReader.call(this, opts); + this.barSpaceRatio = [1, 1]; } var N = 1, @@ -30,29 +31,37 @@ define( [W, N, N, W, N], [N, W, N, W, N] ]}, - SINGLE_CODE_ERROR: {value: 1}, + SINGLE_CODE_ERROR: {value: 0.7}, AVG_CODE_ERROR: {value: 0.38}, + MAX_CORRECTION_FACTOR: {value: 2}, FORMAT: {value: "i2of5", writeable: false} }; I2of5Reader.prototype = Object.create(BarcodeReader.prototype, properties); - I2of5Reader.prototype.constructor = I2of5Reader; + I2of5Reader.prototype.consconstructor = I2of5Reader; I2of5Reader.prototype._matchPattern = function(counter, code) { - var i, - counterSum = [0, 0], - codeSum = [0, 0], - correction = [0, 0]; - - for (i = 0; i < counter.length; i++) { - counterSum[i % 2] += counter[i]; - codeSum[i % 2] += code[i] - } - correction[0] = codeSum[0]/counterSum[0]; - correction[1] = codeSum[1]/counterSum[1]; - - for (i = 0; i < counter.length; i++) { - counter[i] *= correction[i % 2]; + if (this.config.normalizeBarSpaceWidth) { + var i, + counterSum = [0, 0], + codeSum = [0, 0], + correction = [0, 0], + correctionRatio = this.MAX_CORRECTION_FACTOR, + correctionRatioInverse = 1 / correctionRatio; + + for (i = 0; i < counter.length; i++) { + counterSum[i % 2] += counter[i]; + codeSum[i % 2] += code[i] + } + correction[0] = codeSum[0] / counterSum[0]; + correction[1] = codeSum[1] / counterSum[1]; + + correction[0] = Math.max(Math.min(correction[0], correctionRatio), correctionRatioInverse); + correction[1] = Math.max(Math.min(correction[1], correctionRatio), correctionRatioInverse); + this.barSpaceRatio = correction; + for (i = 0; i < counter.length; i++) { + counter[i] *= this.barSpaceRatio[i % 2]; + } } return BarcodeReader.prototype._matchPattern.call(this, counter, code); }; @@ -129,17 +138,18 @@ define( var self = this, leadingWhitespaceStart, offset = self._nextSet(self._row), - startInfo; + startInfo, + narrowBarWidth = 1; while(!startInfo) { startInfo = self._findPattern(self.START_PATTERN, offset, false, true); if (!startInfo) { return null; } - leadingWhitespaceStart = startInfo.start - (startInfo.end - startInfo.start); + narrowBarWidth = Math.floor((startInfo.end - startInfo.start) / 4); + leadingWhitespaceStart = startInfo.start - narrowBarWidth*5; if (leadingWhitespaceStart >= 0) { if (self._matchRange(leadingWhitespaceStart, startInfo.start, 0)) { - startInfo.narrowBarWidth = Math.floor((startInfo.end - startInfo.start) / 4); return startInfo; } } @@ -242,8 +252,8 @@ define( while (pos < counterLength) { for (i = 0; i < 5; i++) { - counterPair[0][i] = counters[pos]; - counterPair[1][i] = counters[pos + 1]; + counterPair[0][i] = counters[pos]*this.barSpaceRatio[0]; + counterPair[1][i] = counters[pos + 1]*this.barSpaceRatio[1]; pos += 2; } codes = self._decodePair(counterPair); @@ -275,20 +285,12 @@ define( if (!startInfo) { return null; } + decodedCodes.push(startInfo); endInfo = self._findEnd(); if (!endInfo) { return null; } - console.log(startInfo); - console.log(endInfo); - - code = { - code : startInfo.code, - start : startInfo.start, - end : startInfo.end - }; - decodedCodes.push(code); counters = self._fillCounters(startInfo.end, endInfo.start, false); if (!self._verifyCounterLength(counters)) { @@ -298,16 +300,30 @@ define( if (!code) { return null; } + if (result.length % 2 !== 0 || + result.length < 6) { + return null; + } + decodedCodes.push(endInfo); return { code : result.join(""), start : startInfo.start, - end : code.end, + end : endInfo.end, startInfo : startInfo, decodedCodes : decodedCodes }; }; + I2of5Reader.CONFIG_KEYS = { + normalizeBarSpaceWidth: { + 'type': 'boolean', + 'default': false, + 'description': 'If true, the reader tries to normalize the' + + 'width-difference between bars and spaces' + } + }; + return (I2of5Reader); } ); \ No newline at end of file