Skip to content

Commit 86b0c2b

Browse files
committed
feat: ✨ 支持从 Canvas 中识别二维码
1 parent 5441a0c commit 86b0c2b

4 files changed

Lines changed: 33 additions & 18 deletions

File tree

apps/common/qr-code/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# common-qr-code
22

3+
## 1.3.0
4+
5+
### Minor Changes
6+
7+
- 支持从 Canvas 中识别二维码
8+
39
## 1.2.0
410

511
### Minor Changes

apps/common/qr-code/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "common-qr-code",
33
"type": "module",
4-
"version": "1.2.0",
4+
"version": "1.3.0",
55
"private": true,
66
"description": "图片二维码识别(Common QR Code)-- 右键图片,识别二维码并复制到剪贴板。",
77
"author": {

apps/common/qr-code/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ImageElement } from './utils'
12
import { GM_registerMenuCommand, GM_setClipboard } from '$'
23
import { Notify } from 'notiflix/build/notiflix-notify-aio'
34
import Swal from 'sweetalert'
@@ -6,13 +7,14 @@ import { decodeQrCode } from './utils'
67

78
console.log(`${ID}(v${VERSION})`)
89

9-
let image: HTMLImageElement | null = null
10+
let image: ImageElement | null = null
1011

1112
GM_registerMenuCommand('Decode QR Code', () => {
1213
if (!image) {
1314
return Notify.warning('未选择图片, 请先右键选择图片')
1415
}
15-
decodeQrCode(image.src).then((results) => {
16+
17+
decodeQrCode(image).then((results) => {
1618
if (results.length === 0) {
1719
return Notify.warning('未识别到二维码, 请确认图片是否有效')
1820
}
@@ -68,7 +70,7 @@ GM_registerMenuCommand('Decode QR Code', () => {
6870
})
6971

7072
document.addEventListener('contextmenu', (event) => {
71-
if (event.target instanceof HTMLImageElement) {
73+
if (event.target instanceof HTMLImageElement || event.target instanceof HTMLCanvasElement) {
7274
image = event.target
7375
}
7476
})

apps/common/qr-code/src/utils.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { GM_xmlhttpRequest } from '$'
22

33
import jsQR from 'jsqr'
44

5-
export async function decodeQrCode(url: string): Promise<string[]> {
5+
export type ImageElement = HTMLImageElement | HTMLCanvasElement
6+
7+
export async function decodeQrCode(element: ImageElement): Promise<string[]> {
68
return new Promise((resolve, reject) => {
79
const image = new Image()
810

@@ -42,18 +44,23 @@ export async function decodeQrCode(url: string): Promise<string[]> {
4244

4345
image.onerror = reject
4446

45-
GM_xmlhttpRequest({
46-
method: 'GET',
47-
url,
48-
responseType: 'blob',
49-
onload: (response) => {
50-
if (response.status !== 200) {
51-
reject(new Error(`Failed to load image: ${response.status} ${response.statusText}`))
52-
return
53-
}
54-
image.src = URL.createObjectURL(response.response)
55-
},
56-
onerror: reject,
57-
})
47+
if (element instanceof HTMLImageElement) { // 静态图片
48+
GM_xmlhttpRequest({
49+
method: 'GET',
50+
url: element.src,
51+
responseType: 'blob',
52+
onload: (response) => {
53+
if (response.status !== 200) {
54+
reject(new Error(`Failed to load image: ${response.status} ${response.statusText}`))
55+
return
56+
}
57+
image.src = URL.createObjectURL(response.response)
58+
},
59+
onerror: reject,
60+
})
61+
}
62+
else if (element instanceof HTMLCanvasElement) { // Canvas 图片
63+
image.src = element.toDataURL()
64+
}
5865
})
5966
}

0 commit comments

Comments
 (0)