From b799a154b09aa3f480e0d1403144f9718ccd96ae Mon Sep 17 00:00:00 2001 From: Christoph Oberhofer Date: Wed, 5 Apr 2017 22:00:14 +0200 Subject: [PATCH] Added scope to Soure to determine if it was created by quagga or not --- src/input/CameraSource.js | 24 ++++++++++++++++-------- src/input/ImageSource.js | 8 +++----- src/input/Source.js | 1 + src/input/SourceInterface.js | 29 +++++++++++++++++++++++++++++ src/input/SourceScope.js | 4 ++++ src/quagga.js | 5 ++++- src/scanner.js | 6 +++--- 7 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 src/input/SourceInterface.js create mode 100644 src/input/SourceScope.js diff --git a/src/input/CameraSource.js b/src/input/CameraSource.js index c5d7ff8..7661771 100644 --- a/src/input/CameraSource.js +++ b/src/input/CameraSource.js @@ -1,7 +1,9 @@ import {clone} from 'lodash'; -import {determineOrientation, PORTRAIT} from '../common/device'; +import {determineOrientation} from '../common/device'; import CameraAccess from './camera_access'; import {getViewport} from '../common/utils'; +import {generateSourceInterface} from './SourceInterface'; +import {Scope} from './input/SourceScope'; const ConstraintPresets = [ { @@ -151,14 +153,14 @@ function adjustWithZoom(videoConstraints) { }; } -export function fromCamera(constraints, {target} = {}) { +export function fromCamera(constraints, {target, scope = Scope.EXTERNAL} = {}) { let {video: videoConstraints, zoom} = adjustWithZoom(constraints); const video = getOrCreateVideo(target); return CameraAccess.request(video, videoConstraints) .then((mediastream) => { let track = mediastream.getVideoTracks()[0]; - return { + return Object.assign(generateSourceInterface(), { type: "CAMERA", getDimensions() { const viewport = { @@ -182,13 +184,13 @@ export function fromCamera(constraints, {target} = {}) { }, }; }, - getConstraints: function() { + getConstraints() { return videoConstraints; }, - getDrawable: function() { + getDrawable() { return video; }, - applyConstraints: function(newConstraints) { + applyConstraints(newConstraints) { track.stop(); constraints = newConstraints; const adjustment = adjustWithZoom(constraints); @@ -200,9 +202,15 @@ export function fromCamera(constraints, {target} = {}) { track = mediastream.getVideoTracks()[0]; }); }, - getLabel: function() { + getLabel() { return track.label; + }, + stop() { + track.stop(); + }, + getScope() { + return scope; } - }; + }); }); } diff --git a/src/input/ImageSource.js b/src/input/ImageSource.js index 76b0346..d24feb1 100644 --- a/src/input/ImageSource.js +++ b/src/input/ImageSource.js @@ -1,4 +1,5 @@ import {findTagsInObjectURL} from './exif_helper'; +import {generateSourceInterface} from './SourceInterface'; export function fromImage(input, constraints = {width: 800, height: 800, channels: 3}) { var $image = null; @@ -49,7 +50,7 @@ export function fromImage(input, constraints = {width: 800, height: 800, channel const calculatedHeight = imageAR > 1 ? Math.floor((1 / imageAR) * constraints.width) : constraints.width; const colorChannels = constraints.channels || 3; - return { + return Object.assign(generateSourceInterface(), { type: "IMAGE", colorChannels, tags, @@ -76,9 +77,6 @@ export function fromImage(input, constraints = {width: 800, height: 800, channel getConstraints: function() { return constraints; }, - applyConstraints: function() { - console.log('ImageSource.applyConstraints not implemented'); - }, - }; + }); }); } diff --git a/src/input/Source.js b/src/input/Source.js index 7efb38e..ea29c2b 100644 --- a/src/input/Source.js +++ b/src/input/Source.js @@ -1,5 +1,6 @@ export * from './ImageSource'; export * from './CameraSource'; +export * from './SourceScope'; export function fromCanvas(input) { var $canvas = null; diff --git a/src/input/SourceInterface.js b/src/input/SourceInterface.js new file mode 100644 index 0000000..bc6ff7b --- /dev/null +++ b/src/input/SourceInterface.js @@ -0,0 +1,29 @@ +const viewport = { + x: 0, + y: 0, + width: 0, + height: 0, +}; + +const canvas = { + height: 0, + width: 0, +}; + +export function generateSourceInterface() { + return { + type: "INTERFACE", + getDimensions() { + return { + viewport, + canvas, + }; + }, + getConstraints() {}, + getDrawable() {}, + applyConstraints() {}, + getLabel() {}, + stop() {}, + getScope() {}, + }; +} diff --git a/src/input/SourceScope.js b/src/input/SourceScope.js new file mode 100644 index 0000000..9674773 --- /dev/null +++ b/src/input/SourceScope.js @@ -0,0 +1,4 @@ +export const Scope = { + INTERNAL: "INTERNAL", + EXTERNAL: 'EXTERNAL', +}; diff --git a/src/quagga.js b/src/quagga.js index cd8e889..e401f65 100644 --- a/src/quagga.js +++ b/src/quagga.js @@ -143,7 +143,10 @@ function createApi() { fromCamera(options) { const config = merge({}, Config, options); return Source - .fromCamera(config.constraints, {target: config.target}) + .fromCamera(config.constraints, { + target: config.target, + scope: Source.Scope.INTERNAL, + }) .then(fromSource.bind(null, config)); }, fromSource(src, inputConfig) { diff --git a/src/scanner.js b/src/scanner.js index fac474f..c3d3a9a 100644 --- a/src/scanner.js +++ b/src/scanner.js @@ -6,8 +6,8 @@ import BarcodeDecoder from './decoder/barcode_decoder'; import createEventedElement from './common/events'; import {release, aquire, releaseAll} from './common/buffers'; import Config from './config/config'; -import CameraAccess from './input/camera_access'; import {getViewport} from './common/utils'; +import {Scope} from './input/SourceScope'; const vec2 = { clone: require('gl-vec2/clone') @@ -438,8 +438,8 @@ function createScanner(pixelCapturer) { _stopped = true; adjustWorkerPool(0); releaseAll(); - if (source.type === "CAMERA") { - CameraAccess.release(); + if (source.getScope() === Scope.INTERNAL) { + source.stop(); } }, applyConfig(newConfig) {