qrcode-reader-vue3 3.1.3 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.browserslistrc +3 -3
- package/.editorconfig +18 -18
- package/.github/ISSUE_TEMPLATE/bug_report.md +28 -28
- package/.github/copilot-instructions.md +209 -0
- package/.github/workflows/npm-publish.yml +15 -15
- package/.releaserc.yml +4 -4
- package/.travis.yml +26 -26
- package/CONTRIBUTING.md +23 -23
- package/LICENSE +21 -21
- package/README.md +194 -194
- package/babel.config.js +3 -3
- package/dist/QrcodeReaderVue3.common.js +15988 -13327
- package/dist/QrcodeReaderVue3.common.js.map +1 -1
- package/dist/QrcodeReaderVue3.umd.js +16152 -13482
- package/dist/QrcodeReaderVue3.umd.js.map +1 -1
- package/dist/QrcodeReaderVue3.umd.min.js +3 -1
- package/dist/QrcodeReaderVue3.umd.min.js.map +1 -1
- package/dist/demo.html +1 -8
- package/docs/.vuepress/components/DemoWrapper.vue +54 -54
- package/docs/.vuepress/components/demos/CustomTracking.vue +3 -2
- package/docs/.vuepress/components/demos/DecodeAll.vue +58 -58
- package/docs/.vuepress/components/demos/DragDrop.vue +80 -80
- package/docs/.vuepress/components/demos/Fullscreen.vue +115 -115
- package/docs/.vuepress/components/demos/LoadingIndicator.vue +61 -61
- package/docs/.vuepress/components/demos/ScanSameQrcodeMoreThanOnce.vue +78 -78
- package/docs/.vuepress/components/demos/SwitchCamera.vue +82 -82
- package/docs/.vuepress/components/demos/Torch.vue +64 -64
- package/docs/.vuepress/components/demos/Upload.vue +28 -28
- package/docs/.vuepress/components/demos/Validate.vue +118 -118
- package/docs/.vuepress/config.js +41 -41
- package/docs/.vuepress/public/checkmark.svg +1 -1
- package/docs/.vuepress/public/debug-memory-leak.html +102 -102
- package/docs/README.md +7 -7
- package/docs/api/QrcodeCapture.md +95 -95
- package/docs/api/QrcodeDropZone.md +117 -117
- package/docs/api/QrcodeStream.md +298 -298
- package/docs/demos/CustomTracking.md +13 -13
- package/docs/demos/DecodeAll.md +12 -12
- package/docs/demos/DragDrop.md +13 -13
- package/docs/demos/Fullscreen.md +13 -13
- package/docs/demos/LoadingIndicator.md +14 -14
- package/docs/demos/ScanSameQrcodeMoreThanOnce.md +17 -17
- package/docs/demos/Simple.md +14 -14
- package/docs/demos/SwitchCamera.md +12 -12
- package/docs/demos/Torch.md +15 -15
- package/docs/demos/Upload.md +16 -16
- package/docs/demos/Validate.md +13 -13
- package/eslint.config.js +49 -0
- package/package.json +33 -23
- package/src/components/QrcodeCapture.vue +31 -30
- package/src/components/QrcodeDropZone.vue +42 -42
- package/src/components/QrcodeStream.vue +28 -31
- package/src/index.ts +44 -0
- package/src/misc/{camera.js → camera.ts} +79 -21
- package/src/misc/{errors.js → errors.ts} +25 -25
- package/src/misc/scanner.ts +149 -0
- package/src/misc/{shimGetUserMedia.js → shimGetUserMedia.ts} +28 -28
- package/src/misc/util.ts +35 -0
- package/src/mixins/CommonAPI.vue +9 -4
- package/src/shims-vue.d.ts +9 -0
- package/src/types/index.d.ts +43 -0
- package/src/types/vue-qrcode-reader.d.ts +32 -4
- package/src/types/webrtc-adapter.d.ts +25 -0
- package/tests/misc/util.test.ts +72 -0
- package/tsconfig.json +35 -0
- package/vitest.config.ts +10 -0
- package/vue.config.js +30 -7
- package/.eslintrc.js +0 -25
- package/src/index.js +0 -32
- package/src/misc/scanner.js +0 -112
- package/src/misc/util.js +0 -15
|
@@ -28,10 +28,13 @@
|
|
|
28
28
|
</div>
|
|
29
29
|
</template>
|
|
30
30
|
|
|
31
|
-
<script>
|
|
32
|
-
import { keepScanning } from "../misc/scanner
|
|
33
|
-
import Camera from "../misc/camera
|
|
31
|
+
<script lang="ts">
|
|
32
|
+
import { keepScanning } from "../misc/scanner";
|
|
33
|
+
import Camera from "../misc/camera";
|
|
34
|
+
import type { Camera as CameraType } from "../misc/camera";
|
|
34
35
|
import CommonAPI from "../mixins/CommonAPI.vue";
|
|
36
|
+
import type { DetectedBarcode } from "barcode-detector";
|
|
37
|
+
import type { QRCodeResult } from "../misc/scanner";
|
|
35
38
|
|
|
36
39
|
export default {
|
|
37
40
|
name: "qrcode-stream",
|
|
@@ -43,7 +46,7 @@ export default {
|
|
|
43
46
|
type: String,
|
|
44
47
|
default: "auto",
|
|
45
48
|
|
|
46
|
-
validator(camera) {
|
|
49
|
+
validator(camera: string) {
|
|
47
50
|
return ["auto", "rear", "front", "off"].includes(camera);
|
|
48
51
|
},
|
|
49
52
|
},
|
|
@@ -60,17 +63,17 @@ export default {
|
|
|
60
63
|
|
|
61
64
|
data() {
|
|
62
65
|
return {
|
|
63
|
-
cameraInstance: null,
|
|
66
|
+
cameraInstance: null as CameraType | null,
|
|
64
67
|
mounted: false,
|
|
65
68
|
};
|
|
66
69
|
},
|
|
67
70
|
|
|
68
71
|
computed: {
|
|
69
|
-
shouldStream() {
|
|
72
|
+
shouldStream(): boolean {
|
|
70
73
|
return this.mounted && this.camera !== "off";
|
|
71
74
|
},
|
|
72
75
|
|
|
73
|
-
shouldScan() {
|
|
76
|
+
shouldScan(): boolean {
|
|
74
77
|
return this.shouldStream === true && this.cameraInstance !== null;
|
|
75
78
|
},
|
|
76
79
|
|
|
@@ -78,7 +81,7 @@ export default {
|
|
|
78
81
|
* Minimum delay in milliseconds between frames to be scanned. Don't scan
|
|
79
82
|
* so often when visual tracking is disabled to improve performance.
|
|
80
83
|
*/
|
|
81
|
-
scanInterval() {
|
|
84
|
+
scanInterval(): number {
|
|
82
85
|
if (this.track === undefined) {
|
|
83
86
|
return 500;
|
|
84
87
|
} else {
|
|
@@ -88,23 +91,23 @@ export default {
|
|
|
88
91
|
},
|
|
89
92
|
|
|
90
93
|
watch: {
|
|
91
|
-
shouldStream(shouldStream) {
|
|
94
|
+
shouldStream(shouldStream: boolean) {
|
|
92
95
|
if (!shouldStream) {
|
|
93
|
-
const canvas = this.$refs.pauseFrame;
|
|
96
|
+
const canvas = this.$refs.pauseFrame as HTMLCanvasElement;
|
|
94
97
|
const ctx = canvas.getContext("2d");
|
|
95
|
-
const video = this.$refs.video;
|
|
98
|
+
const video = this.$refs.video as HTMLVideoElement;
|
|
96
99
|
|
|
97
100
|
canvas.width = video.videoWidth;
|
|
98
101
|
canvas.height = video.videoHeight;
|
|
99
102
|
|
|
100
|
-
ctx
|
|
103
|
+
ctx?.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
|
|
101
104
|
}
|
|
102
105
|
},
|
|
103
106
|
|
|
104
|
-
shouldScan(shouldScan) {
|
|
107
|
+
shouldScan(shouldScan: boolean) {
|
|
105
108
|
if (shouldScan) {
|
|
106
|
-
this.clearCanvas(this.$refs.pauseFrame);
|
|
107
|
-
this.clearCanvas(this.$refs.trackingLayer);
|
|
109
|
+
this.clearCanvas(this.$refs.pauseFrame as HTMLCanvasElement);
|
|
110
|
+
this.clearCanvas(this.$refs.trackingLayer as HTMLCanvasElement);
|
|
108
111
|
this.startScanning();
|
|
109
112
|
}
|
|
110
113
|
},
|
|
@@ -123,12 +126,6 @@ export default {
|
|
|
123
126
|
this.mounted = true;
|
|
124
127
|
},
|
|
125
128
|
|
|
126
|
-
// Vue 2:
|
|
127
|
-
beforeDestroy() {
|
|
128
|
-
this.beforeResetCamera();
|
|
129
|
-
this.mounted = false;
|
|
130
|
-
},
|
|
131
|
-
// Vue 3:
|
|
132
129
|
beforeUnmount() {
|
|
133
130
|
this.beforeResetCamera();
|
|
134
131
|
this.mounted = false;
|
|
@@ -146,8 +143,8 @@ export default {
|
|
|
146
143
|
capabilities: {},
|
|
147
144
|
};
|
|
148
145
|
} else {
|
|
149
|
-
this.cameraInstance = await Camera(this.$refs.video, {
|
|
150
|
-
camera: this.camera,
|
|
146
|
+
this.cameraInstance = await Camera(this.$refs.video as HTMLVideoElement, {
|
|
147
|
+
camera: this.camera as "auto" | "rear" | "front" | "off",
|
|
151
148
|
torch: this.torch,
|
|
152
149
|
});
|
|
153
150
|
|
|
@@ -170,11 +167,11 @@ export default {
|
|
|
170
167
|
},
|
|
171
168
|
|
|
172
169
|
startScanning() {
|
|
173
|
-
const detectHandler = (result) => {
|
|
170
|
+
const detectHandler = (result: QRCodeResult) => {
|
|
174
171
|
this.onDetect(Promise.resolve(result));
|
|
175
172
|
};
|
|
176
173
|
|
|
177
|
-
keepScanning(this.$refs.video, {
|
|
174
|
+
keepScanning(this.$refs.video as HTMLVideoElement, {
|
|
178
175
|
detectHandler,
|
|
179
176
|
locateHandler: this.onLocate,
|
|
180
177
|
minDelay: this.scanInterval,
|
|
@@ -188,9 +185,9 @@ export default {
|
|
|
188
185
|
}
|
|
189
186
|
},
|
|
190
187
|
|
|
191
|
-
onLocate(detectedCodes) {
|
|
192
|
-
const canvas = this.$refs.trackingLayer;
|
|
193
|
-
const video = this.$refs.video;
|
|
188
|
+
onLocate(detectedCodes: DetectedBarcode[]) {
|
|
189
|
+
const canvas = this.$refs.trackingLayer as HTMLCanvasElement;
|
|
190
|
+
const video = this.$refs.video as HTMLVideoElement;
|
|
194
191
|
|
|
195
192
|
if (canvas !== undefined) {
|
|
196
193
|
if (
|
|
@@ -198,7 +195,7 @@ export default {
|
|
|
198
195
|
this.track !== undefined &&
|
|
199
196
|
video !== undefined
|
|
200
197
|
) {
|
|
201
|
-
const adjustPoint = ({ x, y }) => {
|
|
198
|
+
const adjustPoint = ({ x, y }: { x: number; y: number }) => {
|
|
202
199
|
// The visually occupied area of the video element.
|
|
203
200
|
// Because the component is responsive and fills the available space,
|
|
204
201
|
// this can be more or less than the actual resolution of the camera.
|
|
@@ -262,9 +259,9 @@ export default {
|
|
|
262
259
|
}
|
|
263
260
|
},
|
|
264
261
|
|
|
265
|
-
clearCanvas(canvas) {
|
|
262
|
+
clearCanvas(canvas: HTMLCanvasElement) {
|
|
266
263
|
const ctx = canvas.getContext("2d");
|
|
267
|
-
ctx
|
|
264
|
+
ctx?.clearRect(0, 0, canvas.width, canvas.height);
|
|
268
265
|
},
|
|
269
266
|
},
|
|
270
267
|
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { App, Plugin } from "vue";
|
|
2
|
+
import QrcodeStream from "./components/QrcodeStream.vue";
|
|
3
|
+
import QrcodeCapture from "./components/QrcodeCapture.vue";
|
|
4
|
+
import QrcodeDropZone from "./components/QrcodeDropZone.vue";
|
|
5
|
+
|
|
6
|
+
// Install the components
|
|
7
|
+
export function install(app: App): void {
|
|
8
|
+
app.component("qrcode-stream", QrcodeStream);
|
|
9
|
+
app.component("qrcode-capture", QrcodeCapture);
|
|
10
|
+
app.component("qrcode-drop-zone", QrcodeDropZone);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Expose the components
|
|
14
|
+
export { QrcodeStream, QrcodeCapture, QrcodeDropZone };
|
|
15
|
+
|
|
16
|
+
// Export types
|
|
17
|
+
export type { QRCodeResult, QRCodeLocation } from "./misc/scanner";
|
|
18
|
+
|
|
19
|
+
/* -- Plugin definition & Auto-install -- */
|
|
20
|
+
/* You shouldn't have to modify the code below */
|
|
21
|
+
|
|
22
|
+
// Plugin
|
|
23
|
+
const plugin: Plugin = { install };
|
|
24
|
+
|
|
25
|
+
export default plugin;
|
|
26
|
+
|
|
27
|
+
// Auto-install
|
|
28
|
+
declare global {
|
|
29
|
+
interface Window {
|
|
30
|
+
Vue?: App;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let GlobalVue: App | null = null;
|
|
35
|
+
if (typeof window !== "undefined") {
|
|
36
|
+
GlobalVue = window.Vue ?? null;
|
|
37
|
+
} else {
|
|
38
|
+
type GlobalWithVue = typeof globalThis & { Vue?: App };
|
|
39
|
+
const maybeGlobal = globalThis as GlobalWithVue;
|
|
40
|
+
GlobalVue = maybeGlobal.Vue ?? null;
|
|
41
|
+
}
|
|
42
|
+
if (GlobalVue) {
|
|
43
|
+
GlobalVue.use(plugin);
|
|
44
|
+
}
|
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
StreamApiNotSupportedError,
|
|
3
|
+
InsecureContextError,
|
|
4
|
+
} from "./errors";
|
|
5
|
+
import { eventOn, timeout } from "./util";
|
|
3
6
|
import shimGetUserMedia from "./shimGetUserMedia";
|
|
4
7
|
|
|
5
|
-
class Camera {
|
|
6
|
-
|
|
8
|
+
export class Camera {
|
|
9
|
+
videoEl: HTMLVideoElement;
|
|
10
|
+
stream: MediaStream;
|
|
11
|
+
|
|
12
|
+
constructor(videoEl: HTMLVideoElement, stream: MediaStream) {
|
|
7
13
|
this.videoEl = videoEl;
|
|
8
14
|
this.stream = stream;
|
|
9
15
|
}
|
|
10
16
|
|
|
11
|
-
stop() {
|
|
17
|
+
stop(): void {
|
|
12
18
|
this.videoEl.srcObject = null;
|
|
13
19
|
|
|
14
20
|
this.stream.getTracks().forEach((track) => {
|
|
@@ -17,14 +23,42 @@ class Camera {
|
|
|
17
23
|
});
|
|
18
24
|
}
|
|
19
25
|
|
|
20
|
-
getCapabilities() {
|
|
26
|
+
getCapabilities(): MediaTrackCapabilities {
|
|
21
27
|
const [track] = this.stream.getVideoTracks();
|
|
22
28
|
// Firefox does not yet support getCapabilities as of August 2020
|
|
23
|
-
return track?.getCapabilities?.() ?? {};
|
|
29
|
+
return track?.getCapabilities?.() ?? ({} as MediaTrackCapabilities);
|
|
24
30
|
}
|
|
25
31
|
}
|
|
26
32
|
|
|
27
|
-
|
|
33
|
+
type CameraMode = "auto" | "rear" | "front" | "off";
|
|
34
|
+
|
|
35
|
+
type LegacyVideoElement = HTMLVideoElement & {
|
|
36
|
+
mozSrcObject?: MediaStream;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type VideoElementWithOptionalSrcObject = HTMLVideoElement & {
|
|
40
|
+
srcObject?: MediaStream | null;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type ExtendedWindow = Window & {
|
|
44
|
+
webkitURL?: typeof window.URL;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type TorchCapabilities = MediaTrackCapabilities & {
|
|
48
|
+
torch?: boolean;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type TorchConstraintSet = MediaTrackConstraintSet & {
|
|
52
|
+
torch?: boolean;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const narrowDownFacingMode = async (
|
|
56
|
+
camera: CameraMode
|
|
57
|
+
): Promise<
|
|
58
|
+
| { deviceId: { exact: string } }
|
|
59
|
+
| { facingMode: { ideal: string } | { exact: string } }
|
|
60
|
+
| undefined
|
|
61
|
+
> => {
|
|
28
62
|
// Modern phones often have multipe front/rear cameras.
|
|
29
63
|
// Sometimes special purpose cameras like the wide-angle camera are picked
|
|
30
64
|
// by default. Those are not optimal for scanning QR codes but standard
|
|
@@ -64,7 +98,15 @@ const narrowDownFacingMode = async (camera) => {
|
|
|
64
98
|
}
|
|
65
99
|
};
|
|
66
100
|
|
|
67
|
-
export
|
|
101
|
+
export interface CameraOptions {
|
|
102
|
+
camera: CameraMode;
|
|
103
|
+
torch: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default async function (
|
|
107
|
+
videoEl: HTMLVideoElement,
|
|
108
|
+
{ camera, torch }: CameraOptions
|
|
109
|
+
): Promise<Camera> {
|
|
68
110
|
// At least in Chrome `navigator.mediaDevices` is undefined when the page is
|
|
69
111
|
// loaded using HTTP rather than HTTPS. Thus `STREAM_API_NOT_SUPPORTED` is
|
|
70
112
|
// initialized with `false` although the API might actually be supported.
|
|
@@ -83,7 +125,7 @@ export default async function (videoEl, { camera, torch }) {
|
|
|
83
125
|
// is not available during SSR. So we lazily apply this shim at runtime.
|
|
84
126
|
await shimGetUserMedia();
|
|
85
127
|
|
|
86
|
-
const constraints = {
|
|
128
|
+
const constraints: MediaStreamConstraints = {
|
|
87
129
|
audio: false,
|
|
88
130
|
video: {
|
|
89
131
|
width: { min: 360, ideal: 640, max: 1920 },
|
|
@@ -94,16 +136,29 @@ export default async function (videoEl, { camera, torch }) {
|
|
|
94
136
|
|
|
95
137
|
const stream = await navigator.mediaDevices.getUserMedia(constraints);
|
|
96
138
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
} else if (
|
|
104
|
-
|
|
139
|
+
const videoWithMoz = videoEl as LegacyVideoElement;
|
|
140
|
+
const videoWithSrcObject = videoEl as VideoElementWithOptionalSrcObject;
|
|
141
|
+
const windowWithWebkitURL = window as ExtendedWindow;
|
|
142
|
+
|
|
143
|
+
if (videoWithSrcObject.srcObject !== undefined) {
|
|
144
|
+
videoWithSrcObject.srcObject = stream;
|
|
145
|
+
} else if (videoWithMoz.mozSrcObject !== undefined) {
|
|
146
|
+
videoWithMoz.mozSrcObject = stream;
|
|
147
|
+
} else if (typeof window.URL.createObjectURL === "function") {
|
|
148
|
+
const objectUrl = window.URL.createObjectURL(
|
|
149
|
+
stream as unknown as MediaSource
|
|
150
|
+
);
|
|
151
|
+
videoWithMoz.src = objectUrl;
|
|
152
|
+
} else if (
|
|
153
|
+
windowWithWebkitURL.webkitURL !== undefined &&
|
|
154
|
+
typeof windowWithWebkitURL.webkitURL.createObjectURL === "function"
|
|
155
|
+
) {
|
|
156
|
+
const objectUrl = windowWithWebkitURL.webkitURL.createObjectURL(
|
|
157
|
+
stream as unknown as MediaSource
|
|
158
|
+
);
|
|
159
|
+
videoWithMoz.src = objectUrl;
|
|
105
160
|
} else {
|
|
106
|
-
|
|
161
|
+
videoWithMoz.src = stream as unknown as string;
|
|
107
162
|
}
|
|
108
163
|
|
|
109
164
|
await eventOn(videoEl, "loadeddata");
|
|
@@ -117,8 +172,11 @@ export default async function (videoEl, { camera, torch }) {
|
|
|
117
172
|
const [track] = stream.getVideoTracks();
|
|
118
173
|
const capabilities = track.getCapabilities();
|
|
119
174
|
|
|
120
|
-
|
|
121
|
-
|
|
175
|
+
const torchCapabilities = capabilities as TorchCapabilities;
|
|
176
|
+
|
|
177
|
+
if (torchCapabilities.torch) {
|
|
178
|
+
const torchConstraint: TorchConstraintSet = { torch: true };
|
|
179
|
+
track.applyConstraints({ advanced: [torchConstraint] });
|
|
122
180
|
} else {
|
|
123
181
|
console.warn("device does not support torch capability");
|
|
124
182
|
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
export class DropImageFetchError extends Error {
|
|
2
|
-
constructor() {
|
|
3
|
-
super("can't process cross-origin image");
|
|
4
|
-
|
|
5
|
-
this.name = "DropImageFetchError";
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class StreamApiNotSupportedError extends Error {
|
|
10
|
-
constructor() {
|
|
11
|
-
super("this browser has no Stream API support");
|
|
12
|
-
|
|
13
|
-
this.name = "StreamApiNotSupportedError";
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class InsecureContextError extends Error {
|
|
18
|
-
constructor() {
|
|
19
|
-
super(
|
|
20
|
-
"camera access is only permitted in secure context. Use HTTPS or localhost rather than HTTP."
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
this.name = "InsecureContextError";
|
|
24
|
-
}
|
|
25
|
-
}
|
|
1
|
+
export class DropImageFetchError extends Error {
|
|
2
|
+
constructor() {
|
|
3
|
+
super("can't process cross-origin image");
|
|
4
|
+
|
|
5
|
+
this.name = "DropImageFetchError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class StreamApiNotSupportedError extends Error {
|
|
10
|
+
constructor() {
|
|
11
|
+
super("this browser has no Stream API support");
|
|
12
|
+
|
|
13
|
+
this.name = "StreamApiNotSupportedError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class InsecureContextError extends Error {
|
|
18
|
+
constructor() {
|
|
19
|
+
super(
|
|
20
|
+
"camera access is only permitted in secure context. Use HTTPS or localhost rather than HTTP."
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
this.name = "InsecureContextError";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { DropImageFetchError } from "./errors";
|
|
2
|
+
import { BarcodeDetector, type DetectedBarcode } from "barcode-detector";
|
|
3
|
+
import { eventOn } from "./util";
|
|
4
|
+
|
|
5
|
+
export interface QRCodeLocation {
|
|
6
|
+
topLeftCorner: { x: number; y: number };
|
|
7
|
+
topRightCorner: { x: number; y: number };
|
|
8
|
+
bottomRightCorner: { x: number; y: number };
|
|
9
|
+
bottomLeftCorner: { x: number; y: number };
|
|
10
|
+
topLeftFinderPattern: Record<string, never>;
|
|
11
|
+
topRightFinderPattern: Record<string, never>;
|
|
12
|
+
bottomLeftFinderPattern: Record<string, never>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface QRCodeResult {
|
|
16
|
+
content: string | null;
|
|
17
|
+
location: QRCodeLocation | null;
|
|
18
|
+
imageData: ImageData | null;
|
|
19
|
+
binaryData: Uint8ClampedArray | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const adaptOldFormat = (detectedCodes: DetectedBarcode[]): QRCodeResult => {
|
|
23
|
+
if (detectedCodes.length > 0) {
|
|
24
|
+
const [firstCode] = detectedCodes;
|
|
25
|
+
|
|
26
|
+
const [topLeftCorner, topRightCorner, bottomRightCorner, bottomLeftCorner] =
|
|
27
|
+
firstCode.cornerPoints;
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
content: firstCode.rawValue,
|
|
31
|
+
location: {
|
|
32
|
+
topLeftCorner,
|
|
33
|
+
topRightCorner,
|
|
34
|
+
bottomRightCorner,
|
|
35
|
+
bottomLeftCorner,
|
|
36
|
+
|
|
37
|
+
// not supported by native API:
|
|
38
|
+
topLeftFinderPattern: {},
|
|
39
|
+
topRightFinderPattern: {},
|
|
40
|
+
bottomLeftFinderPattern: {},
|
|
41
|
+
},
|
|
42
|
+
imageData: null,
|
|
43
|
+
binaryData: null,
|
|
44
|
+
};
|
|
45
|
+
} else {
|
|
46
|
+
return {
|
|
47
|
+
content: null,
|
|
48
|
+
location: null,
|
|
49
|
+
imageData: null,
|
|
50
|
+
binaryData: null,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
interface ScanState {
|
|
56
|
+
lastScanned: number;
|
|
57
|
+
contentBefore: string | null;
|
|
58
|
+
locationBefore: QRCodeLocation | null;
|
|
59
|
+
binaryData: Uint8ClampedArray | null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface KeepScanningOptions {
|
|
63
|
+
detectHandler: (result: QRCodeResult) => void;
|
|
64
|
+
locateHandler: (detectedCodes: DetectedBarcode[]) => void;
|
|
65
|
+
minDelay: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Continuously extracts frames from camera stream and tries to read
|
|
70
|
+
* potentially pictured QR codes.
|
|
71
|
+
*/
|
|
72
|
+
export const keepScanning = (
|
|
73
|
+
videoElement: HTMLVideoElement,
|
|
74
|
+
options: KeepScanningOptions
|
|
75
|
+
): void => {
|
|
76
|
+
const barcodeDetector = new BarcodeDetector({ formats: ["qr_code"] });
|
|
77
|
+
|
|
78
|
+
const { detectHandler, locateHandler, minDelay } = options;
|
|
79
|
+
|
|
80
|
+
const processFrame =
|
|
81
|
+
(state: ScanState) =>
|
|
82
|
+
async (timeNow: number): Promise<void> => {
|
|
83
|
+
if (videoElement.readyState > 1) {
|
|
84
|
+
const { lastScanned, contentBefore, locationBefore } = state;
|
|
85
|
+
|
|
86
|
+
if (timeNow - lastScanned >= minDelay) {
|
|
87
|
+
const detectedCodes = await barcodeDetector.detect(videoElement);
|
|
88
|
+
const { content, location, imageData, binaryData } =
|
|
89
|
+
adaptOldFormat(detectedCodes);
|
|
90
|
+
|
|
91
|
+
if (content !== null && content !== contentBefore) {
|
|
92
|
+
detectHandler({ content, location, imageData, binaryData });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (location !== null || locationBefore !== null) {
|
|
96
|
+
locateHandler(detectedCodes);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
window.requestAnimationFrame(
|
|
100
|
+
processFrame({
|
|
101
|
+
lastScanned: timeNow,
|
|
102
|
+
contentBefore: content ?? contentBefore,
|
|
103
|
+
locationBefore: location,
|
|
104
|
+
binaryData: binaryData,
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
} else {
|
|
108
|
+
window.requestAnimationFrame(processFrame(state));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
processFrame({
|
|
114
|
+
contentBefore: null,
|
|
115
|
+
locationBefore: null,
|
|
116
|
+
lastScanned: performance.now(),
|
|
117
|
+
binaryData: null,
|
|
118
|
+
})(performance.now());
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const imageElementFromUrl = async (url: string): Promise<HTMLImageElement> => {
|
|
122
|
+
if (url.startsWith("http") && url.includes(location.host) === false) {
|
|
123
|
+
throw new DropImageFetchError();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const image = document.createElement("img");
|
|
127
|
+
image.src = url;
|
|
128
|
+
|
|
129
|
+
await eventOn(image, "load");
|
|
130
|
+
|
|
131
|
+
return image;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const processFile = async (
|
|
135
|
+
file: File | Blob | ImageBitmap | ImageData
|
|
136
|
+
): Promise<QRCodeResult> => {
|
|
137
|
+
const barcodeDetector = new BarcodeDetector({ formats: ["qr_code"] });
|
|
138
|
+
const detectedCodes = await barcodeDetector.detect(file);
|
|
139
|
+
|
|
140
|
+
return adaptOldFormat(detectedCodes);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export const processUrl = async (url: string): Promise<QRCodeResult> => {
|
|
144
|
+
const barcodeDetector = new BarcodeDetector({ formats: ["qr_code"] });
|
|
145
|
+
const image = await imageElementFromUrl(url);
|
|
146
|
+
const detectedCodes = await barcodeDetector.detect(image);
|
|
147
|
+
|
|
148
|
+
return adaptOldFormat(detectedCodes);
|
|
149
|
+
};
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { shimGetUserMedia as chromeShim } from "webrtc-adapter/src/js/chrome/getusermedia";
|
|
2
|
-
import { shimGetUserMedia as
|
|
3
|
-
import { shimGetUserMedia as
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
break;
|
|
25
|
-
default:
|
|
26
|
-
throw new StreamApiNotSupportedError();
|
|
27
|
-
}
|
|
28
|
-
});
|
|
1
|
+
import { shimGetUserMedia as chromeShim } from "webrtc-adapter/src/js/chrome/getusermedia";
|
|
2
|
+
import { shimGetUserMedia as firefoxShim } from "webrtc-adapter/src/js/firefox/getusermedia";
|
|
3
|
+
import { shimGetUserMedia as safariShim } from "webrtc-adapter/src/js/safari/safari_shim";
|
|
4
|
+
import adapter from "webrtc-adapter";
|
|
5
|
+
|
|
6
|
+
import { StreamApiNotSupportedError } from "./errors";
|
|
7
|
+
import { indempotent } from "./util";
|
|
8
|
+
|
|
9
|
+
export default indempotent(() => {
|
|
10
|
+
console.log("adapter.browserDetails.browser", adapter.browserDetails);
|
|
11
|
+
switch (adapter.browserDetails.browser) {
|
|
12
|
+
case "chrome":
|
|
13
|
+
chromeShim(window, adapter.browserDetails);
|
|
14
|
+
break;
|
|
15
|
+
case "firefox":
|
|
16
|
+
firefoxShim(window, adapter.browserDetails);
|
|
17
|
+
break;
|
|
18
|
+
case "safari":
|
|
19
|
+
safariShim(window, adapter.browserDetails);
|
|
20
|
+
break;
|
|
21
|
+
case "edge":
|
|
22
|
+
// Edge now uses Chromium, so use the same shim as Chrome
|
|
23
|
+
chromeShim(window, adapter.browserDetails);
|
|
24
|
+
break;
|
|
25
|
+
default:
|
|
26
|
+
throw new StreamApiNotSupportedError();
|
|
27
|
+
}
|
|
28
|
+
});
|
package/src/misc/util.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const indempotent = <T extends (...args: unknown[]) => unknown>(
|
|
2
|
+
action: T
|
|
3
|
+
): T => {
|
|
4
|
+
let called = false;
|
|
5
|
+
let result: ReturnType<T> | undefined = undefined;
|
|
6
|
+
|
|
7
|
+
return ((...args: Parameters<T>) => {
|
|
8
|
+
if (called) {
|
|
9
|
+
return result;
|
|
10
|
+
} else {
|
|
11
|
+
result = action(...args) as ReturnType<T>;
|
|
12
|
+
called = true;
|
|
13
|
+
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
}) as T;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const eventOn = (
|
|
20
|
+
target: EventTarget,
|
|
21
|
+
eventName: string
|
|
22
|
+
): Promise<Event> =>
|
|
23
|
+
new Promise((resolve) => {
|
|
24
|
+
const handler = (event: Event): void => {
|
|
25
|
+
target.removeEventListener(eventName, handler);
|
|
26
|
+
resolve(event);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
target.addEventListener(eventName, handler);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export const timeout = (ms: number): Promise<void> =>
|
|
33
|
+
new Promise((resolve) => {
|
|
34
|
+
setTimeout(resolve, ms);
|
|
35
|
+
});
|
package/src/mixins/CommonAPI.vue
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import BarcodeDetector from "barcode-detector";
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { BarcodeDetector } from "barcode-detector";
|
|
3
|
+
import type { QRCodeResult } from "../misc/scanner";
|
|
3
4
|
|
|
4
5
|
export default {
|
|
5
6
|
beforeMount() {
|
|
6
7
|
// if (!('BarcodeDetector' in window)) {
|
|
7
|
-
|
|
8
|
+
type WindowWithBarcodeDetector = Window & {
|
|
9
|
+
BarcodeDetector?: typeof BarcodeDetector;
|
|
10
|
+
};
|
|
11
|
+
const windowWithBarcodeDetector = window as WindowWithBarcodeDetector;
|
|
12
|
+
windowWithBarcodeDetector.BarcodeDetector = BarcodeDetector;
|
|
8
13
|
// }
|
|
9
14
|
},
|
|
10
15
|
|
|
11
16
|
methods: {
|
|
12
|
-
async onDetect(resultPromise) {
|
|
17
|
+
async onDetect(resultPromise: Promise<QRCodeResult>) {
|
|
13
18
|
this.$emit("detect", resultPromise);
|
|
14
19
|
|
|
15
20
|
try {
|