Merge branch 'master' of https://github.com/serratus/quaggaJS into jclarkin-master
commit
8dea6f6297
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@ -0,0 +1,131 @@
|
|||||||
|
|
||||||
|
define(['barcode_locator', 'config', 'html_utils'],
|
||||||
|
function(BarcodeLocator, Config, HtmlUtils){
|
||||||
|
|
||||||
|
describe('checkImageConstraints', function() {
|
||||||
|
var config,
|
||||||
|
inputStream,
|
||||||
|
imageSize,
|
||||||
|
streamConfig = {};
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
imageSize = {
|
||||||
|
x: 640, y: 480
|
||||||
|
};
|
||||||
|
config = HtmlUtils.mergeObjects({}, Config);
|
||||||
|
inputStream = {
|
||||||
|
getWidth: function() {
|
||||||
|
return imageSize.x;
|
||||||
|
},
|
||||||
|
getHeight: function() {
|
||||||
|
return imageSize.y;
|
||||||
|
},
|
||||||
|
setWidth: function() {},
|
||||||
|
setHeight: function() {},
|
||||||
|
setTopRight: function() {},
|
||||||
|
setCanvasSize: function() {},
|
||||||
|
getConfig: function() {
|
||||||
|
return streamConfig;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
sinon.stub(inputStream, "setWidth", function(width) {
|
||||||
|
imageSize.x = width;
|
||||||
|
});
|
||||||
|
sinon.stub(inputStream, "setHeight", function(height) {
|
||||||
|
imageSize.y = height;
|
||||||
|
});
|
||||||
|
sinon.stub(inputStream, "setTopRight");
|
||||||
|
sinon.stub(inputStream, "setCanvasSize");
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
inputStream.setWidth.restore();
|
||||||
|
inputStream.setHeight.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not adjust the image-size if not needed', function() {
|
||||||
|
var expected = {x: imageSize.x, y: imageSize.y};
|
||||||
|
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||||
|
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||||
|
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should adjust the image-size', function() {
|
||||||
|
var expected = {x: imageSize.x, y: imageSize.y};
|
||||||
|
|
||||||
|
config.locator.halfSample = true;
|
||||||
|
imageSize.y += 1;
|
||||||
|
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||||
|
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||||
|
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should adjust the image-size', function() {
|
||||||
|
var expected = {x: imageSize.x, y: imageSize.y};
|
||||||
|
|
||||||
|
imageSize.y += 1;
|
||||||
|
config.locator.halfSample = false;
|
||||||
|
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||||
|
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||||
|
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should take the defined area into account", function() {
|
||||||
|
var expectedSize = {
|
||||||
|
x: 420,
|
||||||
|
y: 315
|
||||||
|
},
|
||||||
|
expectedTopRight = {
|
||||||
|
x: 115,
|
||||||
|
y: 52
|
||||||
|
},
|
||||||
|
expectedCanvasSize = {
|
||||||
|
x: 640,
|
||||||
|
y: 480
|
||||||
|
};
|
||||||
|
|
||||||
|
streamConfig.area = {
|
||||||
|
top: "11%",
|
||||||
|
right: "15%",
|
||||||
|
bottom: "20%",
|
||||||
|
left: "18%"
|
||||||
|
};
|
||||||
|
|
||||||
|
config.locator.halfSample = false;
|
||||||
|
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||||
|
expect(inputStream.getHeight()).to.be.equal(expectedSize.y);
|
||||||
|
expect(inputStream.getWidth()).to.be.equal(expectedSize.x);
|
||||||
|
expect(inputStream.setTopRight.getCall(0).args[0]).to.deep.equal(expectedTopRight);
|
||||||
|
expect(inputStream.setCanvasSize.getCall(0).args[0]).to.deep.equal(expectedCanvasSize);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return the original size if set to full image", function() {
|
||||||
|
var expectedSize = {
|
||||||
|
x: 640,
|
||||||
|
y: 480
|
||||||
|
},
|
||||||
|
expectedTopRight = {
|
||||||
|
x: 0,
|
||||||
|
y: 0
|
||||||
|
},
|
||||||
|
expectedCanvasSize = {
|
||||||
|
x: 640,
|
||||||
|
y: 480
|
||||||
|
};
|
||||||
|
|
||||||
|
streamConfig.area = {
|
||||||
|
top: "0%",
|
||||||
|
right: "0%",
|
||||||
|
bottom: "0%",
|
||||||
|
left: "0%"
|
||||||
|
};
|
||||||
|
|
||||||
|
config.locator.halfSample = false;
|
||||||
|
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||||
|
expect(inputStream.getHeight()).to.be.equal(expectedSize.y);
|
||||||
|
expect(inputStream.getWidth()).to.be.equal(expectedSize.x);
|
||||||
|
expect(inputStream.setTopRight.getCall(0).args[0]).to.deep.equal(expectedTopRight);
|
||||||
|
expect(inputStream.setCanvasSize.getCall(0).args[0]).to.deep.equal(expectedCanvasSize);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,103 @@
|
|||||||
|
|
||||||
|
define(['result_collector', 'image_debug'], function(ResultCollector, ImageDebug) {
|
||||||
|
var canvasMock,
|
||||||
|
imageSize,
|
||||||
|
config;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
imageSize = {x: 320, y: 240};
|
||||||
|
config = {
|
||||||
|
capture: true,
|
||||||
|
capacity: 20,
|
||||||
|
blacklist: [{code: "3574660239843", format: "ean_13"}],
|
||||||
|
filter: function(codeResult) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
canvasMock = {
|
||||||
|
getContext: function() {
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
toDataURL: sinon.spy(),
|
||||||
|
width: 0,
|
||||||
|
height: 0
|
||||||
|
};
|
||||||
|
sinon.stub(document, "createElement", function(type) {
|
||||||
|
if (type === "canvas") {
|
||||||
|
return canvasMock;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
document.createElement.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('create', function () {
|
||||||
|
it("should return a new collector", function() {
|
||||||
|
ResultCollector.create(config);
|
||||||
|
expect(document.createElement.calledOnce).to.be.equal(true);
|
||||||
|
expect(document.createElement.getCall(0).args[0]).to.equal("canvas");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('addResult', function() {
|
||||||
|
beforeEach(function() {
|
||||||
|
sinon.stub(ImageDebug, "drawImage", function() {});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
ImageDebug.drawImage.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not add result if capacity is full", function(){
|
||||||
|
config.capacity = 1;
|
||||||
|
var collector = ResultCollector.create(config);
|
||||||
|
collector.addResult([], imageSize, {});
|
||||||
|
collector.addResult([], imageSize, {});
|
||||||
|
collector.addResult([], imageSize, {});
|
||||||
|
expect(collector.getResults()).to.have.length(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should only add results which match constraints", function(){
|
||||||
|
var collector = ResultCollector.create(config),
|
||||||
|
results;
|
||||||
|
|
||||||
|
collector.addResult([], imageSize, {code: "423423443", format: "ean_13"});
|
||||||
|
collector.addResult([], imageSize, {code: "3574660239843", format: "ean_13"});
|
||||||
|
collector.addResult([], imageSize, {code: "3574660239843", format: "code_128"});
|
||||||
|
|
||||||
|
results = collector.getResults();
|
||||||
|
expect(results).to.have.length(2);
|
||||||
|
|
||||||
|
results.forEach(function(result) {
|
||||||
|
expect(result).not.to.deep.equal(config.blacklist[0]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should add result if no filter is set", function() {
|
||||||
|
delete config.filter;
|
||||||
|
var collector = ResultCollector.create(config);
|
||||||
|
|
||||||
|
collector.addResult([], imageSize, {code: "423423443", format: "ean_13"});
|
||||||
|
expect(collector.getResults()).to.have.length(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not add results if filter returns false", function() {
|
||||||
|
config.filter = function(){ return false };
|
||||||
|
var collector = ResultCollector.create(config);
|
||||||
|
|
||||||
|
collector.addResult([], imageSize, {code: "423423443", format: "ean_13"});
|
||||||
|
expect(collector.getResults()).to.have.length(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should add result if no blacklist is set", function() {
|
||||||
|
delete config.blacklist;
|
||||||
|
var collector = ResultCollector.create(config);
|
||||||
|
|
||||||
|
collector.addResult([], imageSize, {code: "3574660239843", format: "ean_13"});
|
||||||
|
expect(collector.getResults()).to.have.length(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,59 @@
|
|||||||
|
/* jshint undef: true, unused: true, browser:true, devel: true */
|
||||||
|
/* global define */
|
||||||
|
|
||||||
|
define(["image_debug"], function(ImageDebug) {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
function contains(codeResult, list) {
|
||||||
|
if (list) {
|
||||||
|
return list.some(function (item) {
|
||||||
|
return Object.keys(item).every(function (key) {
|
||||||
|
return item[key] === codeResult[key];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function passesFilter(codeResult, filter) {
|
||||||
|
if (typeof filter === 'function') {
|
||||||
|
return filter(codeResult);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
create: function(config) {
|
||||||
|
var canvas = document.createElement("canvas"),
|
||||||
|
ctx = canvas.getContext("2d"),
|
||||||
|
results = [],
|
||||||
|
capacity = config.capacity || 20,
|
||||||
|
capture = config.capture === true;
|
||||||
|
|
||||||
|
function matchesConstraints(codeResult) {
|
||||||
|
return capacity && codeResult && !contains(codeResult, config.blacklist) && passesFilter(codeResult, config.filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
addResult: function(data, imageSize, codeResult) {
|
||||||
|
var result = {};
|
||||||
|
|
||||||
|
if (matchesConstraints(codeResult)) {
|
||||||
|
capacity--;
|
||||||
|
result.codeResult = codeResult;
|
||||||
|
if (capture) {
|
||||||
|
canvas.width = imageSize.x;
|
||||||
|
canvas.height = imageSize.y;
|
||||||
|
ImageDebug.drawImage(data, imageSize, ctx);
|
||||||
|
result.frame = canvas.toDataURL();
|
||||||
|
}
|
||||||
|
results.push(result);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getResults: function() {
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
Loading…
Reference in New Issue