@weapp-vite/qr 1.0.0 → 1.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/README.md +109 -1
- package/dist/index.d.mts +123 -1
- package/dist/index.mjs +3416 -3136
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @weapp-vite/qr
|
|
2
2
|
|
|
3
|
-
`@weapp-vite/qr`
|
|
3
|
+
`@weapp-vite/qr` 提供二维码矩阵编码、图片解码、reader 适配、微信小程序码结构识别与终端文本渲染能力。
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
6
|
import { decodeQrCodeFromBase64, renderTerminalQrCode } from '@weapp-vite/qr'
|
|
@@ -8,3 +8,111 @@ import { decodeQrCodeFromBase64, renderTerminalQrCode } from '@weapp-vite/qr'
|
|
|
8
8
|
const content = await decodeQrCodeFromBase64(base64Png)
|
|
9
9
|
process.stdout.write(renderTerminalQrCode(content, { small: true }))
|
|
10
10
|
```
|
|
11
|
+
|
|
12
|
+
## API
|
|
13
|
+
|
|
14
|
+
### 编码
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { createQrCodeMatrix } from '@weapp-vite/qr'
|
|
18
|
+
|
|
19
|
+
const matrix = createQrCodeMatrix('https://vite.icebreaker.top/')
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### 解码
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import {
|
|
26
|
+
decodeQrCodeFromBase64,
|
|
27
|
+
decodeQrCodeFromBuffer,
|
|
28
|
+
decodeQrCodeFromFile,
|
|
29
|
+
} from '@weapp-vite/qr'
|
|
30
|
+
|
|
31
|
+
const fromBase64 = await decodeQrCodeFromBase64(base64Png)
|
|
32
|
+
const fromBuffer = await decodeQrCodeFromBuffer(imageBuffer)
|
|
33
|
+
const fromFile = await decodeQrCodeFromFile('./fixtures/qr.png')
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Reader 输入
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { decodeWithQrReader } from '@weapp-vite/qr'
|
|
40
|
+
import sharp from 'sharp'
|
|
41
|
+
|
|
42
|
+
const { data, info } = await sharp(imageBuffer)
|
|
43
|
+
.ensureAlpha()
|
|
44
|
+
.raw()
|
|
45
|
+
.toBuffer({ resolveWithObject: true })
|
|
46
|
+
|
|
47
|
+
const result = await decodeWithQrReader({
|
|
48
|
+
width: info.width,
|
|
49
|
+
height: info.height,
|
|
50
|
+
data,
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
console.log(result.result)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 小程序码结构识别
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { detectMiniProgramCodeFromFile } from '@weapp-vite/qr'
|
|
60
|
+
|
|
61
|
+
const detected = await detectMiniProgramCodeFromFile('./fixtures/mini-program-code.jpg')
|
|
62
|
+
|
|
63
|
+
if (detected) {
|
|
64
|
+
console.log(detected.kind)
|
|
65
|
+
console.log(detected.locatorPoints)
|
|
66
|
+
console.log(detected.badgeBounds)
|
|
67
|
+
console.log(detected.logoBounds)
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
这里的结构识别目标是判断图片是否符合微信小程序码几何特征,并返回定位点、中心 logo 区和右下角徽标区域。
|
|
72
|
+
它不是把小程序码直接解码成链接或页面路径。
|
|
73
|
+
|
|
74
|
+
### 高层类型检测
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
import { detectCodeTypeFromFile } from '@weapp-vite/qr'
|
|
78
|
+
|
|
79
|
+
const codeType = await detectCodeTypeFromFile('./fixtures/code.png')
|
|
80
|
+
|
|
81
|
+
if (codeType === 'qr') {
|
|
82
|
+
console.log('standard qr')
|
|
83
|
+
}
|
|
84
|
+
else if (codeType === 'mini-program-code') {
|
|
85
|
+
console.log('wechat mini program code')
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
console.log('unknown')
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 终端渲染
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import {
|
|
96
|
+
createQrCodeMatrix,
|
|
97
|
+
renderTerminalQrCode,
|
|
98
|
+
renderTerminalQrCodeFromMatrix,
|
|
99
|
+
} from '@weapp-vite/qr'
|
|
100
|
+
|
|
101
|
+
const compact = renderTerminalQrCode('weapp-vite', { small: true })
|
|
102
|
+
|
|
103
|
+
const matrix = createQrCodeMatrix('weapp-vite')
|
|
104
|
+
const rendered = renderTerminalQrCodeFromMatrix(matrix)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## 导出类型
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import type {
|
|
111
|
+
MiniProgramCodeDetectionResult,
|
|
112
|
+
QRCodeMatrix,
|
|
113
|
+
QRCodeReaderInput,
|
|
114
|
+
QRCodeReaderResult,
|
|
115
|
+
QRCodeRenderOptions,
|
|
116
|
+
QRCodeType,
|
|
117
|
+
} from '@weapp-vite/qr'
|
|
118
|
+
```
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
|
|
3
|
+
//#region src/codeType.d.ts
|
|
4
|
+
/** QRCodeType 的类型定义。 */
|
|
5
|
+
type QRCodeType = 'qr' | 'mini-program-code' | 'unknown';
|
|
6
|
+
/** detectCodeTypeFromBuffer 的方法封装。 */
|
|
7
|
+
declare function detectCodeTypeFromBuffer(buffer: Buffer): Promise<"qr" | "mini-program-code" | "unknown">;
|
|
8
|
+
/** detectCodeTypeFromBase64 的方法封装。 */
|
|
9
|
+
declare function detectCodeTypeFromBase64(content: string): Promise<"qr" | "mini-program-code" | "unknown">;
|
|
10
|
+
/** detectCodeTypeFromFile 的方法封装。 */
|
|
11
|
+
declare function detectCodeTypeFromFile(filePath: string): Promise<"qr" | "mini-program-code" | "unknown">;
|
|
12
|
+
//#endregion
|
|
1
13
|
//#region src/decode.d.ts
|
|
14
|
+
/** decodeQrCodeFromBuffer 的方法封装。 */
|
|
15
|
+
declare function decodeQrCodeFromBuffer(buffer: Buffer): Promise<string>;
|
|
2
16
|
/** decodeQrCodeFromBase64 的方法封装。 */
|
|
3
17
|
declare function decodeQrCodeFromBase64(content: string): Promise<string>;
|
|
18
|
+
/** decodeQrCodeFromFile 的方法封装。 */
|
|
19
|
+
declare function decodeQrCodeFromFile(filePath: string): Promise<string>;
|
|
4
20
|
//#endregion
|
|
5
21
|
//#region src/types.d.ts
|
|
6
22
|
/**
|
|
@@ -17,8 +33,114 @@ type QRCodeMatrix = boolean[][];
|
|
|
17
33
|
/** createQrCodeMatrix 的方法封装。 */
|
|
18
34
|
declare function createQrCodeMatrix(input: string): QRCodeMatrix;
|
|
19
35
|
//#endregion
|
|
36
|
+
//#region src/miniProgramCode.d.ts
|
|
37
|
+
interface MiniProgramCodePoint {
|
|
38
|
+
x: number;
|
|
39
|
+
y: number;
|
|
40
|
+
}
|
|
41
|
+
interface MiniProgramCodeBounds {
|
|
42
|
+
left: number;
|
|
43
|
+
top: number;
|
|
44
|
+
width: number;
|
|
45
|
+
height: number;
|
|
46
|
+
}
|
|
47
|
+
/** MiniProgramCodeDetectionResult 的类型定义。 */
|
|
48
|
+
interface MiniProgramCodeDetectionResult {
|
|
49
|
+
kind: 'wechat-mini-program-code';
|
|
50
|
+
width: number;
|
|
51
|
+
height: number;
|
|
52
|
+
confidence: number;
|
|
53
|
+
locatorPoints: readonly [MiniProgramCodePoint, MiniProgramCodePoint, MiniProgramCodePoint];
|
|
54
|
+
badgeBounds?: MiniProgramCodeBounds;
|
|
55
|
+
logoBounds?: MiniProgramCodeBounds;
|
|
56
|
+
}
|
|
57
|
+
/** detectMiniProgramCodeFromBuffer 的方法封装。 */
|
|
58
|
+
declare function detectMiniProgramCodeFromBuffer(buffer: Buffer): Promise<{
|
|
59
|
+
kind: "wechat-mini-program-code";
|
|
60
|
+
width: number;
|
|
61
|
+
height: number;
|
|
62
|
+
confidence: number;
|
|
63
|
+
locatorPoints: readonly [MiniProgramCodePoint, MiniProgramCodePoint, MiniProgramCodePoint];
|
|
64
|
+
badgeBounds: {
|
|
65
|
+
left: number;
|
|
66
|
+
top: number;
|
|
67
|
+
width: number;
|
|
68
|
+
height: number;
|
|
69
|
+
};
|
|
70
|
+
logoBounds: {
|
|
71
|
+
left: number;
|
|
72
|
+
top: number;
|
|
73
|
+
width: number;
|
|
74
|
+
height: number;
|
|
75
|
+
} | undefined;
|
|
76
|
+
} | null>;
|
|
77
|
+
/** detectMiniProgramCodeFromBase64 的方法封装。 */
|
|
78
|
+
declare function detectMiniProgramCodeFromBase64(content: string): Promise<{
|
|
79
|
+
kind: "wechat-mini-program-code";
|
|
80
|
+
width: number;
|
|
81
|
+
height: number;
|
|
82
|
+
confidence: number;
|
|
83
|
+
locatorPoints: readonly [MiniProgramCodePoint, MiniProgramCodePoint, MiniProgramCodePoint];
|
|
84
|
+
badgeBounds: {
|
|
85
|
+
left: number;
|
|
86
|
+
top: number;
|
|
87
|
+
width: number;
|
|
88
|
+
height: number;
|
|
89
|
+
};
|
|
90
|
+
logoBounds: {
|
|
91
|
+
left: number;
|
|
92
|
+
top: number;
|
|
93
|
+
width: number;
|
|
94
|
+
height: number;
|
|
95
|
+
} | undefined;
|
|
96
|
+
} | null>;
|
|
97
|
+
/** detectMiniProgramCodeFromFile 的方法封装。 */
|
|
98
|
+
declare function detectMiniProgramCodeFromFile(filePath: string): Promise<{
|
|
99
|
+
kind: "wechat-mini-program-code";
|
|
100
|
+
width: number;
|
|
101
|
+
height: number;
|
|
102
|
+
confidence: number;
|
|
103
|
+
locatorPoints: readonly [MiniProgramCodePoint, MiniProgramCodePoint, MiniProgramCodePoint];
|
|
104
|
+
badgeBounds: {
|
|
105
|
+
left: number;
|
|
106
|
+
top: number;
|
|
107
|
+
width: number;
|
|
108
|
+
height: number;
|
|
109
|
+
};
|
|
110
|
+
logoBounds: {
|
|
111
|
+
left: number;
|
|
112
|
+
top: number;
|
|
113
|
+
width: number;
|
|
114
|
+
height: number;
|
|
115
|
+
} | undefined;
|
|
116
|
+
} | null>;
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/reader/core/types.d.ts
|
|
119
|
+
/** QRCodeReaderResult 的类型定义。 */
|
|
120
|
+
interface QRCodeReaderResult {
|
|
121
|
+
result: string;
|
|
122
|
+
}
|
|
123
|
+
/** QRCodeReaderInput 的类型定义。 */
|
|
124
|
+
interface QRCodeReaderInput {
|
|
125
|
+
width: number;
|
|
126
|
+
height: number;
|
|
127
|
+
data: Buffer;
|
|
128
|
+
}
|
|
129
|
+
/** QRCodeReaderInstance 的类型定义。 */
|
|
130
|
+
interface QRCodeReaderInstance {
|
|
131
|
+
decode: (input: QRCodeReaderInput) => Promise<QRCodeReaderResult>;
|
|
132
|
+
}
|
|
133
|
+
/** QRCodeReaderConstructor 的类型定义。 */
|
|
134
|
+
type QRCodeReaderConstructor = new () => QRCodeReaderInstance;
|
|
135
|
+
//#endregion
|
|
136
|
+
//#region src/reader/decode.d.ts
|
|
137
|
+
/** decodeWithQrReader 的方法封装。 */
|
|
138
|
+
declare function decodeWithQrReader(input: QRCodeReaderInput): Promise<QRCodeReaderResult>;
|
|
139
|
+
//#endregion
|
|
20
140
|
//#region src/render.d.ts
|
|
141
|
+
/** renderTerminalQrCodeFromMatrix 的方法封装。 */
|
|
142
|
+
declare function renderTerminalQrCodeFromMatrix(matrix: QRCodeMatrix, options?: QRCodeRenderOptions): string;
|
|
21
143
|
/** renderTerminalQrCode 的方法封装。 */
|
|
22
144
|
declare function renderTerminalQrCode(input: string, options?: QRCodeRenderOptions): string;
|
|
23
145
|
//#endregion
|
|
24
|
-
export { type QRCodeMatrix, type QRCodeRenderOptions, createQrCodeMatrix, decodeQrCodeFromBase64, renderTerminalQrCode };
|
|
146
|
+
export { type MiniProgramCodeDetectionResult, type QRCodeMatrix, type QRCodeReaderConstructor, type QRCodeReaderInput, type QRCodeReaderInstance, type QRCodeReaderResult, type QRCodeRenderOptions, type QRCodeType, createQrCodeMatrix, decodeQrCodeFromBase64, decodeQrCodeFromBuffer, decodeQrCodeFromFile, decodeWithQrReader, detectCodeTypeFromBase64, detectCodeTypeFromBuffer, detectCodeTypeFromFile, detectMiniProgramCodeFromBase64, detectMiniProgramCodeFromBuffer, detectMiniProgramCodeFromFile, renderTerminalQrCode, renderTerminalQrCodeFromMatrix };
|