diff --git a/example/file-input/index.html b/example/file-input/index.html index 8513aba..c5c4de2 100644 --- a/example/file-input/index.html +++ b/example/file-input/index.html @@ -39,7 +39,6 @@

@@ -53,21 +52,23 @@ var App = { init: function() { this.attachListeners(); }, - decode: function(src) { - Quagga - .decoder({readers: ['ean_reader']}) - .locator({patchSize: 'medium'}) - .fromImage(src, {size: 800}) - .toPromise() - .then(function(result) { - document.querySelector('input.isbn').value = result.codeResult.code; - }) - .catch(function() { - document.querySelector('input.isbn').value = "Not Found"; - }) - .then(function() { - this.attachListeners(); - }.bind(this)); + decode: function(file) { + Quagga.fromImage({ + constraints: {src: file, width: 800, height: 800}, + decoder: {readers: ['ean_reader']}, + }) + .then(function(scanner) { + return scanner.detect(); + }) + .then(function(result) { + document.querySelector('input.isbn').value = result.codeResult.code; + }) + .catch(function() { + document.querySelector('input.isbn').value = "Not Found"; + }) + .then(function() { + this.attachListeners(); + }.bind(this)); }, attachListeners: function() { var self = this, @@ -84,7 +85,7 @@ var App = { e.preventDefault(); fileInput.removeEventListener("change", onChange); if (e.target.files && e.target.files.length) { - self.decode(URL.createObjectURL(e.target.files[0])); + self.decode(e.target.files[0]); } }); } diff --git a/example/file-input/index.js b/example/file-input/index.js index a8acf52..07e20a9 100644 --- a/example/file-input/index.js +++ b/example/file-input/index.js @@ -5,20 +5,23 @@ var App = { this.attachListeners(); }, decode: function(file) { - Quagga - .decoder({readers: ['ean_reader']}) - .locator({patchSize: 'medium'}) - .fromSource(file, {size: 800}) - .toPromise() - .then(function(result) { - document.querySelector('input.isbn').value = result.codeResult.code; - }) - .catch(function() { - document.querySelector('input.isbn').value = "Not Found"; - }) - .then(function() { - this.attachListeners(); - }.bind(this)); + Quagga.fromImage({ + constraints: {src: file, width: 800, height: 800}, + decoder: {readers: ['ean_reader']}, + }) + .then(function(scanner) { + return scanner.detect(); + }) + .then(function(result) { + document.querySelector('input.isbn').value = result.codeResult.code; + }) + .catch(function(e) { + console.error(e); + document.querySelector('input.isbn').value = "Not Found"; + }) + .then(function() { + this.attachListeners(); + }.bind(this)); }, attachListeners: function() { var self = this, diff --git a/src/input/PixelCapture.js b/src/input/PixelCapture.js index e0afe7d..1c20531 100644 --- a/src/input/PixelCapture.js +++ b/src/input/PixelCapture.js @@ -32,7 +32,11 @@ function adjustCanvasSize(input, canvas) { function getOrCreateCanvas(source, target) { const $viewport = getViewport(target); - let $canvas = $viewport.querySelector("canvas.imgBuffer"); + let $canvas = null; + if ($viewport) { + $canvas = $viewport.querySelector("canvas.imgBuffer"); + } + if (!$canvas) { $canvas = document.createElement("canvas"); $canvas.className = "imgBuffer"; diff --git a/src/scanner.js b/src/scanner.js index 669c9d2..6c709c8 100644 --- a/src/scanner.js +++ b/src/scanner.js @@ -47,16 +47,20 @@ function createScanner(pixelCapturer) { const source = pixelCapturer ? pixelCapturer.getSource() : {}; function updateViewportStyle(target) { - const $video = source.getDrawable(); + const $drawable = source.getDrawable(); const $viewport = getViewport(target); + if (!$viewport) { + return; + } + const {viewport} = source.getDimensions(); const zoom = Math.floor((((2 * viewport.x) + viewport.width) / viewport.width) * 100) / 100; const videoWidth = zoom * viewport.width; const translate = ((viewport.x / videoWidth) * (-100)).toFixed(5); - $video.style.width = `${zoom * 100}%`; - $video.style.transform = `translate(${translate}%, ${translate}%)`; + $drawable.style.width = `${zoom * 100}%`; + $drawable.style.transform = `translate(${translate}%, ${translate}%)`; $viewport.style.paddingBottom = `${(viewport.height * 100 / viewport.width).toFixed(5)}%`; $viewport.style.overflow = "hidden"; $viewport.style.height = 0;