Initial version for node.js
parent
89da5cc4e7
commit
a042422515
@ -0,0 +1,15 @@
|
|||||||
|
var Quagga = require('../lib/quagga');
|
||||||
|
|
||||||
|
Quagga.decodeSingle({
|
||||||
|
src: "../test/fixtures/code_128/image-001.jpg",
|
||||||
|
numOfWorkers: 0,
|
||||||
|
inputStream: {
|
||||||
|
size: 640
|
||||||
|
}
|
||||||
|
}, function(result) {
|
||||||
|
if(result.codeResult) {
|
||||||
|
console.log("result", result.codeResult.code);
|
||||||
|
} else {
|
||||||
|
console.log("not detected");
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,75 @@
|
|||||||
|
/* jshint undef: true, unused: true, browser:true, devel: true */
|
||||||
|
/* global define */
|
||||||
|
|
||||||
|
define(["cv_utils"], function(CVUtils) {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var FrameGrabber = {};
|
||||||
|
|
||||||
|
FrameGrabber.create = function(inputStream) {
|
||||||
|
var _that = {},
|
||||||
|
_streamConfig = inputStream.getConfig(),
|
||||||
|
_video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight()),
|
||||||
|
_size =_streamConfig.size ? CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight()) : _video_size,
|
||||||
|
_sx = 0,
|
||||||
|
_sy = 0,
|
||||||
|
_dx = 0,
|
||||||
|
_dy = 0,
|
||||||
|
_sWidth,
|
||||||
|
_dWidth,
|
||||||
|
_sHeight,
|
||||||
|
_dHeight,
|
||||||
|
_canvas = null,
|
||||||
|
_ctx = null,
|
||||||
|
_data = null;
|
||||||
|
|
||||||
|
_sWidth = _video_size.x;
|
||||||
|
_dWidth = _size.x;
|
||||||
|
_sHeight = _video_size.y;
|
||||||
|
_dHeight = _size.y;
|
||||||
|
|
||||||
|
_data = new Uint8Array(_size.x * _size.y);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses the given array as frame-buffer
|
||||||
|
*/
|
||||||
|
_that.attachData = function(data) {
|
||||||
|
_data = data;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the used frame-buffer
|
||||||
|
*/
|
||||||
|
_that.getData = function() {
|
||||||
|
return _data;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches a frame from the input-stream and puts into the frame-buffer.
|
||||||
|
* The image-data is converted to gray-scale and then half-sampled if configured.
|
||||||
|
*/
|
||||||
|
_that.grab = function() {
|
||||||
|
var doHalfSample = _streamConfig.halfSample,
|
||||||
|
frame = inputStream.getFrame();
|
||||||
|
|
||||||
|
if (frame) {
|
||||||
|
if(doHalfSample){
|
||||||
|
CVUtils.grayAndHalfSampleFromCanvasData(frame.data, _size, _data);
|
||||||
|
} else {
|
||||||
|
CVUtils.computeGray(frame.data, _data);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_that.getSize = function() {
|
||||||
|
return _size;
|
||||||
|
};
|
||||||
|
|
||||||
|
return _that;
|
||||||
|
};
|
||||||
|
|
||||||
|
return (FrameGrabber);
|
||||||
|
});
|
@ -0,0 +1,138 @@
|
|||||||
|
/* jshint undef: true, unused: true, browser:true, devel: true */
|
||||||
|
/* global define */
|
||||||
|
|
||||||
|
define(function() {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var GetPixels = require("get-pixels");
|
||||||
|
|
||||||
|
var InputStream = {};
|
||||||
|
|
||||||
|
InputStream.createImageStream = function() {
|
||||||
|
var that = {};
|
||||||
|
var _config = null;
|
||||||
|
|
||||||
|
var width = 0,
|
||||||
|
height = 0,
|
||||||
|
frameIdx = 0,
|
||||||
|
paused = true,
|
||||||
|
loaded = false,
|
||||||
|
frame = null,
|
||||||
|
baseUrl,
|
||||||
|
ended = false,
|
||||||
|
size,
|
||||||
|
calculatedWidth,
|
||||||
|
calculatedHeight,
|
||||||
|
_eventNames = ['canrecord', 'ended'],
|
||||||
|
_eventHandlers = {};
|
||||||
|
|
||||||
|
function loadImages() {
|
||||||
|
loaded = false;
|
||||||
|
GetPixels(baseUrl, function(err, pixels) {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
loaded = true;
|
||||||
|
console.log(pixels.shape);
|
||||||
|
frame = pixels;
|
||||||
|
width = pixels.shape[0];
|
||||||
|
height = pixels.shape[1];
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
publishEvent("canrecord", []);
|
||||||
|
}, 0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function publishEvent(eventName, args) {
|
||||||
|
var j,
|
||||||
|
handlers = _eventHandlers[eventName];
|
||||||
|
|
||||||
|
if (handlers && handlers.length > 0) {
|
||||||
|
for ( j = 0; j < handlers.length; j++) {
|
||||||
|
handlers[j].apply(that, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
that.trigger = publishEvent;
|
||||||
|
|
||||||
|
that.getWidth = function() {
|
||||||
|
return calculatedWidth;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getHeight = function() {
|
||||||
|
return calculatedHeight;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.setWidth = function(width) {
|
||||||
|
calculatedWidth = width;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.setHeight = function(height) {
|
||||||
|
calculatedHeight = height;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getRealWidth = function() {
|
||||||
|
return width;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getRealHeight = function() {
|
||||||
|
return height;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.setInputStream = function(stream) {
|
||||||
|
_config = stream;
|
||||||
|
baseUrl = _config.src;
|
||||||
|
size = 1;
|
||||||
|
loadImages();
|
||||||
|
};
|
||||||
|
|
||||||
|
that.ended = function() {
|
||||||
|
return ended;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.setAttribute = function() {};
|
||||||
|
|
||||||
|
that.getConfig = function() {
|
||||||
|
return _config;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.pause = function() {
|
||||||
|
paused = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.play = function() {
|
||||||
|
paused = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.setCurrentTime = function(time) {
|
||||||
|
frameIdx = time;
|
||||||
|
};
|
||||||
|
|
||||||
|
that.addEventListener = function(event, f) {
|
||||||
|
if (_eventNames.indexOf(event) !== -1) {
|
||||||
|
if (!_eventHandlers[event]) {
|
||||||
|
_eventHandlers[event] = [];
|
||||||
|
}
|
||||||
|
_eventHandlers[event].push(f);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
that.getFrame = function() {
|
||||||
|
if (!loaded){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return frame;
|
||||||
|
};
|
||||||
|
|
||||||
|
return that;
|
||||||
|
};
|
||||||
|
|
||||||
|
return (InputStream);
|
||||||
|
});
|
@ -0,0 +1,12 @@
|
|||||||
|
var requirejs = require('requirejs');
|
||||||
|
|
||||||
|
requirejs.config({
|
||||||
|
"baseUrl" : "../src",
|
||||||
|
"paths" : {
|
||||||
|
"typedefs" : "typedefs",
|
||||||
|
"input_stream": "../lib/input_stream",
|
||||||
|
"frame_grabber": "../lib/frame_grabber"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = requirejs('quagga');
|
Loading…
Reference in New Issue