|
|
@ -1,13 +1,12 @@
|
|
|
|
import jsQR, { QRCode, Options } from 'jsqr';
|
|
|
|
import { Options } from 'jsqr';
|
|
|
|
|
|
|
|
import jsQR from 'jsqr';
|
|
|
|
export type CodeResult = QRCode | null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const videoSize = {
|
|
|
|
const videoSize = {
|
|
|
|
width: { min: 360, ideal: 720, max: 1080 },
|
|
|
|
width: { min: 360, ideal: 720, max: 1080 },
|
|
|
|
height: { min: 360, ideal: 720, max: 1080 },
|
|
|
|
height: { min: 360, ideal: 720, max: 1080 },
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export class QrcodeDecoder {
|
|
|
|
class QrcodeDecoder {
|
|
|
|
timerCapture: null | NodeJS.Timeout;
|
|
|
|
timerCapture: null | NodeJS.Timeout;
|
|
|
|
canvasElem: null | HTMLCanvasElement;
|
|
|
|
canvasElem: null | HTMLCanvasElement;
|
|
|
|
gCtx: null | CanvasRenderingContext2D;
|
|
|
|
gCtx: null | CanvasRenderingContext2D;
|
|
|
@ -101,10 +100,7 @@ export class QrcodeDecoder {
|
|
|
|
* inversionAttempts - (attemptBoth (default), dontInvert, onlyInvert, or invertFirst)
|
|
|
|
* inversionAttempts - (attemptBoth (default), dontInvert, onlyInvert, or invertFirst)
|
|
|
|
* refer to jsqr options: https://github.com/cozmo/jsQR
|
|
|
|
* refer to jsqr options: https://github.com/cozmo/jsQR
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async _captureToCanvas(
|
|
|
|
async _captureToCanvas(videoElem: HTMLVideoElement, options: Options) {
|
|
|
|
videoElem: HTMLVideoElement,
|
|
|
|
|
|
|
|
options: Options,
|
|
|
|
|
|
|
|
): Promise<CodeResult> {
|
|
|
|
|
|
|
|
if (this.timerCapture) {
|
|
|
|
if (this.timerCapture) {
|
|
|
|
clearTimeout(this.timerCapture);
|
|
|
|
clearTimeout(this.timerCapture);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -146,7 +142,7 @@ export class QrcodeDecoder {
|
|
|
|
|
|
|
|
|
|
|
|
const result = await proms();
|
|
|
|
const result = await proms();
|
|
|
|
|
|
|
|
|
|
|
|
return result as CodeResult;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -159,10 +155,7 @@ export class QrcodeDecoder {
|
|
|
|
* inversionAttempts - (attemptBoth (default), dontInvert, onlyInvert, or invertFirst)
|
|
|
|
* inversionAttempts - (attemptBoth (default), dontInvert, onlyInvert, or invertFirst)
|
|
|
|
* refer to jsqr options: https://github.com/cozmo/jsQR
|
|
|
|
* refer to jsqr options: https://github.com/cozmo/jsQR
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async decodeFromCamera(
|
|
|
|
async decodeFromCamera(videoElem: HTMLVideoElement, options: any = {}) {
|
|
|
|
videoElem: HTMLVideoElement,
|
|
|
|
|
|
|
|
options: any = {},
|
|
|
|
|
|
|
|
): Promise<CodeResult> {
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
const opts = {
|
|
|
|
...this.defaultOption,
|
|
|
|
...this.defaultOption,
|
|
|
|
...options,
|
|
|
|
...options,
|
|
|
@ -204,8 +197,6 @@ export class QrcodeDecoder {
|
|
|
|
const code = await this.decodeFromVideo(videoElem, opts);
|
|
|
|
const code = await this.decodeFromVideo(videoElem, opts);
|
|
|
|
return code;
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -216,10 +207,7 @@ export class QrcodeDecoder {
|
|
|
|
* inversionAttempts - (attemptBoth (default), dontInvert, onlyInvert, or invertFirst)
|
|
|
|
* inversionAttempts - (attemptBoth (default), dontInvert, onlyInvert, or invertFirst)
|
|
|
|
* refer to jsqr options: https://github.com/cozmo/jsQR
|
|
|
|
* refer to jsqr options: https://github.com/cozmo/jsQR
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async decodeFromVideo(
|
|
|
|
async decodeFromVideo(videoElem: HTMLVideoElement, options = {}) {
|
|
|
|
videoElem: HTMLVideoElement,
|
|
|
|
|
|
|
|
options = {},
|
|
|
|
|
|
|
|
): Promise<CodeResult> {
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
const opts = {
|
|
|
|
...this.defaultOption,
|
|
|
|
...this.defaultOption,
|
|
|
|
...options,
|
|
|
|
...options,
|
|
|
@ -241,16 +229,20 @@ export class QrcodeDecoder {
|
|
|
|
* refer to jsqr options: https://github.com/cozmo/jsQR
|
|
|
|
* refer to jsqr options: https://github.com/cozmo/jsQR
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async decodeFromImage(
|
|
|
|
async decodeFromImage(
|
|
|
|
img: HTMLImageElement | string,
|
|
|
|
img: HTMLImageElement,
|
|
|
|
options: { crossOrigin?: string } = {},
|
|
|
|
options: { crossOrigin?: string } = {},
|
|
|
|
): Promise<CodeResult> {
|
|
|
|
) {
|
|
|
|
let imgDom: HTMLImageElement | null = null;
|
|
|
|
let imgDom: HTMLImageElement | null = null;
|
|
|
|
const opts = {
|
|
|
|
const opts = {
|
|
|
|
...this.defaultOption,
|
|
|
|
...this.defaultOption,
|
|
|
|
...options,
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
if (+img.nodeType > 0) {
|
|
|
|
if (typeof img === 'string') {
|
|
|
|
if (!img.src) {
|
|
|
|
|
|
|
|
throw new Error('The ImageElement must contain a src');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
imgDom = img;
|
|
|
|
|
|
|
|
} else if (typeof img === 'string') {
|
|
|
|
imgDom = document.createElement('img');
|
|
|
|
imgDom = document.createElement('img');
|
|
|
|
if (options.crossOrigin) {
|
|
|
|
if (options.crossOrigin) {
|
|
|
|
imgDom.crossOrigin = options.crossOrigin;
|
|
|
|
imgDom.crossOrigin = options.crossOrigin;
|
|
|
@ -261,11 +253,6 @@ export class QrcodeDecoder {
|
|
|
|
imgDom!.onload = () => resolve(true);
|
|
|
|
imgDom!.onload = () => resolve(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await proms();
|
|
|
|
await proms();
|
|
|
|
} else if (+img.nodeType > 0) {
|
|
|
|
|
|
|
|
if (!img.src) {
|
|
|
|
|
|
|
|
throw new Error('The ImageElement must contain a src');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
imgDom = img;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let code = null;
|
|
|
|
let code = null;
|
|
|
@ -292,7 +279,7 @@ export class QrcodeDecoder {
|
|
|
|
return code;
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|