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