Skip to content

Commit eec9772

Browse files
committed
feat: scan
1 parent cf0e952 commit eec9772

File tree

2 files changed

+73
-29
lines changed

2 files changed

+73
-29
lines changed

app/components/Scan.vue

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<script lang="ts" setup>
2+
import { scan } from 'qr-scanner-wechat'
3+
4+
const props = defineProps<{
5+
speed: number
6+
}>()
7+
8+
const results = defineModel<Set<string>>('results', { default: new Set() })
9+
10+
const shutterCount = ref(0)
11+
12+
const stream = await navigator.mediaDevices.getUserMedia({
13+
audio: false,
14+
video: {
15+
width: 512,
16+
height: 512,
17+
},
18+
})
19+
20+
const video = shallowRef<HTMLVideoElement>()
21+
onMounted(() => {
22+
video.value!.srcObject = stream
23+
video.value!.play()
24+
})
25+
26+
async function scanFrame() {
27+
shutterCount.value += 1
28+
const canvas = document.createElement('canvas')
29+
canvas.width = video.value!.videoWidth
30+
canvas.height = video.value!.videoHeight
31+
const ctx = canvas.getContext('2d')!
32+
ctx.drawImage(video.value!, 0, 0, canvas.width, canvas.height)
33+
const result = await scan(canvas)
34+
35+
if (result?.text) {
36+
results.value.add(result.text)
37+
}
38+
}
39+
40+
let intervalId: any
41+
watch(() => props.speed, () => {
42+
intervalId && clearInterval(intervalId)
43+
intervalId = setInterval(scanFrame, props.speed)
44+
}, { immediate: true })
45+
</script>
46+
47+
<template>
48+
<p>shutterCount: {{ shutterCount }}</p>
49+
<video ref="video" />
50+
</template>

app/pages/scan.vue

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
<script lang="ts" setup>
2-
import { scan } from 'qr-scanner-wechat'
2+
const speed = ref(100)
33
4-
const stream = await navigator.mediaDevices.getUserMedia({
5-
audio: false,
6-
video: {
7-
width: 512,
8-
height: 512,
9-
},
10-
})
11-
12-
const video = shallowRef<HTMLVideoElement>()
13-
onMounted(() => {
14-
video.value!.srcObject = stream
15-
video.value!.play()
16-
})
17-
18-
async function scanFrame() {
19-
const canvas = document.createElement('canvas')
20-
canvas.width = video.value!.videoWidth
21-
canvas.height = video.value!.videoHeight
22-
const ctx = canvas.getContext('2d')!
23-
ctx.drawImage(video.value!, 0, 0, canvas.width, canvas.height)
24-
const result = await scan(canvas)
25-
26-
if (result?.text)
27-
alert(result?.text)
28-
}
29-
30-
setInterval(scanFrame, 100) // scan one frame every 100ms
4+
const results = ref(new Set<string>())
315
</script>
326

337
<template>
34-
<video ref="video" />
8+
<div py-10>
9+
<h1 text-4xl>
10+
Scan
11+
</h1>
12+
<Scan v-model:results="results" :speed="speed" />
13+
<div mt-10>
14+
<label>
15+
<input v-model.number="speed" type="range" step="100" min="30" max="1000">
16+
<span>Speed: {{ speed }}ms</span>
17+
</label>
18+
</div>
19+
<h2 text-3xl>
20+
Results: {{ results.size }}
21+
</h2>
22+
<div>
23+
<div v-for="(item, index) of results" :key="index" font-mono>
24+
ID:{{ index }}:<br>
25+
{{ item }}
26+
</div>
27+
</div>
28+
</div>
3529
</template>

0 commit comments

Comments
 (0)