diff --git a/spec/barcode_locator.spec.js b/spec/barcode_locator.spec.js new file mode 100644 index 0000000..a0694f4 --- /dev/null +++ b/spec/barcode_locator.spec.js @@ -0,0 +1,65 @@ + +define(['barcode_locator', 'config', 'html_utils'], + function(BarcodeLocator, Config, HtmlUtils){ + + describe('checkImageConstraints', function() { + var config, + inputStream, + imageSize; + + 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() {} + }; + sinon.stub(inputStream, "setWidth", function(width) { + imageSize.x = width; + }); + sinon.stub(inputStream, "setHeight", function(height) { + imageSize.y = height; + }); + }); + + 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); + }); + }); +}); diff --git a/spec/cv_utils.spec.js b/spec/cv_utils.spec.js index 2282b01..25a0bcb 100644 --- a/spec/cv_utils.spec.js +++ b/spec/cv_utils.spec.js @@ -8,4 +8,20 @@ define(['cv_utils'], function(CVUtils){ expect(res.toVec2()[0]).to.equal(1); }); }); + + describe('calculatePatchSize', function() { + it('should not throw an error in case of valid image size', function() { + var expected = {x: 32, y: 32}, + patchSize = CVUtils.calculatePatchSize("medium", {x: 640, y: 480}); + + expect(patchSize).to.be.deep.equal(expected); + }); + + it('should thow an error if image size it not valid', function() { + var expected = {x: 32, y: 32}, + patchSize = CVUtils.calculatePatchSize("medium", {x: 640, y: 480}); + + expect(patchSize).to.be.deep.equal(expected); + }); + }); }); diff --git a/src/barcode_locator.js b/src/barcode_locator.js index 8d7b2bf..4b1420d 100644 --- a/src/barcode_locator.js +++ b/src/barcode_locator.js @@ -486,6 +486,7 @@ function(ImageWrapper, CVUtils, Rasterizer, Tracer, skeletonizer, ArrayHelper, I initBuffers(); initCanvas(); }, + locate : function() { var patchesFound, topLabels = [], @@ -516,6 +517,30 @@ function(ImageWrapper, CVUtils, Rasterizer, Tracer, skeletonizer, ArrayHelper, I boxes = findBoxes(topLabels, maxLabel); return boxes; + }, + + checkImageConstraints: function(inputStream, config) { + var patchSize, + width = inputStream.getWidth(), + height = inputStream.getHeight(), + halfSample = config.halfSample ? 0.5 : 1, + size = { + x: Math.floor(width * halfSample), + y: Math.floor(height * halfSample) + }; + + patchSize = CVUtils.calculatePatchSize(config.patchSize, size); + inputStream.setWidth(Math.floor(Math.floor(size.x/patchSize.x)*(1/halfSample)*patchSize.x)); + inputStream.setHeight(Math.floor(Math.floor(size.y/patchSize.y)*(1/halfSample)*patchSize.y)); + + console.log("Patch-Size: " + JSON.stringify(patchSize)); + if ((inputStream.getWidth() % patchSize.x) === 0 && (inputStream.getHeight() % patchSize.y) === 0) { + return true; + } + + throw new Error("Image dimensions do not comply with the current settings: Width (" + + width + " )and height (" + height + + ") must a multiple of " + patchSize.x); } }; }); diff --git a/src/cv_utils.js b/src/cv_utils.js index 12ff3d0..64eef14 100644 --- a/src/cv_utils.js +++ b/src/cv_utils.js @@ -647,7 +647,6 @@ define(['cluster', 'glMatrixAddon', "array_helper"], function(Cluster2, glMatrix optimalPatchSize = findPatchSizeForDivisors(common); if (!optimalPatchSize) { optimalPatchSize = findPatchSizeForDivisors(this._computeDivisors(wideSide)); - throw new AdjustToSizeError("", optimalPatchSize); } return optimalPatchSize; }; diff --git a/src/quagga.js b/src/quagga.js index b7ee257..d752108 100644 --- a/src/quagga.js +++ b/src/quagga.js @@ -14,8 +14,7 @@ define([ "config", "events", "camera_access", - "image_debug", - "cv_utils"], + "image_debug"], function(Code128Reader, EANReader, InputStream, @@ -27,8 +26,7 @@ function(Code128Reader, _config, Events, CameraAccess, - ImageDebug, - CVUtils) { + ImageDebug) { "use strict"; var _inputStream, @@ -107,39 +105,10 @@ function(Code128Reader, _inputStream.addEventListener("canrecord", canRecord.bind(undefined, cb)); } - function checkImageConstraints() { - var patchSize, - width = _inputStream.getWidth(), - height = _inputStream.getHeight(), - halfSample = _config.locator.halfSample ? 0.5 : 1, - size = { - x: Math.floor(width * halfSample), - y: Math.floor(height * halfSample) - }; - + function canRecord(cb) { if (_config.locate) { - try { - console.log(size); - patchSize = CVUtils.calculatePatchSize(_config.locator.patchSize, size); - } catch (error) { - if (error instanceof CVUtils.AdjustToSizeError) { - _inputStream.setWidth(Math.floor(Math.floor(size.x/error.patchSize.x)*(1/halfSample)*error.patchSize.x)); - _inputStream.setHeight(Math.floor(Math.floor(size.y/error.patchSize.y)*(1/halfSample)*error.patchSize.y)); - patchSize = error.patchSize; - } - } - console.log("Patch-Size: " + JSON.stringify(patchSize)); - if ((_inputStream.getWidth() % patchSize.x) === 0 && (_inputStream.getHeight() % patchSize.y) === 0) { - return true; - } + BarcodeLocator.checkImageConstraints(_inputStream, _config.locator); } - throw new Error("Image dimensions do not comply with the current settings: Width (" + - width + " )and height (" + height + - ") must a multiple of " + patchSize.x); - } - - function canRecord(cb) { - checkImageConstraints(); initCanvas(); _framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image); initConfig();