diff --git a/dist/quagga.js b/dist/quagga.js index b6fa950..3bfb037 100644 --- a/dist/quagga.js +++ b/dist/quagga.js @@ -1240,6 +1240,39 @@ define( throw BarcodeReader.PatternNotFoundException; }; + EANReader.prototype._findStart = function() { + var self = this, + leadingWhitespaceStart, + offset = self._nextSet(self._row), + startInfo; + + while(!startInfo) { + startInfo = self._findPattern(self.START_PATTERN, offset); + leadingWhitespaceStart = startInfo.start - (startInfo.end - startInfo.start); + if (leadingWhitespaceStart >= 0) { + if (self._matchRange(leadingWhitespaceStart, startInfo.start, 0)) { + return startInfo; + } + } + offset = startInfo.end; + startInfo = null; + } + }; + + EANReader.prototype._findEnd = function(offset) { + var self = this, + trailingWhitespaceEnd, + endInfo = self._findPattern(self.STOP_PATTERN, offset); + + trailingWhitespaceEnd = endInfo.end + (endInfo.end - endInfo.start); + if (trailingWhitespaceEnd < self._row.length) { + if (self._matchRange(endInfo.end, trailingWhitespaceEnd, 0)) { + return endInfo; + } + } + return null; + }; + EANReader.prototype._decode = function() { var startInfo, self = this, @@ -1250,7 +1283,7 @@ define( decodedCodes = []; try { - startInfo = self._findPattern(self.START_PATTERN); + startInfo = self._findStart(); code = { code : startInfo.code, start : startInfo.start, @@ -1288,7 +1321,11 @@ define( result.push(code.code); } - code = self._findPattern(self.STOP_PATTERN, code.end); + code = self._findEnd(code.end); + if (code === null){ + return null; + } + decodedCodes.push(code); // Checksum @@ -6384,23 +6421,23 @@ define('bresenham',[],function() { extrema = [], currentDir, dir, - threshold = (max - min) / 8, + threshold = (max - min) / 12, rThreshold = -threshold, i, j; // 1. find extrema - currentDir = line[0] > center ? Slope.DIR.DOWN : Slope.DIR.UP; + currentDir = line[0] > center ? Slope.DIR.UP : Slope.DIR.DOWN; extrema.push({ pos : 0, val : line[0] }); for ( i = 0; i < line.length - 1; i++) { slope = (line[i + 1] - line[i]); - if (slope < rThreshold) { - dir = Slope.DIR.UP; - } else if (slope > threshold) { + if (slope < rThreshold && line[i + 1] < (center*1.5)) { dir = Slope.DIR.DOWN; + } else if (slope > threshold && line[i + 1] > (center*0.5)) { + dir = Slope.DIR.UP; } else { dir = currentDir; } @@ -7090,18 +7127,25 @@ define('barcode_decoder',["bresenham", "image_debug", 'code_128_reader', 'ean_re * @param {Number} angle */ function getExtendedLine(line, angle, ext) { - var extension = { - y : ext * Math.sin(angle), - x : ext * Math.cos(angle) + function extendLine(amount) { + var extension = { + y : amount * Math.sin(angle), + x : amount * Math.cos(angle) }; - - line[0].y -= extension.y; - line[0].x -= extension.x; - line[1].y += extension.y; - line[1].x += extension.x; + + line[0].y -= extension.y; + line[0].x -= extension.x; + line[1].y += extension.y; + line[1].x += extension.x; + } // check if inside image - if (!inputImageWrapper.inImageWithBorder(line[0], 0) || !inputImageWrapper.inImageWithBorder(line[1], 0)) { + extendLine(ext); + while (ext > 1 && !inputImageWrapper.inImageWithBorder(line[0], 0) || !inputImageWrapper.inImageWithBorder(line[1], 0)) { + ext -= Math.floor(ext/2); + extendLine(-ext); + } + if (ext <= 1) { return null; } return line; @@ -7181,6 +7225,12 @@ define('barcode_decoder',["bresenham", "image_debug", 'code_128_reader', 'ean_re return result; } + function getLineLength(line) { + return Math.sqrt( + Math.pow(Math.abs(line[1].y - line[0].y), 2) + + Math.pow(Math.abs(line[1].x - line[0].x), 2)); + } + /** * With the help of the configured readers (Code128 or EAN) this function tries to detect a * valid barcode pattern within the given area. @@ -7191,15 +7241,17 @@ define('barcode_decoder',["bresenham", "image_debug", 'code_128_reader', 'ean_re var line, lineAngle, ctx = _canvas.ctx.overlay, - result; + result, + lineLength; if (config.drawBoundingBox && ctx) { ImageDebug.drawPath(box, {x: 0, y: 1}, ctx, {color: "blue", lineWidth: 2}); } line = getLine(box); + lineLength = getLineLength(line); lineAngle = Math.atan2(line[1].y - line[0].y, line[1].x - line[0].x); - line = getExtendedLine(line, lineAngle, 10); + line = getExtendedLine(line, lineAngle, Math.floor(lineLength*0.07)); if(line === null){ return null; } @@ -7402,7 +7454,7 @@ define('config',[],function(){ debug: false, controls: false, locate: true, - numOfWorkers: 4, + numOfWorkers: 0, visual: { show: true }, diff --git a/example/file_input.html b/example/file_input.html index d01bdb2..429fe27 100644 --- a/example/file_input.html +++ b/example/file_input.html @@ -42,9 +42,9 @@ Barcode-Type - Code 128 + Code 128 Code 39 - EAN + EAN Codabar @@ -52,8 +52,8 @@ Resolution (long side) 320px - 640px - 800px + 640px + 800px 1280px 1600px 1920px @@ -64,8 +64,8 @@ x-small small - medium - large + medium + large x-large @@ -76,8 +76,8 @@ Workers - 0 - 1 + 0 + 1 2 4 8 diff --git a/example/file_input.js b/example/file_input.js index aefe247..201841a 100644 --- a/example/file_input.js +++ b/example/file_input.js @@ -92,15 +92,17 @@ $(function() { }, state: { inputStream: { - size: 800 + size: 640 }, locator: { - patchSize: "medium", + patchSize: "large", halfSample: false }, - numOfWorkers: 1, + numOfWorkers: 0, decoder: { - readers: ["code_128_reader"] + readers: ["ean_reader"], + showFrequency: true, + showPattern: true }, locate: true, src: null diff --git a/example/static_images.html b/example/static_images.html index 05f9358..b4bb5ab 100644 --- a/example/static_images.html +++ b/example/static_images.html @@ -32,13 +32,13 @@ Next Codabar - + Code39 Code128 EAN - + diff --git a/example/static_images.js b/example/static_images.js index 5f4e2c9..d48266d 100644 --- a/example/static_images.js +++ b/example/static_images.js @@ -16,7 +16,7 @@ $(function() { }); }, config: { - reader: "codabar", + reader: "ean", length: 10 }, attachListeners: function() { diff --git a/src/barcode_decoder.js b/src/barcode_decoder.js index da539e7..ff0757e 100644 --- a/src/barcode_decoder.js +++ b/src/barcode_decoder.js @@ -96,18 +96,25 @@ define(["bresenham", "image_debug", 'code_128_reader', 'ean_reader', 'code_39_re * @param {Number} angle */ function getExtendedLine(line, angle, ext) { - var extension = { - y : ext * Math.sin(angle), - x : ext * Math.cos(angle) + function extendLine(amount) { + var extension = { + y : amount * Math.sin(angle), + x : amount * Math.cos(angle) }; - - line[0].y -= extension.y; - line[0].x -= extension.x; - line[1].y += extension.y; - line[1].x += extension.x; + + line[0].y -= extension.y; + line[0].x -= extension.x; + line[1].y += extension.y; + line[1].x += extension.x; + } // check if inside image - if (!inputImageWrapper.inImageWithBorder(line[0], 0) || !inputImageWrapper.inImageWithBorder(line[1], 0)) { + extendLine(ext); + while (ext > 1 && !inputImageWrapper.inImageWithBorder(line[0], 0) || !inputImageWrapper.inImageWithBorder(line[1], 0)) { + ext -= Math.floor(ext/2); + extendLine(-ext); + } + if (ext <= 1) { return null; } return line; @@ -187,6 +194,12 @@ define(["bresenham", "image_debug", 'code_128_reader', 'ean_reader', 'code_39_re return result; } + function getLineLength(line) { + return Math.sqrt( + Math.pow(Math.abs(line[1].y - line[0].y), 2) + + Math.pow(Math.abs(line[1].x - line[0].x), 2)); + } + /** * With the help of the configured readers (Code128 or EAN) this function tries to detect a * valid barcode pattern within the given area. @@ -197,15 +210,17 @@ define(["bresenham", "image_debug", 'code_128_reader', 'ean_reader', 'code_39_re var line, lineAngle, ctx = _canvas.ctx.overlay, - result; + result, + lineLength; if (config.drawBoundingBox && ctx) { ImageDebug.drawPath(box, {x: 0, y: 1}, ctx, {color: "blue", lineWidth: 2}); } line = getLine(box); + lineLength = getLineLength(line); lineAngle = Math.atan2(line[1].y - line[0].y, line[1].x - line[0].x); - line = getExtendedLine(line, lineAngle, 10); + line = getExtendedLine(line, lineAngle, Math.floor(lineLength*0.07)); if(line === null){ return null; } diff --git a/src/bresenham.js b/src/bresenham.js index 1504f4a..14ca314 100644 --- a/src/bresenham.js +++ b/src/bresenham.js @@ -107,23 +107,23 @@ define(function() { extrema = [], currentDir, dir, - threshold = (max - min) / 8, + threshold = (max - min) / 12, rThreshold = -threshold, i, j; // 1. find extrema - currentDir = line[0] > center ? Slope.DIR.DOWN : Slope.DIR.UP; + currentDir = line[0] > center ? Slope.DIR.UP : Slope.DIR.DOWN; extrema.push({ pos : 0, val : line[0] }); for ( i = 0; i < line.length - 1; i++) { slope = (line[i + 1] - line[i]); - if (slope < rThreshold) { - dir = Slope.DIR.UP; - } else if (slope > threshold) { + if (slope < rThreshold && line[i + 1] < (center*1.5)) { dir = Slope.DIR.DOWN; + } else if (slope > threshold && line[i + 1] > (center*0.5)) { + dir = Slope.DIR.UP; } else { dir = currentDir; } diff --git a/src/ean_reader.js b/src/ean_reader.js index dd8f470..20ae675 100644 --- a/src/ean_reader.js +++ b/src/ean_reader.js @@ -167,6 +167,39 @@ define( throw BarcodeReader.PatternNotFoundException; }; + EANReader.prototype._findStart = function() { + var self = this, + leadingWhitespaceStart, + offset = self._nextSet(self._row), + startInfo; + + while(!startInfo) { + startInfo = self._findPattern(self.START_PATTERN, offset); + leadingWhitespaceStart = startInfo.start - (startInfo.end - startInfo.start); + if (leadingWhitespaceStart >= 0) { + if (self._matchRange(leadingWhitespaceStart, startInfo.start, 0)) { + return startInfo; + } + } + offset = startInfo.end; + startInfo = null; + } + }; + + EANReader.prototype._findEnd = function(offset) { + var self = this, + trailingWhitespaceEnd, + endInfo = self._findPattern(self.STOP_PATTERN, offset); + + trailingWhitespaceEnd = endInfo.end + (endInfo.end - endInfo.start); + if (trailingWhitespaceEnd < self._row.length) { + if (self._matchRange(endInfo.end, trailingWhitespaceEnd, 0)) { + return endInfo; + } + } + return null; + }; + EANReader.prototype._decode = function() { var startInfo, self = this, @@ -177,7 +210,7 @@ define( decodedCodes = []; try { - startInfo = self._findPattern(self.START_PATTERN); + startInfo = self._findStart(); code = { code : startInfo.code, start : startInfo.start, @@ -215,7 +248,11 @@ define( result.push(code.code); } - code = self._findPattern(self.STOP_PATTERN, code.end); + code = self._findEnd(code.end); + if (code === null){ + return null; + } + decodedCodes.push(code); // Checksum diff --git a/test/fixtures/code_128/image-001.jpg b/test/fixtures/code_128/image-001.jpg index 2b3b89e..8156afb 100644 Binary files a/test/fixtures/code_128/image-001.jpg and b/test/fixtures/code_128/image-001.jpg differ diff --git a/test/fixtures/code_128/image-002.jpg b/test/fixtures/code_128/image-002.jpg index 4e6aab3..cfd0e52 100644 Binary files a/test/fixtures/code_128/image-002.jpg and b/test/fixtures/code_128/image-002.jpg differ diff --git a/test/fixtures/code_128/image-003.jpg b/test/fixtures/code_128/image-003.jpg index bbf945a..46fdf39 100644 Binary files a/test/fixtures/code_128/image-003.jpg and b/test/fixtures/code_128/image-003.jpg differ diff --git a/test/fixtures/code_128/image-004.jpg b/test/fixtures/code_128/image-004.jpg index a71d5b8..11759ce 100644 Binary files a/test/fixtures/code_128/image-004.jpg and b/test/fixtures/code_128/image-004.jpg differ diff --git a/test/fixtures/code_128/image-005.jpg b/test/fixtures/code_128/image-005.jpg index 8fbd0d3..8d3e917 100644 Binary files a/test/fixtures/code_128/image-005.jpg and b/test/fixtures/code_128/image-005.jpg differ diff --git a/test/fixtures/code_128/image-006.jpg b/test/fixtures/code_128/image-006.jpg index ae2e020..d66a6cd 100644 Binary files a/test/fixtures/code_128/image-006.jpg and b/test/fixtures/code_128/image-006.jpg differ diff --git a/test/fixtures/code_128/image-007.jpg b/test/fixtures/code_128/image-007.jpg index f5ad54c..27cb82f 100644 Binary files a/test/fixtures/code_128/image-007.jpg and b/test/fixtures/code_128/image-007.jpg differ diff --git a/test/fixtures/code_128/image-008.jpg b/test/fixtures/code_128/image-008.jpg index 4cbd7e1..566943a 100644 Binary files a/test/fixtures/code_128/image-008.jpg and b/test/fixtures/code_128/image-008.jpg differ diff --git a/test/fixtures/code_128/image-009.jpg b/test/fixtures/code_128/image-009.jpg index 20e1c91..4614592 100644 Binary files a/test/fixtures/code_128/image-009.jpg and b/test/fixtures/code_128/image-009.jpg differ diff --git a/test/fixtures/code_128/image-010.jpg b/test/fixtures/code_128/image-010.jpg index 7820ac2..d9b6dfb 100644 Binary files a/test/fixtures/code_128/image-010.jpg and b/test/fixtures/code_128/image-010.jpg differ