mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
Moved DOM objects to helper module
This commit is contained in:
@@ -4,11 +4,11 @@ var App = {
|
|||||||
init: function() {
|
init: function() {
|
||||||
this.attachListeners();
|
this.attachListeners();
|
||||||
},
|
},
|
||||||
decode: function(src) {
|
decode: function(file) {
|
||||||
Quagga
|
Quagga
|
||||||
.decoder({readers: ['ean_reader']})
|
.decoder({readers: ['ean_reader']})
|
||||||
.locator({patchSize: 'medium'})
|
.locator({patchSize: 'medium'})
|
||||||
.fromImage(src, {size: 800})
|
.fromSource(file, {size: 800})
|
||||||
.toPromise()
|
.toPromise()
|
||||||
.then(function(result) {
|
.then(function(result) {
|
||||||
document.querySelector('input.isbn').value = result.codeResult.code;
|
document.querySelector('input.isbn').value = result.codeResult.code;
|
||||||
@@ -35,7 +35,7 @@ var App = {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
fileInput.removeEventListener("change", onChange);
|
fileInput.removeEventListener("change", onChange);
|
||||||
if (e.target.files && e.target.files.length) {
|
if (e.target.files && e.target.files.length) {
|
||||||
self.decode(URL.createObjectURL(e.target.files[0]));
|
self.decode(e.target.files[0]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ var App = {
|
|||||||
this._scanner = Quagga
|
this._scanner = Quagga
|
||||||
.decoder({readers: ['ean_reader']})
|
.decoder({readers: ['ean_reader']})
|
||||||
.locator({patchSize: 'medium'})
|
.locator({patchSize: 'medium'})
|
||||||
.fromVideo({
|
.fromSource({
|
||||||
target: selector,
|
target: selector,
|
||||||
constraints: {
|
constraints: {
|
||||||
width: 800,
|
width: 800,
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
const hasWindow = typeof window !== 'undefined';
|
||||||
|
const windowRef = hasWindow ? window : {};
|
||||||
|
|
||||||
|
const windowObjects = [
|
||||||
|
"MediaStream",
|
||||||
|
"HTMLImageElement",
|
||||||
|
"HTMLVideoElement",
|
||||||
|
"HTMLCanvasElement",
|
||||||
|
"FileList",
|
||||||
|
"File",
|
||||||
|
"URL"
|
||||||
|
];
|
||||||
|
|
||||||
|
const DOMHelper = windowObjects.reduce((result, obj) => {
|
||||||
|
return {
|
||||||
|
...result,
|
||||||
|
[obj]: obj in windowRef ? windowRef[obj] : () => {}
|
||||||
|
};
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
export default DOMHelper;
|
||||||
@@ -1,29 +1,29 @@
|
|||||||
import {merge, pick, omitBy, isEmpty} from 'lodash';
|
import {merge, pick, omitBy, isEmpty} from 'lodash';
|
||||||
|
|
||||||
|
import DOMHelper from '../common/dom_helper';
|
||||||
|
|
||||||
const isDataURL = {regex: /^\s*data:([a-z]+\/[a-z0-9\-\+]+(;[a-z\-]+\=[a-z0-9\-]+)?)?(;base64)?,[a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i}, // eslint-disable-line max-len
|
const isDataURL = {regex: /^\s*data:([a-z]+\/[a-z0-9\-\+]+(;[a-z\-]+\=[a-z0-9\-]+)?)?(;base64)?,[a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i}, // eslint-disable-line max-len
|
||||||
isBlobURL = {regex: /^\s*blob:(.*)$/i},
|
isBlobURL = {regex: /^\s*blob:(.*)$/i},
|
||||||
isMediaURL = {regex: /^(?:(?:http[s]?|ftp):\/)?\/?(?:(?:[^:\/\s]+)(?:(?:\/\w+)*\/))?([\w\-]+\.([^#?\s]+))(?:.*)?(?:#[\w\-]+)?$/i}, // eslint-disable-line max-len
|
isMediaURL = {regex: /^(?:(?:http[s]?|ftp):\/)?\/?(?:(?:[^:\/\s]+)(?:(?:\/\w+)*\/))?([\w\-]+\.([^#?\s]+))(?:.*)?(?:#[\w\-]+)?$/i}, // eslint-disable-line max-len
|
||||||
isImageExt = {regex: /(jpe?g|png|gif|tiff)(?:\s+|$)/i},
|
isImageExt = {regex: /(jpe?g|png|gif|tiff)(?:\s+|$)/i},
|
||||||
isVideoExt = {regex: /(webm|ogg|mp4|m4v)/i};
|
isVideoExt = {regex: /(webm|ogg|mp4|m4v)/i};
|
||||||
|
|
||||||
const MediaStream = "MediaStream" in window ? window.MediaStream : function(){};
|
|
||||||
|
|
||||||
export function createConfigFromSource(config, sourceConfig, source) {
|
export function createConfigFromSource(config, sourceConfig, source) {
|
||||||
if (source instanceof MediaStream) {
|
if (source instanceof DOMHelper.MediaStream) {
|
||||||
return createConfigForStream(config, sourceConfig, {srcObject: source});
|
return createConfigForStream(config, sourceConfig, {srcObject: source});
|
||||||
} else if (source instanceof HTMLImageElement) {
|
} else if (source instanceof DOMHelper.HTMLImageElement) {
|
||||||
throw new Error('Source "HTMLImageElement": not yet supported');
|
throw new Error('Source "HTMLImageElement": not yet supported');
|
||||||
// return createConfigForImage(config, inputConfig, {image: source});
|
// return createConfigForImage(config, inputConfig, {image: source});
|
||||||
} else if (source instanceof HTMLVideoElement) {
|
} else if (source instanceof DOMHelper.HTMLVideoElement) {
|
||||||
throw new Error('Source "HTMLVideoElement": not yet supported');
|
throw new Error('Source "HTMLVideoElement": not yet supported');
|
||||||
// return createConfigForVideo(config, inputConfig, {video: source});
|
// return createConfigForVideo(config, inputConfig, {video: source});
|
||||||
} else if (source instanceof HTMLCanvasElement) {
|
} else if (source instanceof DOMHelper.HTMLCanvasElement) {
|
||||||
return createConfigForCanvas(config, sourceConfig, {canvas: source});
|
return createConfigForCanvas(config, sourceConfig, {canvas: source});
|
||||||
} else if (source instanceof FileList) {
|
} else if (source instanceof DOMHelper.FileList) {
|
||||||
if (source.length > 0) {
|
if (source.length > 0) {
|
||||||
return createConfigForFile(config, sourceConfig, source[0]);
|
return createConfigForFile(config, sourceConfig, source[0]);
|
||||||
}
|
}
|
||||||
} else if (source instanceof File) {
|
} else if (source instanceof DOMHelper.File) {
|
||||||
return createConfigForFile(config, sourceConfig, source);
|
return createConfigForFile(config, sourceConfig, source);
|
||||||
} else if (typeof source === 'string') {
|
} else if (typeof source === 'string') {
|
||||||
return createConfigForString(config, sourceConfig, source);
|
return createConfigForString(config, sourceConfig, source);
|
||||||
@@ -64,7 +64,7 @@ function createConfigForMimeType(config, inputConfig, {src, mime}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createConfigForFile(config, inputConfig, file) {
|
function createConfigForFile(config, inputConfig, file) {
|
||||||
const src = window.URL.createObjectURL(file);
|
const src = DOMHelper.URL.createObjectURL(file);
|
||||||
return createConfigForMimeType(config, inputConfig, {
|
return createConfigForMimeType(config, inputConfig, {
|
||||||
src,
|
src,
|
||||||
mime: file.type
|
mime: file.type
|
||||||
|
|||||||
Reference in New Issue
Block a user