mirror of
https://github.com/yugasun/qrcode-decoder.git
synced 2026-08-12 20:27:56 +08:00
fix: add decode code result type
This commit is contained in:
@@ -2,4 +2,4 @@
|
|||||||
|
|
||||||
Rewrite unit test.
|
Rewrite unit test.
|
||||||
|
|
||||||
- [] Rewrite unit test.
|
- [x] Rewrite unit test.
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@
|
|||||||
var vConsole = new VConsole();
|
var vConsole = new VConsole();
|
||||||
console.log('Hello world');
|
console.log('Hello world');
|
||||||
function main() {
|
function main() {
|
||||||
var qr = new QrcodeDecoder();
|
var qr = new QrcodeDecoder.default();
|
||||||
var video = document.querySelector('#video');
|
var video = document.querySelector('#video');
|
||||||
var start = document.querySelector('#start');
|
var start = document.querySelector('#start');
|
||||||
var stop = document.querySelector('#stop');
|
var stop = document.querySelector('#stop');
|
||||||
|
|||||||
+1
-1
@@ -30,7 +30,7 @@
|
|||||||
<script type="module">
|
<script type="module">
|
||||||
var vConsole = new VConsole();
|
var vConsole = new VConsole();
|
||||||
function main() {
|
function main() {
|
||||||
var qr = new QrcodeDecoder();
|
var qr = new QrcodeDecoder.default();
|
||||||
|
|
||||||
var btn1 = document.querySelector('button#decode1');
|
var btn1 = document.querySelector('button#decode1');
|
||||||
var btn2 = document.querySelector('button#decode2');
|
var btn2 = document.querySelector('button#decode2');
|
||||||
|
|||||||
+1
-1
@@ -22,7 +22,7 @@
|
|||||||
var result = document.querySelector('#result');
|
var result = document.querySelector('#result');
|
||||||
var start = document.querySelector('#start');
|
var start = document.querySelector('#start');
|
||||||
var stop = document.querySelector('#stop');
|
var stop = document.querySelector('#stop');
|
||||||
var qr = new QrcodeDecoder();
|
var qr = new QrcodeDecoder.default();
|
||||||
|
|
||||||
start.onclick = startScan;
|
start.onclick = startScan;
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -79,7 +79,7 @@
|
|||||||
"ts-node": "^8.4.1",
|
"ts-node": "^8.4.1",
|
||||||
"tslint": "^5.20.1",
|
"tslint": "^5.20.1",
|
||||||
"tslint-config-prettier": "^1.18.0",
|
"tslint-config-prettier": "^1.18.0",
|
||||||
"typescript": "^3.7.5"
|
"typescript": "^4.4.3"
|
||||||
},
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
|
|||||||
Generated
+6122
-4621
File diff suppressed because it is too large
Load Diff
+21
-9
@@ -1,12 +1,13 @@
|
|||||||
import { Options } from 'jsqr';
|
import jsQR, { QRCode, 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 },
|
||||||
};
|
};
|
||||||
|
|
||||||
class QrcodeDecoder {
|
export class QrcodeDecoder {
|
||||||
timerCapture: null | NodeJS.Timeout;
|
timerCapture: null | NodeJS.Timeout;
|
||||||
canvasElem: null | HTMLCanvasElement;
|
canvasElem: null | HTMLCanvasElement;
|
||||||
gCtx: null | CanvasRenderingContext2D;
|
gCtx: null | CanvasRenderingContext2D;
|
||||||
@@ -100,7 +101,10 @@ 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(videoElem: HTMLVideoElement, options: Options) {
|
async _captureToCanvas(
|
||||||
|
videoElem: HTMLVideoElement,
|
||||||
|
options: Options,
|
||||||
|
): Promise<CodeResult> {
|
||||||
if (this.timerCapture) {
|
if (this.timerCapture) {
|
||||||
clearTimeout(this.timerCapture);
|
clearTimeout(this.timerCapture);
|
||||||
}
|
}
|
||||||
@@ -142,7 +146,7 @@ class QrcodeDecoder {
|
|||||||
|
|
||||||
const result = await proms();
|
const result = await proms();
|
||||||
|
|
||||||
return result;
|
return result as CodeResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -155,7 +159,10 @@ 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(videoElem: HTMLVideoElement, options: any = {}) {
|
async decodeFromCamera(
|
||||||
|
videoElem: HTMLVideoElement,
|
||||||
|
options: any = {},
|
||||||
|
): Promise<CodeResult> {
|
||||||
const opts = {
|
const opts = {
|
||||||
...this.defaultOption,
|
...this.defaultOption,
|
||||||
...options,
|
...options,
|
||||||
@@ -197,6 +204,8 @@ class QrcodeDecoder {
|
|||||||
const code = await this.decodeFromVideo(videoElem, opts);
|
const code = await this.decodeFromVideo(videoElem, opts);
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -207,7 +216,10 @@ 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(videoElem: HTMLVideoElement, options = {}) {
|
async decodeFromVideo(
|
||||||
|
videoElem: HTMLVideoElement,
|
||||||
|
options = {},
|
||||||
|
): Promise<CodeResult> {
|
||||||
const opts = {
|
const opts = {
|
||||||
...this.defaultOption,
|
...this.defaultOption,
|
||||||
...options,
|
...options,
|
||||||
@@ -231,7 +243,7 @@ class QrcodeDecoder {
|
|||||||
async decodeFromImage(
|
async decodeFromImage(
|
||||||
img: HTMLImageElement | string,
|
img: HTMLImageElement | string,
|
||||||
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,
|
||||||
@@ -280,7 +292,7 @@ class QrcodeDecoder {
|
|||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+4
-1
@@ -11,7 +11,10 @@
|
|||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"preserveConstEnums": true,
|
"preserveConstEnums": true,
|
||||||
"importHelpers": false,
|
"importHelpers": false,
|
||||||
"lib": ["esnext", "dom"],
|
"lib": [
|
||||||
|
"esnext",
|
||||||
|
"dom"
|
||||||
|
],
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"sourceMap": false,
|
"sourceMap": false,
|
||||||
|
|||||||
Reference in New Issue
Block a user