123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- $(function() {
- var App = {
- init : function() {
- if (sessionStorage.getItem('deviceId') !== null) {
- this.state.inputStream.constraints.deviceId = sessionStorage.getItem('deviceId');
- }
- Quagga.init(this.state, function(err) {
- if (err) {
- console.log(err);
- return;
- }
- App.attachListeners();
- App.checkCapabilities();
- Quagga.start();
- });
- },
- checkCapabilities: function() {
- var track = Quagga.CameraAccess.getActiveTrack();
- var capabilities = {};
- if (typeof track.getCapabilities === 'function') {
- capabilities = track.getCapabilities();
- }
- this.applySettingsVisibility('zoom', capabilities.zoom);
- this.applySettingsVisibility('torch', capabilities.torch);
- },
- updateOptionsForMediaRange: function(node, range) {
- console.log('updateOptionsForMediaRange', node, range);
- var NUM_STEPS = 6;
- var stepSize = (range.max - range.min) / NUM_STEPS;
- var option;
- var value;
- while (node.firstChild) {
- node.removeChild(node.firstChild);
- }
- for (var i = 0; i <= NUM_STEPS; i++) {
- value = range.min + (stepSize * i);
- option = document.createElement('option');
- option.value = value;
- option.innerHTML = value;
- node.appendChild(option);
- }
- },
- applySettingsVisibility: function(setting, capability) {
- // depending on type of capability
- if (typeof capability === 'boolean') {
- var node = document.querySelector('input[name="settings_' + setting + '"]');
- if (node) {
- node.parentNode.style.display = capability ? 'block' : 'none';
- }
- return;
- }
- if (window.MediaSettingsRange && capability instanceof window.MediaSettingsRange) {
- var node = document.querySelector('select[name="settings_' + setting + '"]');
- if (node) {
- this.updateOptionsForMediaRange(node, capability);
- node.parentNode.style.display = 'block';
- }
- return;
- }
- },
- initCameraSelection: function(){
- var streamLabel = Quagga.CameraAccess.getActiveStreamLabel();
- return Quagga.CameraAccess.enumerateVideoDevices()
- .then(function(devices) {
- function pruneText(text) {
- return text.length > 30 ? text.substr(0, 30) : text;
- }
- var $deviceSelection = document.getElementById("deviceSelection");
- while ($deviceSelection.firstChild) {
- $deviceSelection.removeChild($deviceSelection.firstChild);
- }
- devices.forEach(function(device) {
- var $option = document.createElement("option");
- $option.value = device.deviceId || device.id;
- $option.appendChild(document.createTextNode(pruneText(device.label || device.deviceId || device.id)));
- $option.selected = streamLabel === device.label;
- $deviceSelection.appendChild($option);
- });
- });
- },
- attachListeners: function() {
- var self = this;
- self.initCameraSelection();
- $(".controls").on("click", "button.stop", function(e) {
- e.preventDefault();
- Quagga.stop();
- });
- $(".controls .reader-config-group").on("change", "input, select", function(e) {
- e.preventDefault();
- var $target = $(e.target),
- value = $target.attr("type") === "checkbox" ? $target.prop("checked") : $target.val(),
- name = $target.attr("name"),
- state = self._convertNameToState(name);
- sessionStorage.setItem('deviceId', value);
- console.log("Value of "+ state + " changed to " + value);
- self.setState(state, value);
- });
- $("#snapshot").on("click", function(e) {
- e.preventDefault();
- var code = $("#manual_code").val();
- if (self.addCode(code)) {
- $("#manual_code").val("");
- }
- });
- $("#add_bag").on("click", function(e) {
- e.preventDefault();
- var code = "123-456";
- if (self.addCode(code)) {
- $("input[name='123-456']").val("1");
- }
- });
- $("#submit_button").on("click", function(e) {
- $(".price input").each(function() {
- if ($(this).val() <= 0 || $(this).val() == "") {
- e.preventDefault();
- $(this).focus();
- }
- });
- });
- },
- _accessByPath: function(obj, path, val) {
- var parts = path.split('.'),
- depth = parts.length,
- setter = (typeof val !== "undefined") ? true : false;
- return parts.reduce(function(o, key, i) {
- if (setter && (i + 1) === depth) {
- if (typeof o[key] === "object" && typeof val === "object") {
- Object.assign(o[key], val);
- } else {
- o[key] = val;
- }
- }
- return key in o ? o[key] : {};
- }, obj);
- },
- _convertNameToState: function(name) {
- return name.replace("_", ".").split("-").reduce(function(result, value) {
- return result + value.charAt(0).toUpperCase() + value.substring(1);
- });
- },
- detachListeners: function() {
- $(".controls").off("click", "button.stop");
- $(".controls .reader-config-group").off("change", "input, select");
- },
- applySetting: function(setting, value) {
- var track = Quagga.CameraAccess.getActiveTrack();
- if (track && typeof track.getCapabilities === 'function') {
- switch (setting) {
- case 'zoom':
- return track.applyConstraints({advanced: [{zoom: parseFloat(value)}]});
- case 'torch':
- return track.applyConstraints({advanced: [{torch: !!value}]});
- }
- }
- },
- setState: function(path, value) {
- var self = this;
- if (typeof self._accessByPath(self.inputMapper, path) === "function") {
- value = self._accessByPath(self.inputMapper, path)(value);
- }
- if (path.startsWith('settings.')) {
- var setting = path.substring(9);
- return self.applySetting(setting, value);
- }
- self._accessByPath(self.state, path, value);
- console.log(JSON.stringify(self.state));
- App.detachListeners();
- Quagga.stop();
- App.init();
- },
- addCode: function(code) {
- if (code.match(/^\d\d\d\.\d\d\d$/)) {
- code = code.replace('.', '-');
- }
- if (!App.results.includes(code) && code.match(/^\d\d\d\-\d\d\d$/)) {
- App.results.push(code);
- var $node = null, canvas = Quagga.canvas.dom.image;
-
- $node = $('<li><div class="thumbnail">' +
- '<div class="close"><button type="button" class="btn-close" aria-label="entfernen" tabindex="-1"></button></div>' +
- '<div class="imgWrapper"><img /></div>' +
- '<div class="caption"><h4 class="code"></h4></div>' +
- '<div class="price"><input type="number" size="5" step="0.1"/></div>' +
- '</div></li>');
- $node.find("img").attr("src", canvas.toDataURL());
- $node.find("h4.code").html(code);
- $node.find("input").attr("name", code);
- $("#result_strip ul.thumbnails").append($node);
- $node.find("input").focus();
- $(".btn-close").on("click", function(e) {
- e.preventDefault();
- e.target.parentNode.parentNode.parentNode.remove();
- App.results = $(".price input").map(function () { return this.name; }).get();
- console.log(App.results);
- });
- return true;
- }
- return false;
- },
- inputMapper: {
- inputStream: {
- constraints: function(value){
- if (/^(\d+)x(\d+)$/.test(value)) {
- var values = value.split('x');
- return {
- width: {min: parseInt(values[0])},
- height: {min: parseInt(values[1])}
- };
- }
- return {
- deviceId: value
- };
- }
- },
- numOfWorkers: function(value) {
- return parseInt(value);
- },
- decoder: {
- readers: function(value) {
- if (value === 'ean_extended') {
- return [{
- format: "ean_reader",
- config: {
- supplements: [
- 'ean_5_reader', 'ean_2_reader'
- ]
- }
- }];
- }
- return [{
- format: value + "_reader",
- config: {}
- }];
- }
- }
- },
- state: {
- inputStream: {
- type : "LiveStream",
- constraints: {
- width: 640, //{min: 640, max: 640},
- height: 480, // {min: 480, max: 480},
- aspectRatio: 1, // min: 1, max: 50,
- facingMode: "environment" // "environment" // or user
- }
- },
- locator: {
- patchSize: "medium",
- halfSample: true
- },
- numOfWorkers: 2,
- frequency: 10,
- decoder: {
- readers : [{
- format: "code_128_reader",
- config: {}
- }]
- },
- locate: true
- },
- lastResult : null,
- results: []
- };
- App.init();
- Quagga.onProcessed(function(result) {
- var drawingCtx = Quagga.canvas.ctx.overlay,
- drawingCanvas = Quagga.canvas.dom.overlay;
- if (result) {
- if (result.boxes) {
- drawingCtx.clearRect(0, 0, parseInt(drawingCanvas.getAttribute("width")), parseInt(drawingCanvas.getAttribute("height")));
- result.boxes.filter(function (box) {
- return box !== result.box;
- }).forEach(function (box) {
- Quagga.ImageDebug.drawPath(box, {x: 0, y: 1}, drawingCtx, {color: "green", lineWidth: 2});
- });
- }
- if (result.box) {
- Quagga.ImageDebug.drawPath(result.box, {x: 0, y: 1}, drawingCtx, {color: "#00F", lineWidth: 2});
- }
- if (result.codeResult && result.codeResult.code) {
- Quagga.ImageDebug.drawPath(result.line, {x: 'x', y: 'y'}, drawingCtx, {color: 'red', lineWidth: 3});
- }
- }
- });
- Quagga.onDetected(function(result) {
- App.addCode(result.codeResult.code)
- });
- });
|