Skip to content

Commit

Permalink
Improving EAN-Reader, Barcode-Line extension, ToBinaryLine
Browse files Browse the repository at this point in the history
  • Loading branch information
serratus committed Apr 25, 2015
1 parent f75da96 commit 566c10b
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 52 deletions.
90 changes: 71 additions & 19 deletions dist/quagga.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -1250,7 +1283,7 @@ define(
decodedCodes = [];

try {
startInfo = self._findPattern(self.START_PATTERN);
startInfo = self._findStart();
code = {
code : startInfo.code,
start : startInfo.start,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}
Expand Down Expand Up @@ -7402,7 +7454,7 @@ define('config',[],function(){
debug: false,
controls: false,
locate: true,
numOfWorkers: 4,
numOfWorkers: 0,
visual: {
show: true
},
Expand Down
16 changes: 8 additions & 8 deletions example/file_input.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ <h3>Working with file-input</h3>
<label>
<span>Barcode-Type</span>
<select name="decoder_readers">
<option value="code_128" selected="selected">Code 128</option>
<option value="code_128">Code 128</option>
<option value="code_39">Code 39</option>
<option value="ean">EAN</option>
<option value="ean" selected="selected">EAN</option>
<option value="codabar">Codabar</option>
</select>
</label>
<label>
<span>Resolution (long side)</span>
<select name="input-stream_size">
<option value="320">320px</option>
<option value="640">640px</option>
<option selected="selected" value="800">800px</option>
<option selected="selected" value="640">640px</option>
<option value="800">800px</option>
<option value="1280">1280px</option>
<option value="1600">1600px</option>
<option value="1920">1920px</option>
Expand All @@ -64,8 +64,8 @@ <h3>Working with file-input</h3>
<select name="locator_patch-size">
<option value="x-small">x-small</option>
<option value="small">small</option>
<option selected="selected" value="medium">medium</option>
<option value="large">large</option>
<option value="medium">medium</option>
<option selected="selected" value="large">large</option>
<option value="x-large">x-large</option>
</select>
</label>
Expand All @@ -76,8 +76,8 @@ <h3>Working with file-input</h3>
<label>
<span>Workers</span>
<select name="numOfWorkers">
<option value="0">0</option>
<option value="1" selected="selected">1</option>
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
Expand Down
10 changes: 6 additions & 4 deletions example/file_input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions example/static_images.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ <h3>Working with static images</h3>
<button class="next">Next</button>
<fieldset class="reader-group">
<label>Codabar</label>
<input type="radio" name="reader" value="codabar" checked />
<input type="radio" name="reader" value="codabar" />
<label>Code39</label>
<input type="radio" name="reader" value="code_39" />
<label>Code128</label>
<input type="radio" name="reader" value="code_128" />
<label>EAN</label>
<input type="radio" name="reader" value="ean" />
<input type="radio" name="reader" value="ean" checked />
</fieldset>
</div>
<div id="result_strip">
Expand Down
2 changes: 1 addition & 1 deletion example/static_images.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $(function() {
});
},
config: {
reader: "codabar",
reader: "ean",
length: 10
},
attachListeners: function() {
Expand Down
37 changes: 26 additions & 11 deletions src/barcode_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions src/bresenham.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 566c10b

Please sign in to comment.