mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
issue-43: Added configurable area for decoding
This commit is contained in:
Vendored
+219
-82
@@ -1489,7 +1489,9 @@ define('input_stream',["image_loader"], function(ImageLoader) {
|
|||||||
_eventNames = ['canrecord', 'ended'],
|
_eventNames = ['canrecord', 'ended'],
|
||||||
_eventHandlers = {},
|
_eventHandlers = {},
|
||||||
_calculatedWidth,
|
_calculatedWidth,
|
||||||
_calculatedHeight;
|
_calculatedHeight,
|
||||||
|
_topRight = {x: 0, y: 0},
|
||||||
|
_canvasSize = {x: 0, y: 0};
|
||||||
|
|
||||||
function initSize() {
|
function initSize() {
|
||||||
var width = video.videoWidth,
|
var width = video.videoWidth,
|
||||||
@@ -1497,6 +1499,9 @@ define('input_stream',["image_loader"], function(ImageLoader) {
|
|||||||
|
|
||||||
_calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
|
_calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
|
||||||
_calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
|
_calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
|
||||||
|
|
||||||
|
_canvasSize.x = _calculatedWidth;
|
||||||
|
_canvasSize.y = _calculatedHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
that.getRealWidth = function() {
|
that.getRealWidth = function() {
|
||||||
@@ -1589,6 +1594,24 @@ define('input_stream',["image_loader"], function(ImageLoader) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
that.setTopRight = function(topRight) {
|
||||||
|
_topRight.x = topRight.x;
|
||||||
|
_topRight.y = topRight.y;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getTopRight = function() {
|
||||||
|
return _topRight;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.setCanvasSize = function(size) {
|
||||||
|
_canvasSize.x = size.x;
|
||||||
|
_canvasSize.y = size.y;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getCanvasSize = function() {
|
||||||
|
return _canvasSize;
|
||||||
|
};
|
||||||
|
|
||||||
that.getFrame = function() {
|
that.getFrame = function() {
|
||||||
return video;
|
return video;
|
||||||
};
|
};
|
||||||
@@ -1624,7 +1647,9 @@ define('input_stream',["image_loader"], function(ImageLoader) {
|
|||||||
calculatedWidth,
|
calculatedWidth,
|
||||||
calculatedHeight,
|
calculatedHeight,
|
||||||
_eventNames = ['canrecord', 'ended'],
|
_eventNames = ['canrecord', 'ended'],
|
||||||
_eventHandlers = {};
|
_eventHandlers = {},
|
||||||
|
_topRight = {x: 0, y: 0},
|
||||||
|
_canvasSize = {x: 0, y: 0};
|
||||||
|
|
||||||
function loadImages() {
|
function loadImages() {
|
||||||
loaded = false;
|
loaded = false;
|
||||||
@@ -1634,6 +1659,8 @@ define('input_stream',["image_loader"], function(ImageLoader) {
|
|||||||
height = imgs[0].height;
|
height = imgs[0].height;
|
||||||
calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
|
calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
|
||||||
calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
|
calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
|
||||||
|
_canvasSize.x = calculatedWidth;
|
||||||
|
_canvasSize.y = calculatedHeight;
|
||||||
loaded = true;
|
loaded = true;
|
||||||
frameIdx = 0;
|
frameIdx = 0;
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
@@ -1723,6 +1750,24 @@ define('input_stream',["image_loader"], function(ImageLoader) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
that.setTopRight = function(topRight) {
|
||||||
|
_topRight.x = topRight.x;
|
||||||
|
_topRight.y = topRight.y;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getTopRight = function() {
|
||||||
|
return _topRight;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.setCanvasSize = function(size) {
|
||||||
|
_canvasSize.x = size.x;
|
||||||
|
_canvasSize.y = size.y;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getCanvasSize = function() {
|
||||||
|
return _canvasSize;
|
||||||
|
};
|
||||||
|
|
||||||
that.getFrame = function() {
|
that.getFrame = function() {
|
||||||
var frame;
|
var frame;
|
||||||
|
|
||||||
@@ -4955,21 +5000,64 @@ define('cv_utils',['cluster', 'glMatrixAddon', "array_helper"], function(Cluster
|
|||||||
optimalPatchSize = findPatchSizeForDivisors(common);
|
optimalPatchSize = findPatchSizeForDivisors(common);
|
||||||
if (!optimalPatchSize) {
|
if (!optimalPatchSize) {
|
||||||
optimalPatchSize = findPatchSizeForDivisors(this._computeDivisors(wideSide));
|
optimalPatchSize = findPatchSizeForDivisors(this._computeDivisors(wideSide));
|
||||||
throw new AdjustToSizeError("", optimalPatchSize);
|
if (!optimalPatchSize) {
|
||||||
|
optimalPatchSize = findPatchSizeForDivisors((this._computeDivisors(desiredPatchSize * nrOfPatches)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return optimalPatchSize;
|
return optimalPatchSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
function AdjustToSizeError(message, desiredPatchSize) {
|
CVUtils._parseCSSDimensionValues = function(value) {
|
||||||
this.name = 'AdjustToSizeError';
|
var dimension = {
|
||||||
this.message = message || 'AdjustToSizeError';
|
value: parseFloat(value),
|
||||||
this.patchSize = desiredPatchSize;
|
unit: value.indexOf("%") === value.length-1 ? "%" : "px"
|
||||||
}
|
};
|
||||||
|
|
||||||
AdjustToSizeError.prototype = Object.create(RangeError.prototype);
|
return dimension;
|
||||||
AdjustToSizeError.prototype.constructor = AdjustToSizeError;
|
};
|
||||||
|
|
||||||
CVUtils.AdjustToSizeError = AdjustToSizeError;
|
CVUtils._dimensionsConverters = {
|
||||||
|
top: function(dimension, context) {
|
||||||
|
if (dimension.unit === "%") {
|
||||||
|
return Math.floor(context.height * (dimension.value / 100));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
right: function(dimension, context) {
|
||||||
|
if (dimension.unit === "%") {
|
||||||
|
return Math.floor(context.width - (context.width * (dimension.value / 100)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
bottom: function(dimension, context) {
|
||||||
|
if (dimension.unit === "%") {
|
||||||
|
return Math.floor(context.height - (context.height * (dimension.value / 100)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
left: function(dimension, context) {
|
||||||
|
if (dimension.unit === "%") {
|
||||||
|
return Math.floor(context.width * (dimension.value / 100));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
CVUtils.computeImageArea = function(inputWidth, inputHeight, area) {
|
||||||
|
var context = {width: inputWidth, height: inputHeight};
|
||||||
|
|
||||||
|
var parsedArea = Object.keys(area).reduce(function(result, key) {
|
||||||
|
var value = area[key],
|
||||||
|
parsed = CVUtils._parseCSSDimensionValues(value),
|
||||||
|
calculated = CVUtils._dimensionsConverters[key](parsed, context);
|
||||||
|
|
||||||
|
result[key] = calculated;
|
||||||
|
return result;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
return {
|
||||||
|
sx: parsedArea.left,
|
||||||
|
sy: parsedArea.top,
|
||||||
|
sw: parsedArea.right - parsedArea.left,
|
||||||
|
sh: parsedArea.bottom - parsedArea.top
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
return (CVUtils);
|
return (CVUtils);
|
||||||
});
|
});
|
||||||
@@ -6430,10 +6518,11 @@ function(ImageWrapper, CVUtils, Rasterizer, Tracer, skeletonizer, ArrayHelper, I
|
|||||||
initBuffers();
|
initBuffers();
|
||||||
initCanvas();
|
initCanvas();
|
||||||
},
|
},
|
||||||
|
|
||||||
locate : function() {
|
locate : function() {
|
||||||
var patchesFound,
|
var patchesFound,
|
||||||
topLabels = [],
|
topLabels,
|
||||||
boxes = [];
|
boxes;
|
||||||
|
|
||||||
if (_config.halfSample) {
|
if (_config.halfSample) {
|
||||||
CVUtils.halfSample(_inputImageWrapper, _currentImageWrapper);
|
CVUtils.halfSample(_inputImageWrapper, _currentImageWrapper);
|
||||||
@@ -6460,6 +6549,42 @@ function(ImageWrapper, CVUtils, Rasterizer, Tracer, skeletonizer, ArrayHelper, I
|
|||||||
|
|
||||||
boxes = findBoxes(topLabels, maxLabel);
|
boxes = findBoxes(topLabels, maxLabel);
|
||||||
return boxes;
|
return boxes;
|
||||||
|
},
|
||||||
|
|
||||||
|
checkImageConstraints: function(inputStream, config) {
|
||||||
|
var patchSize,
|
||||||
|
width = inputStream.getWidth(),
|
||||||
|
height = inputStream.getHeight(),
|
||||||
|
halfSample = config.halfSample ? 0.5 : 1,
|
||||||
|
size,
|
||||||
|
area;
|
||||||
|
|
||||||
|
// calculate width and height based on area
|
||||||
|
if (inputStream.getConfig().area) {
|
||||||
|
area = CVUtils.computeImageArea(width, height, inputStream.getConfig().area);
|
||||||
|
inputStream.setTopRight({x: area.sx, y: area.sy});
|
||||||
|
inputStream.setCanvasSize({x: width, y: height});
|
||||||
|
width = area.sw;
|
||||||
|
height = area.sh;
|
||||||
|
}
|
||||||
|
|
||||||
|
size = {
|
||||||
|
x: Math.floor(width * halfSample),
|
||||||
|
y: Math.floor(height * halfSample)
|
||||||
|
};
|
||||||
|
|
||||||
|
patchSize = CVUtils.calculatePatchSize(config.patchSize, size);
|
||||||
|
console.log("patchSize:", patchSize);
|
||||||
|
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));
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -7762,29 +7887,26 @@ define('frame_grabber',["cv_utils"], function(CVUtils) {
|
|||||||
var _that = {},
|
var _that = {},
|
||||||
_streamConfig = inputStream.getConfig(),
|
_streamConfig = inputStream.getConfig(),
|
||||||
_video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight()),
|
_video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight()),
|
||||||
_size =_streamConfig.size ? CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight()) : _video_size,
|
_canvasSize = inputStream.getCanvasSize(),
|
||||||
_sx = 0,
|
_size = CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight()),
|
||||||
_sy = 0,
|
topRight = inputStream.getTopRight(),
|
||||||
_dx = 0,
|
_sx = topRight.x,
|
||||||
_dy = 0,
|
_sy = topRight.y,
|
||||||
_sWidth,
|
_canvas,
|
||||||
_dWidth,
|
|
||||||
_sHeight,
|
|
||||||
_dHeight,
|
|
||||||
_canvas = null,
|
|
||||||
_ctx = null,
|
_ctx = null,
|
||||||
_data = null;
|
_data = null;
|
||||||
|
|
||||||
_sWidth = _video_size.x;
|
|
||||||
_dWidth = _size.x;
|
|
||||||
_sHeight = _video_size.y;
|
|
||||||
_dHeight = _size.y;
|
|
||||||
|
|
||||||
_canvas = canvas ? canvas : document.createElement("canvas");
|
_canvas = canvas ? canvas : document.createElement("canvas");
|
||||||
_canvas.width = _size.x;
|
_canvas.width = _canvasSize.x;
|
||||||
_canvas.height = _size.y;
|
_canvas.height = _canvasSize.y;
|
||||||
_ctx = _canvas.getContext("2d");
|
_ctx = _canvas.getContext("2d");
|
||||||
_data = new Uint8Array(_size.x * _size.y);
|
_data = new Uint8Array(_size.x * _size.y);
|
||||||
|
console.log("FrameGrabber", {
|
||||||
|
size: _size,
|
||||||
|
topRight: topRight,
|
||||||
|
videoSize: _video_size,
|
||||||
|
canvasSize: _canvasSize
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses the given array as frame-buffer
|
* Uses the given array as frame-buffer
|
||||||
@@ -7809,8 +7931,8 @@ define('frame_grabber',["cv_utils"], function(CVUtils) {
|
|||||||
frame = inputStream.getFrame(),
|
frame = inputStream.getFrame(),
|
||||||
ctxData;
|
ctxData;
|
||||||
if (frame) {
|
if (frame) {
|
||||||
_ctx.drawImage(frame, _sx, _sy, _sWidth, _sHeight, _dx, _dy, _dWidth, _dHeight);
|
_ctx.drawImage(frame, 0, 0, _canvasSize.x, _canvasSize.y);
|
||||||
ctxData = _ctx.getImageData(0, 0, _size.x, _size.y).data;
|
ctxData = _ctx.getImageData(_sx, _sy, _size.x, _size.y).data;
|
||||||
if(doHalfSample){
|
if(doHalfSample){
|
||||||
CVUtils.grayAndHalfSampleFromCanvasData(ctxData, _size, _data);
|
CVUtils.grayAndHalfSampleFromCanvasData(ctxData, _size, _data);
|
||||||
} else {
|
} else {
|
||||||
@@ -7886,6 +8008,12 @@ define('config',[],function(){
|
|||||||
minAspectRatio: 0,
|
minAspectRatio: 0,
|
||||||
maxAspectRatio: 100,
|
maxAspectRatio: 100,
|
||||||
facing: "environment" // or user
|
facing: "environment" // or user
|
||||||
|
},
|
||||||
|
area: {
|
||||||
|
top: "0%",
|
||||||
|
right: "0%",
|
||||||
|
left: "0%",
|
||||||
|
bottom: "0%"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
tracking: false,
|
tracking: false,
|
||||||
@@ -8173,8 +8301,7 @@ define('quagga',[
|
|||||||
"config",
|
"config",
|
||||||
"events",
|
"events",
|
||||||
"camera_access",
|
"camera_access",
|
||||||
"image_debug",
|
"image_debug"],
|
||||||
"cv_utils"],
|
|
||||||
function(Code128Reader,
|
function(Code128Reader,
|
||||||
EANReader,
|
EANReader,
|
||||||
InputStream,
|
InputStream,
|
||||||
@@ -8186,8 +8313,7 @@ function(Code128Reader,
|
|||||||
_config,
|
_config,
|
||||||
Events,
|
Events,
|
||||||
CameraAccess,
|
CameraAccess,
|
||||||
ImageDebug,
|
ImageDebug) {
|
||||||
CVUtils) {
|
|
||||||
|
|
||||||
|
|
||||||
var _inputStream,
|
var _inputStream,
|
||||||
@@ -8266,39 +8392,10 @@ function(Code128Reader,
|
|||||||
_inputStream.addEventListener("canrecord", canRecord.bind(undefined, cb));
|
_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)
|
|
||||||
};
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
function canRecord(cb) {
|
||||||
checkImageConstraints();
|
if (_config.locate) {
|
||||||
|
BarcodeLocator.checkImageConstraints(_inputStream, _config.locator);
|
||||||
|
}
|
||||||
initCanvas();
|
initCanvas();
|
||||||
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
|
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
|
||||||
initConfig();
|
initConfig();
|
||||||
@@ -8330,8 +8427,8 @@ function(Code128Reader,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_canvasContainer.ctx.image = _canvasContainer.dom.image.getContext("2d");
|
_canvasContainer.ctx.image = _canvasContainer.dom.image.getContext("2d");
|
||||||
_canvasContainer.dom.image.width = _inputStream.getWidth();
|
_canvasContainer.dom.image.width = _inputStream.getCanvasSize().x;
|
||||||
_canvasContainer.dom.image.height = _inputStream.getHeight();
|
_canvasContainer.dom.image.height = _inputStream.getCanvasSize().y;
|
||||||
|
|
||||||
_canvasContainer.dom.overlay = document.querySelector("canvas.drawingBuffer");
|
_canvasContainer.dom.overlay = document.querySelector("canvas.drawingBuffer");
|
||||||
if (!_canvasContainer.dom.overlay) {
|
if (!_canvasContainer.dom.overlay) {
|
||||||
@@ -8347,8 +8444,8 @@ function(Code128Reader,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_canvasContainer.ctx.overlay = _canvasContainer.dom.overlay.getContext("2d");
|
_canvasContainer.ctx.overlay = _canvasContainer.dom.overlay.getContext("2d");
|
||||||
_canvasContainer.dom.overlay.width = _inputStream.getWidth();
|
_canvasContainer.dom.overlay.width = _inputStream.getCanvasSize().x;
|
||||||
_canvasContainer.dom.overlay.height = _inputStream.getHeight();
|
_canvasContainer.dom.overlay.height = _inputStream.getCanvasSize().y;
|
||||||
}
|
}
|
||||||
|
|
||||||
function initBuffers(imageWrapper) {
|
function initBuffers(imageWrapper) {
|
||||||
@@ -8379,6 +8476,53 @@ function(Code128Reader,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function transformResult(result) {
|
||||||
|
var topRight = _inputStream.getTopRight(),
|
||||||
|
xOffset = topRight.x,
|
||||||
|
yOffset = topRight.y,
|
||||||
|
i;
|
||||||
|
|
||||||
|
if (!result || (xOffset === 0 && yOffset === 0)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (result.line && result.line.length === 2) {
|
||||||
|
moveLine(result.line);
|
||||||
|
}
|
||||||
|
if (result.boxes && result.boxes.length > 0) {
|
||||||
|
for (i = 0; i < result.boxes.length; i++) {
|
||||||
|
moveBox(result.boxes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveBox(box) {
|
||||||
|
var corner = box.length;
|
||||||
|
|
||||||
|
while(corner--) {
|
||||||
|
box[corner][0] += xOffset;
|
||||||
|
box[corner][1] += yOffset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveLine(line) {
|
||||||
|
line[0].x += xOffset;
|
||||||
|
line[0].y += yOffset;
|
||||||
|
line[1].x += xOffset;
|
||||||
|
line[1].y += yOffset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function publishResult(result) {
|
||||||
|
if (_onUIThread) {
|
||||||
|
transformResult(result);
|
||||||
|
}
|
||||||
|
Events.publish("processed", result);
|
||||||
|
if (result && result.codeResult) {
|
||||||
|
Events.publish("detected", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function locateAndDecode() {
|
function locateAndDecode() {
|
||||||
var result,
|
var result,
|
||||||
boxes;
|
boxes;
|
||||||
@@ -8388,14 +8532,10 @@ function(Code128Reader,
|
|||||||
result = _decoder.decodeFromBoundingBoxes(boxes);
|
result = _decoder.decodeFromBoundingBoxes(boxes);
|
||||||
result = result || {};
|
result = result || {};
|
||||||
result.boxes = boxes;
|
result.boxes = boxes;
|
||||||
Events.publish("processed", result);
|
publishResult(result);
|
||||||
if (result && result.codeResult) {
|
|
||||||
Events.publish("detected", result);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Events.publish("processed");
|
publishResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function update() {
|
function update() {
|
||||||
@@ -8479,10 +8619,7 @@ function(Code128Reader,
|
|||||||
} else if (e.data.event === 'processed') {
|
} else if (e.data.event === 'processed') {
|
||||||
workerThread.imageData = new Uint8Array(e.data.imageData);
|
workerThread.imageData = new Uint8Array(e.data.imageData);
|
||||||
workerThread.busy = false;
|
workerThread.busy = false;
|
||||||
Events.publish("processed", e.data.result);
|
publishResult(e.data.result);
|
||||||
if (e.data.result && e.data.result.codeResult) {
|
|
||||||
Events.publish("detected", e.data.result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Vendored
+4
-4
File diff suppressed because one or more lines are too long
@@ -5,7 +5,8 @@ define(['barcode_locator', 'config', 'html_utils'],
|
|||||||
describe('checkImageConstraints', function() {
|
describe('checkImageConstraints', function() {
|
||||||
var config,
|
var config,
|
||||||
inputStream,
|
inputStream,
|
||||||
imageSize;
|
imageSize,
|
||||||
|
streamConfig = {};
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
imageSize = {
|
imageSize = {
|
||||||
@@ -20,7 +21,12 @@ define(['barcode_locator', 'config', 'html_utils'],
|
|||||||
return imageSize.y;
|
return imageSize.y;
|
||||||
},
|
},
|
||||||
setWidth: function() {},
|
setWidth: function() {},
|
||||||
setHeight: function() {}
|
setHeight: function() {},
|
||||||
|
setTopRight: function() {},
|
||||||
|
setCanvasSize: function() {},
|
||||||
|
getConfig: function() {
|
||||||
|
return streamConfig;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
sinon.stub(inputStream, "setWidth", function(width) {
|
sinon.stub(inputStream, "setWidth", function(width) {
|
||||||
imageSize.x = width;
|
imageSize.x = width;
|
||||||
@@ -28,6 +34,8 @@ define(['barcode_locator', 'config', 'html_utils'],
|
|||||||
sinon.stub(inputStream, "setHeight", function(height) {
|
sinon.stub(inputStream, "setHeight", function(height) {
|
||||||
imageSize.y = height;
|
imageSize.y = height;
|
||||||
});
|
});
|
||||||
|
sinon.stub(inputStream, "setTopRight");
|
||||||
|
sinon.stub(inputStream, "setCanvasSize");
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
@@ -61,5 +69,63 @@ define(['barcode_locator', 'config', 'html_utils'],
|
|||||||
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||||
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -24,4 +24,122 @@ define(['cv_utils'], function(CVUtils){
|
|||||||
expect(patchSize).to.be.deep.equal(expected);
|
expect(patchSize).to.be.deep.equal(expected);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('_parseCSSDimensionValues', function() {
|
||||||
|
it("should convert a percentual value correctly", function() {
|
||||||
|
var expected = {
|
||||||
|
value: 10,
|
||||||
|
unit: "%"
|
||||||
|
},
|
||||||
|
result = CVUtils._parseCSSDimensionValues("10%");
|
||||||
|
|
||||||
|
expect(result).to.be.deep.equal(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should convert a 0% value correctly", function() {
|
||||||
|
var expected = {
|
||||||
|
value: 100,
|
||||||
|
unit: "%"
|
||||||
|
},
|
||||||
|
result = CVUtils._parseCSSDimensionValues("100%");
|
||||||
|
|
||||||
|
expect(result).to.be.deep.equal(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should convert a 100% value correctly", function() {
|
||||||
|
var expected = {
|
||||||
|
value: 0,
|
||||||
|
unit: "%"
|
||||||
|
},
|
||||||
|
result = CVUtils._parseCSSDimensionValues("0%");
|
||||||
|
|
||||||
|
expect(result).to.be.deep.equal(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should convert a pixel value to percentage", function() {
|
||||||
|
var expected = {
|
||||||
|
value: 26.3,
|
||||||
|
unit: "%"
|
||||||
|
},
|
||||||
|
result = CVUtils._parseCSSDimensionValues("26.3px");
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
|
expect(result).to.be.deep.equal(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("_dimensionsConverters", function(){
|
||||||
|
var context;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
context = {
|
||||||
|
width: 640,
|
||||||
|
height: 480
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should convert a top-value correclty", function() {
|
||||||
|
var expected = 48,
|
||||||
|
result = CVUtils._dimensionsConverters.top({value: 10, unit: "%"}, context);
|
||||||
|
|
||||||
|
expect(result).to.be.equal(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should convert a right-value correclty", function() {
|
||||||
|
var expected = 640 - 128,
|
||||||
|
result = CVUtils._dimensionsConverters.right({value: 20, unit: "%"}, context);
|
||||||
|
|
||||||
|
expect(result).to.be.equal(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should convert a bottom-value correclty", function() {
|
||||||
|
var expected = 480 - 77,
|
||||||
|
result = CVUtils._dimensionsConverters.bottom({value: 16, unit: "%"}, context);
|
||||||
|
|
||||||
|
expect(result).to.be.equal(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should convert a left-value correclty", function() {
|
||||||
|
var expected = 57,
|
||||||
|
result = CVUtils._dimensionsConverters.left({value: 9, unit: "%"}, context);
|
||||||
|
|
||||||
|
expect(result).to.be.equal(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("computeImageArea", function() {
|
||||||
|
it("should calculate an image-area", function() {
|
||||||
|
var expected = {
|
||||||
|
sx: 115,
|
||||||
|
sy: 48,
|
||||||
|
sw: 429,
|
||||||
|
sh: 336
|
||||||
|
},
|
||||||
|
result = CVUtils.computeImageArea(640, 480, {
|
||||||
|
top: "10%",
|
||||||
|
right: "15%",
|
||||||
|
bottom: "20%",
|
||||||
|
left: "18%"
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).to.be.deep.equal(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should calculate full image-area", function() {
|
||||||
|
var expected = {
|
||||||
|
sx: 0,
|
||||||
|
sy: 0,
|
||||||
|
sw: 640,
|
||||||
|
sh: 480
|
||||||
|
},
|
||||||
|
result = CVUtils.computeImageArea(640, 480, {
|
||||||
|
top: "0%",
|
||||||
|
right: "0%",
|
||||||
|
bottom: "0%",
|
||||||
|
left: "0%"
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).to.be.deep.equal(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+20
-7
@@ -489,8 +489,8 @@ function(ImageWrapper, CVUtils, Rasterizer, Tracer, skeletonizer, ArrayHelper, I
|
|||||||
|
|
||||||
locate : function() {
|
locate : function() {
|
||||||
var patchesFound,
|
var patchesFound,
|
||||||
topLabels = [],
|
topLabels,
|
||||||
boxes = [];
|
boxes;
|
||||||
|
|
||||||
if (_config.halfSample) {
|
if (_config.halfSample) {
|
||||||
CVUtils.halfSample(_inputImageWrapper, _currentImageWrapper);
|
CVUtils.halfSample(_inputImageWrapper, _currentImageWrapper);
|
||||||
@@ -524,16 +524,29 @@ function(ImageWrapper, CVUtils, Rasterizer, Tracer, skeletonizer, ArrayHelper, I
|
|||||||
width = inputStream.getWidth(),
|
width = inputStream.getWidth(),
|
||||||
height = inputStream.getHeight(),
|
height = inputStream.getHeight(),
|
||||||
halfSample = config.halfSample ? 0.5 : 1,
|
halfSample = config.halfSample ? 0.5 : 1,
|
||||||
size = {
|
size,
|
||||||
x: Math.floor(width * halfSample),
|
area;
|
||||||
y: Math.floor(height * halfSample)
|
|
||||||
};
|
// calculate width and height based on area
|
||||||
|
if (inputStream.getConfig().area) {
|
||||||
|
area = CVUtils.computeImageArea(width, height, inputStream.getConfig().area);
|
||||||
|
inputStream.setTopRight({x: area.sx, y: area.sy});
|
||||||
|
inputStream.setCanvasSize({x: width, y: height});
|
||||||
|
width = area.sw;
|
||||||
|
height = area.sh;
|
||||||
|
}
|
||||||
|
|
||||||
|
size = {
|
||||||
|
x: Math.floor(width * halfSample),
|
||||||
|
y: Math.floor(height * halfSample)
|
||||||
|
};
|
||||||
|
|
||||||
patchSize = CVUtils.calculatePatchSize(config.patchSize, size);
|
patchSize = CVUtils.calculatePatchSize(config.patchSize, size);
|
||||||
|
console.log("Patch-Size: " + JSON.stringify(patchSize));
|
||||||
|
|
||||||
inputStream.setWidth(Math.floor(Math.floor(size.x/patchSize.x)*(1/halfSample)*patchSize.x));
|
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));
|
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) {
|
if ((inputStream.getWidth() % patchSize.x) === 0 && (inputStream.getHeight() % patchSize.y) === 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,12 @@ define(function(){
|
|||||||
minAspectRatio: 0,
|
minAspectRatio: 0,
|
||||||
maxAspectRatio: 100,
|
maxAspectRatio: 100,
|
||||||
facing: "environment" // or user
|
facing: "environment" // or user
|
||||||
|
},
|
||||||
|
area: {
|
||||||
|
top: "0%",
|
||||||
|
right: "0%",
|
||||||
|
left: "0%",
|
||||||
|
bottom: "0%"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
tracking: false,
|
tracking: false,
|
||||||
|
|||||||
+52
-8
@@ -647,20 +647,64 @@ define(['cluster', 'glMatrixAddon', "array_helper"], function(Cluster2, glMatrix
|
|||||||
optimalPatchSize = findPatchSizeForDivisors(common);
|
optimalPatchSize = findPatchSizeForDivisors(common);
|
||||||
if (!optimalPatchSize) {
|
if (!optimalPatchSize) {
|
||||||
optimalPatchSize = findPatchSizeForDivisors(this._computeDivisors(wideSide));
|
optimalPatchSize = findPatchSizeForDivisors(this._computeDivisors(wideSide));
|
||||||
|
if (!optimalPatchSize) {
|
||||||
|
optimalPatchSize = findPatchSizeForDivisors((this._computeDivisors(desiredPatchSize * nrOfPatches)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return optimalPatchSize;
|
return optimalPatchSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
function AdjustToSizeError(message, desiredPatchSize) {
|
CVUtils._parseCSSDimensionValues = function(value) {
|
||||||
this.name = 'AdjustToSizeError';
|
var dimension = {
|
||||||
this.message = message || 'AdjustToSizeError';
|
value: parseFloat(value),
|
||||||
this.patchSize = desiredPatchSize;
|
unit: value.indexOf("%") === value.length-1 ? "%" : "%"
|
||||||
}
|
};
|
||||||
|
|
||||||
AdjustToSizeError.prototype = Object.create(RangeError.prototype);
|
return dimension;
|
||||||
AdjustToSizeError.prototype.constructor = AdjustToSizeError;
|
};
|
||||||
|
|
||||||
CVUtils.AdjustToSizeError = AdjustToSizeError;
|
CVUtils._dimensionsConverters = {
|
||||||
|
top: function(dimension, context) {
|
||||||
|
if (dimension.unit === "%") {
|
||||||
|
return Math.floor(context.height * (dimension.value / 100));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
right: function(dimension, context) {
|
||||||
|
if (dimension.unit === "%") {
|
||||||
|
return Math.floor(context.width - (context.width * (dimension.value / 100)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
bottom: function(dimension, context) {
|
||||||
|
if (dimension.unit === "%") {
|
||||||
|
return Math.floor(context.height - (context.height * (dimension.value / 100)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
left: function(dimension, context) {
|
||||||
|
if (dimension.unit === "%") {
|
||||||
|
return Math.floor(context.width * (dimension.value / 100));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
CVUtils.computeImageArea = function(inputWidth, inputHeight, area) {
|
||||||
|
var context = {width: inputWidth, height: inputHeight};
|
||||||
|
|
||||||
|
var parsedArea = Object.keys(area).reduce(function(result, key) {
|
||||||
|
var value = area[key],
|
||||||
|
parsed = CVUtils._parseCSSDimensionValues(value),
|
||||||
|
calculated = CVUtils._dimensionsConverters[key](parsed, context);
|
||||||
|
|
||||||
|
result[key] = calculated;
|
||||||
|
return result;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
return {
|
||||||
|
sx: parsedArea.left,
|
||||||
|
sy: parsedArea.top,
|
||||||
|
sw: parsedArea.right - parsedArea.left,
|
||||||
|
sh: parsedArea.bottom - parsedArea.top
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
return (CVUtils);
|
return (CVUtils);
|
||||||
});
|
});
|
||||||
|
|||||||
+16
-19
@@ -10,29 +10,26 @@ define(["cv_utils"], function(CVUtils) {
|
|||||||
var _that = {},
|
var _that = {},
|
||||||
_streamConfig = inputStream.getConfig(),
|
_streamConfig = inputStream.getConfig(),
|
||||||
_video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight()),
|
_video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight()),
|
||||||
_size =_streamConfig.size ? CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight()) : _video_size,
|
_canvasSize = inputStream.getCanvasSize(),
|
||||||
_sx = 0,
|
_size = CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight()),
|
||||||
_sy = 0,
|
topRight = inputStream.getTopRight(),
|
||||||
_dx = 0,
|
_sx = topRight.x,
|
||||||
_dy = 0,
|
_sy = topRight.y,
|
||||||
_sWidth,
|
_canvas,
|
||||||
_dWidth,
|
|
||||||
_sHeight,
|
|
||||||
_dHeight,
|
|
||||||
_canvas = null,
|
|
||||||
_ctx = null,
|
_ctx = null,
|
||||||
_data = null;
|
_data = null;
|
||||||
|
|
||||||
_sWidth = _video_size.x;
|
|
||||||
_dWidth = _size.x;
|
|
||||||
_sHeight = _video_size.y;
|
|
||||||
_dHeight = _size.y;
|
|
||||||
|
|
||||||
_canvas = canvas ? canvas : document.createElement("canvas");
|
_canvas = canvas ? canvas : document.createElement("canvas");
|
||||||
_canvas.width = _size.x;
|
_canvas.width = _canvasSize.x;
|
||||||
_canvas.height = _size.y;
|
_canvas.height = _canvasSize.y;
|
||||||
_ctx = _canvas.getContext("2d");
|
_ctx = _canvas.getContext("2d");
|
||||||
_data = new Uint8Array(_size.x * _size.y);
|
_data = new Uint8Array(_size.x * _size.y);
|
||||||
|
console.log("FrameGrabber", {
|
||||||
|
size: _size,
|
||||||
|
topRight: topRight,
|
||||||
|
videoSize: _video_size,
|
||||||
|
canvasSize: _canvasSize
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses the given array as frame-buffer
|
* Uses the given array as frame-buffer
|
||||||
@@ -57,8 +54,8 @@ define(["cv_utils"], function(CVUtils) {
|
|||||||
frame = inputStream.getFrame(),
|
frame = inputStream.getFrame(),
|
||||||
ctxData;
|
ctxData;
|
||||||
if (frame) {
|
if (frame) {
|
||||||
_ctx.drawImage(frame, _sx, _sy, _sWidth, _sHeight, _dx, _dy, _dWidth, _dHeight);
|
_ctx.drawImage(frame, 0, 0, _canvasSize.x, _canvasSize.y);
|
||||||
ctxData = _ctx.getImageData(0, 0, _size.x, _size.y).data;
|
ctxData = _ctx.getImageData(_sx, _sy, _size.x, _size.y).data;
|
||||||
if(doHalfSample){
|
if(doHalfSample){
|
||||||
CVUtils.grayAndHalfSampleFromCanvasData(ctxData, _size, _data);
|
CVUtils.grayAndHalfSampleFromCanvasData(ctxData, _size, _data);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+47
-2
@@ -11,7 +11,9 @@ define(["image_loader"], function(ImageLoader) {
|
|||||||
_eventNames = ['canrecord', 'ended'],
|
_eventNames = ['canrecord', 'ended'],
|
||||||
_eventHandlers = {},
|
_eventHandlers = {},
|
||||||
_calculatedWidth,
|
_calculatedWidth,
|
||||||
_calculatedHeight;
|
_calculatedHeight,
|
||||||
|
_topRight = {x: 0, y: 0},
|
||||||
|
_canvasSize = {x: 0, y: 0};
|
||||||
|
|
||||||
function initSize() {
|
function initSize() {
|
||||||
var width = video.videoWidth,
|
var width = video.videoWidth,
|
||||||
@@ -19,6 +21,9 @@ define(["image_loader"], function(ImageLoader) {
|
|||||||
|
|
||||||
_calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
|
_calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
|
||||||
_calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
|
_calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
|
||||||
|
|
||||||
|
_canvasSize.x = _calculatedWidth;
|
||||||
|
_canvasSize.y = _calculatedHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
that.getRealWidth = function() {
|
that.getRealWidth = function() {
|
||||||
@@ -111,6 +116,24 @@ define(["image_loader"], function(ImageLoader) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
that.setTopRight = function(topRight) {
|
||||||
|
_topRight.x = topRight.x;
|
||||||
|
_topRight.y = topRight.y;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getTopRight = function() {
|
||||||
|
return _topRight;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.setCanvasSize = function(size) {
|
||||||
|
_canvasSize.x = size.x;
|
||||||
|
_canvasSize.y = size.y;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getCanvasSize = function() {
|
||||||
|
return _canvasSize;
|
||||||
|
};
|
||||||
|
|
||||||
that.getFrame = function() {
|
that.getFrame = function() {
|
||||||
return video;
|
return video;
|
||||||
};
|
};
|
||||||
@@ -146,7 +169,9 @@ define(["image_loader"], function(ImageLoader) {
|
|||||||
calculatedWidth,
|
calculatedWidth,
|
||||||
calculatedHeight,
|
calculatedHeight,
|
||||||
_eventNames = ['canrecord', 'ended'],
|
_eventNames = ['canrecord', 'ended'],
|
||||||
_eventHandlers = {};
|
_eventHandlers = {},
|
||||||
|
_topRight = {x: 0, y: 0},
|
||||||
|
_canvasSize = {x: 0, y: 0};
|
||||||
|
|
||||||
function loadImages() {
|
function loadImages() {
|
||||||
loaded = false;
|
loaded = false;
|
||||||
@@ -156,6 +181,8 @@ define(["image_loader"], function(ImageLoader) {
|
|||||||
height = imgs[0].height;
|
height = imgs[0].height;
|
||||||
calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
|
calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
|
||||||
calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
|
calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
|
||||||
|
_canvasSize.x = calculatedWidth;
|
||||||
|
_canvasSize.y = calculatedHeight;
|
||||||
loaded = true;
|
loaded = true;
|
||||||
frameIdx = 0;
|
frameIdx = 0;
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
@@ -245,6 +272,24 @@ define(["image_loader"], function(ImageLoader) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
that.setTopRight = function(topRight) {
|
||||||
|
_topRight.x = topRight.x;
|
||||||
|
_topRight.y = topRight.y;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getTopRight = function() {
|
||||||
|
return _topRight;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.setCanvasSize = function(size) {
|
||||||
|
_canvasSize.x = size.x;
|
||||||
|
_canvasSize.y = size.y;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getCanvasSize = function() {
|
||||||
|
return _canvasSize;
|
||||||
|
};
|
||||||
|
|
||||||
that.getFrame = function() {
|
that.getFrame = function() {
|
||||||
var frame;
|
var frame;
|
||||||
|
|
||||||
|
|||||||
+54
-14
@@ -140,8 +140,8 @@ function(Code128Reader,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_canvasContainer.ctx.image = _canvasContainer.dom.image.getContext("2d");
|
_canvasContainer.ctx.image = _canvasContainer.dom.image.getContext("2d");
|
||||||
_canvasContainer.dom.image.width = _inputStream.getWidth();
|
_canvasContainer.dom.image.width = _inputStream.getCanvasSize().x;
|
||||||
_canvasContainer.dom.image.height = _inputStream.getHeight();
|
_canvasContainer.dom.image.height = _inputStream.getCanvasSize().y;
|
||||||
|
|
||||||
_canvasContainer.dom.overlay = document.querySelector("canvas.drawingBuffer");
|
_canvasContainer.dom.overlay = document.querySelector("canvas.drawingBuffer");
|
||||||
if (!_canvasContainer.dom.overlay) {
|
if (!_canvasContainer.dom.overlay) {
|
||||||
@@ -157,8 +157,8 @@ function(Code128Reader,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_canvasContainer.ctx.overlay = _canvasContainer.dom.overlay.getContext("2d");
|
_canvasContainer.ctx.overlay = _canvasContainer.dom.overlay.getContext("2d");
|
||||||
_canvasContainer.dom.overlay.width = _inputStream.getWidth();
|
_canvasContainer.dom.overlay.width = _inputStream.getCanvasSize().x;
|
||||||
_canvasContainer.dom.overlay.height = _inputStream.getHeight();
|
_canvasContainer.dom.overlay.height = _inputStream.getCanvasSize().y;
|
||||||
}
|
}
|
||||||
|
|
||||||
function initBuffers(imageWrapper) {
|
function initBuffers(imageWrapper) {
|
||||||
@@ -189,6 +189,53 @@ function(Code128Reader,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function transformResult(result) {
|
||||||
|
var topRight = _inputStream.getTopRight(),
|
||||||
|
xOffset = topRight.x,
|
||||||
|
yOffset = topRight.y,
|
||||||
|
i;
|
||||||
|
|
||||||
|
if (!result || (xOffset === 0 && yOffset === 0)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (result.line && result.line.length === 2) {
|
||||||
|
moveLine(result.line);
|
||||||
|
}
|
||||||
|
if (result.boxes && result.boxes.length > 0) {
|
||||||
|
for (i = 0; i < result.boxes.length; i++) {
|
||||||
|
moveBox(result.boxes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveBox(box) {
|
||||||
|
var corner = box.length;
|
||||||
|
|
||||||
|
while(corner--) {
|
||||||
|
box[corner][0] += xOffset;
|
||||||
|
box[corner][1] += yOffset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveLine(line) {
|
||||||
|
line[0].x += xOffset;
|
||||||
|
line[0].y += yOffset;
|
||||||
|
line[1].x += xOffset;
|
||||||
|
line[1].y += yOffset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function publishResult(result) {
|
||||||
|
if (_onUIThread) {
|
||||||
|
transformResult(result);
|
||||||
|
}
|
||||||
|
Events.publish("processed", result);
|
||||||
|
if (result && result.codeResult) {
|
||||||
|
Events.publish("detected", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function locateAndDecode() {
|
function locateAndDecode() {
|
||||||
var result,
|
var result,
|
||||||
boxes;
|
boxes;
|
||||||
@@ -198,14 +245,10 @@ function(Code128Reader,
|
|||||||
result = _decoder.decodeFromBoundingBoxes(boxes);
|
result = _decoder.decodeFromBoundingBoxes(boxes);
|
||||||
result = result || {};
|
result = result || {};
|
||||||
result.boxes = boxes;
|
result.boxes = boxes;
|
||||||
Events.publish("processed", result);
|
publishResult(result);
|
||||||
if (result && result.codeResult) {
|
|
||||||
Events.publish("detected", result);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Events.publish("processed");
|
publishResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function update() {
|
function update() {
|
||||||
@@ -289,10 +332,7 @@ function(Code128Reader,
|
|||||||
} else if (e.data.event === 'processed') {
|
} else if (e.data.event === 'processed') {
|
||||||
workerThread.imageData = new Uint8Array(e.data.imageData);
|
workerThread.imageData = new Uint8Array(e.data.imageData);
|
||||||
workerThread.busy = false;
|
workerThread.busy = false;
|
||||||
Events.publish("processed", e.data.result);
|
publishResult(e.data.result);
|
||||||
if (e.data.result && e.data.result.codeResult) {
|
|
||||||
Events.publish("detected", e.data.result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user