@hxa-rn/vision-camera-code-scanner 0.2.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/LICENSE +21 -0
- package/README.md +179 -0
- package/dist/commonjs/hook.js +39 -0
- package/dist/commonjs/hook.js.map +1 -0
- package/dist/commonjs/index.js +346 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/module/hook.js +34 -0
- package/dist/module/hook.js.map +1 -0
- package/dist/module/index.js +334 -0
- package/dist/module/index.js.map +1 -0
- package/dist/typescript/hook.d.ts +19 -0
- package/dist/typescript/index.d.ts +301 -0
- package/package.json +67 -0
- package/src/hook.tsx +49 -0
- package/src/index.ts +504 -0
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import type { Code, CodeType, Frame } from 'react-native-vision-camera';
|
|
2
|
+
/**
|
|
3
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.BarcodeFormat
|
|
4
|
+
*/
|
|
5
|
+
export declare enum BarcodeFormat {
|
|
6
|
+
UNKNOWN = -1,
|
|
7
|
+
ALL_FORMATS = 0,
|
|
8
|
+
CODE_128 = 1,
|
|
9
|
+
CODE_39 = 2,
|
|
10
|
+
CODE_93 = 4,
|
|
11
|
+
CODABAR = 8,
|
|
12
|
+
DATA_MATRIX = 16,
|
|
13
|
+
EAN_13 = 32,
|
|
14
|
+
EAN_8 = 64,
|
|
15
|
+
ITF = 128,
|
|
16
|
+
QR_CODE = 256,
|
|
17
|
+
UPC_A = 512,
|
|
18
|
+
UPC_E = 1024,
|
|
19
|
+
PDF417 = 2048,
|
|
20
|
+
AZTEC = 4096
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.BarcodeValueType
|
|
24
|
+
*/
|
|
25
|
+
export declare enum BarcodeValueType {
|
|
26
|
+
UNKNOWN = 0,
|
|
27
|
+
CONTACT_INFO = 1,
|
|
28
|
+
EMAIL = 2,
|
|
29
|
+
ISBN = 3,
|
|
30
|
+
PHONE = 4,
|
|
31
|
+
PRODUCT = 5,
|
|
32
|
+
SMS = 6,
|
|
33
|
+
TEXT = 7,
|
|
34
|
+
URL = 8,
|
|
35
|
+
WIFI = 9,
|
|
36
|
+
GEO = 10,
|
|
37
|
+
CALENDAR_EVENT = 11,
|
|
38
|
+
DRIVER_LICENSE = 12
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.Address.AddressType
|
|
42
|
+
*/
|
|
43
|
+
export declare enum AddressType {
|
|
44
|
+
UNKNOWN = 0,
|
|
45
|
+
WORK = 1,
|
|
46
|
+
HOME = 2
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.Address
|
|
50
|
+
*/
|
|
51
|
+
export interface Address {
|
|
52
|
+
addressLines?: string[];
|
|
53
|
+
type?: AddressType;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.PersonName
|
|
57
|
+
*/
|
|
58
|
+
export interface PersonName {
|
|
59
|
+
first?: string;
|
|
60
|
+
formattedName?: string;
|
|
61
|
+
last?: string;
|
|
62
|
+
middle?: string;
|
|
63
|
+
prefix?: string;
|
|
64
|
+
pronunciation?: string;
|
|
65
|
+
suffix?: string;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.ContactInfo
|
|
69
|
+
*/
|
|
70
|
+
export interface ContactInfo {
|
|
71
|
+
addresses?: Address[];
|
|
72
|
+
emails?: Email[];
|
|
73
|
+
name?: PersonName;
|
|
74
|
+
organization?: string;
|
|
75
|
+
phones?: Phone[];
|
|
76
|
+
title?: string;
|
|
77
|
+
urls?: string[];
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.Email.FormatType
|
|
81
|
+
*/
|
|
82
|
+
export declare enum EmailType {
|
|
83
|
+
UNKNOWN = 0,
|
|
84
|
+
WORK = 1,
|
|
85
|
+
HOME = 2
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.Email
|
|
89
|
+
*/
|
|
90
|
+
export interface Email {
|
|
91
|
+
address?: string;
|
|
92
|
+
body?: string;
|
|
93
|
+
subject?: string;
|
|
94
|
+
type?: EmailType;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.Phone.FormatType
|
|
98
|
+
*/
|
|
99
|
+
export declare enum PhoneType {
|
|
100
|
+
UNKNOWN = 0,
|
|
101
|
+
WORK = 1,
|
|
102
|
+
HOME = 2,
|
|
103
|
+
FAX = 3,
|
|
104
|
+
MOBILE = 4
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.Phone
|
|
108
|
+
*/
|
|
109
|
+
export interface Phone {
|
|
110
|
+
number?: string;
|
|
111
|
+
type?: PhoneType;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.Sms
|
|
115
|
+
*/
|
|
116
|
+
export interface Sms {
|
|
117
|
+
message?: string;
|
|
118
|
+
phoneNumber?: string;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.UrlBookmark
|
|
122
|
+
*/
|
|
123
|
+
export interface UrlBookmark {
|
|
124
|
+
title?: string;
|
|
125
|
+
url?: string;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.WiFi.EncryptionType
|
|
129
|
+
*/
|
|
130
|
+
export declare enum EncryptionType {
|
|
131
|
+
OPEN = 1,
|
|
132
|
+
WPA = 2,
|
|
133
|
+
WEP = 3
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.WiFi
|
|
137
|
+
*/
|
|
138
|
+
export interface Wifi {
|
|
139
|
+
encryptionType?: EncryptionType;
|
|
140
|
+
password?: string;
|
|
141
|
+
ssid?: string;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.GeoPoint
|
|
145
|
+
*/
|
|
146
|
+
export interface GeoPoint {
|
|
147
|
+
lat?: number;
|
|
148
|
+
lng?: number;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.CalendarDateTime
|
|
152
|
+
*/
|
|
153
|
+
export interface Date {
|
|
154
|
+
day: number;
|
|
155
|
+
hours: number;
|
|
156
|
+
minutes: number;
|
|
157
|
+
month: number;
|
|
158
|
+
rawValue: string;
|
|
159
|
+
seconds: number;
|
|
160
|
+
year: number;
|
|
161
|
+
isUtc: boolean;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.CalendarEvent
|
|
165
|
+
*/
|
|
166
|
+
export interface CalendarEvent {
|
|
167
|
+
description?: string;
|
|
168
|
+
end?: Date;
|
|
169
|
+
location?: string;
|
|
170
|
+
organizer?: string;
|
|
171
|
+
start?: Date;
|
|
172
|
+
status?: string;
|
|
173
|
+
summary?: string;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode.DriverLicense
|
|
177
|
+
*/
|
|
178
|
+
export interface DriverLicense {
|
|
179
|
+
addressCity?: string;
|
|
180
|
+
addressState?: string;
|
|
181
|
+
addressStreet?: string;
|
|
182
|
+
addressZip?: string;
|
|
183
|
+
birthDate?: string;
|
|
184
|
+
documentType?: string;
|
|
185
|
+
expiryDate?: string;
|
|
186
|
+
firstName?: string;
|
|
187
|
+
gender?: string;
|
|
188
|
+
issueDate?: string;
|
|
189
|
+
issuingCountry?: string;
|
|
190
|
+
lastName?: string;
|
|
191
|
+
licenseNumber?: string;
|
|
192
|
+
middleName?: string;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* @see https://developer.android.com/reference/android/graphics/Rect.html
|
|
196
|
+
*/
|
|
197
|
+
export interface Rect {
|
|
198
|
+
bottom: number;
|
|
199
|
+
left: number;
|
|
200
|
+
right: number;
|
|
201
|
+
top: number;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* @see https://developer.android.com/reference/android/graphics/Point.html
|
|
205
|
+
*/
|
|
206
|
+
export interface Point {
|
|
207
|
+
x: number;
|
|
208
|
+
y: number;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* @see https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/Barcode
|
|
212
|
+
*/
|
|
213
|
+
export type Barcode = {
|
|
214
|
+
boundingBox?: Rect;
|
|
215
|
+
cornerPoints?: Point[];
|
|
216
|
+
displayValue?: string;
|
|
217
|
+
rawValue?: string;
|
|
218
|
+
format: BarcodeFormat;
|
|
219
|
+
content: {
|
|
220
|
+
type: BarcodeValueType.UNKNOWN | BarcodeValueType.ISBN | BarcodeValueType.TEXT;
|
|
221
|
+
data: string;
|
|
222
|
+
} | {
|
|
223
|
+
type: BarcodeValueType.CONTACT_INFO;
|
|
224
|
+
data: ContactInfo;
|
|
225
|
+
} | {
|
|
226
|
+
type: BarcodeValueType.EMAIL;
|
|
227
|
+
data: Email;
|
|
228
|
+
} | {
|
|
229
|
+
type: BarcodeValueType.PHONE;
|
|
230
|
+
data: Phone;
|
|
231
|
+
} | {
|
|
232
|
+
type: BarcodeValueType.SMS;
|
|
233
|
+
data: Sms;
|
|
234
|
+
} | {
|
|
235
|
+
type: BarcodeValueType.URL;
|
|
236
|
+
data: UrlBookmark;
|
|
237
|
+
} | {
|
|
238
|
+
type: BarcodeValueType.WIFI;
|
|
239
|
+
data: Wifi;
|
|
240
|
+
} | {
|
|
241
|
+
type: BarcodeValueType.GEO;
|
|
242
|
+
data: GeoPoint;
|
|
243
|
+
} | {
|
|
244
|
+
type: BarcodeValueType.CALENDAR_EVENT;
|
|
245
|
+
data: CalendarEvent;
|
|
246
|
+
} | {
|
|
247
|
+
type: BarcodeValueType.DRIVER_LICENSE;
|
|
248
|
+
data: DriverLicense;
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
export interface CodeScannerOptions {
|
|
252
|
+
/**
|
|
253
|
+
* checkInverted: `Allows you to also scan white barcode with black backgrounds`
|
|
254
|
+
*
|
|
255
|
+
* 注意:鸿蒙(HarmonyOS)Scan Kit 无图像反色识别选项,此选项在鸿蒙端被忽略。
|
|
256
|
+
*/
|
|
257
|
+
checkInverted?: boolean;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* 将 JS 层 BarcodeFormat[] 转换为 vision-camera codeScanner 需要的 CodeType[]。
|
|
261
|
+
*
|
|
262
|
+
* - 空数组或包含 ALL_FORMATS:返回空数组,触发 Scan Kit customScan 的 ALL 默认扫描。
|
|
263
|
+
* - 其余格式经 FORMAT_TO_CODE_TYPE 映射(含上游 off-by-one 补偿)。
|
|
264
|
+
*
|
|
265
|
+
* @internal
|
|
266
|
+
*/
|
|
267
|
+
export declare function mapFormatsToCodeTypes(types: BarcodeFormat[]): CodeType[];
|
|
268
|
+
/**
|
|
269
|
+
* 将 vision-camera 扫码回调的 Code 转换为原 API 的 Barcode 类型。
|
|
270
|
+
*
|
|
271
|
+
* 鸿蒙 Scan Kit 的 ScanResult 仅提供 originalValue / scanCodeRect / cornerPoints,
|
|
272
|
+
* 无 MLKit/iOS Vision 的丰富结构化字段,因此 content 统一降级为
|
|
273
|
+
* `{ type: BarcodeValueType.TEXT, data: rawValue }`。
|
|
274
|
+
*
|
|
275
|
+
* @internal
|
|
276
|
+
*/
|
|
277
|
+
export declare function toBarcode(code: Code): Barcode;
|
|
278
|
+
/**
|
|
279
|
+
* 按调用方请求的格式,归一化 UPC_A / EAN_13 上报结果。
|
|
280
|
+
*
|
|
281
|
+
* UPC_A 与「前导 0 的 EAN_13」条纹图案完全等价,Scan Kit / 多数扫码库常把 UPC_A
|
|
282
|
+
* 报成 EAN_13(13 位且以 0 开头),或把 EAN_13 报成 UPC_A(去掉前导 0)。
|
|
283
|
+
* 当请求格式仅包含其中一种时,按请求意图纠正 format 与数值,便于格式过滤用例验证。
|
|
284
|
+
*
|
|
285
|
+
* @internal
|
|
286
|
+
*/
|
|
287
|
+
export declare function normalizeReportedBarcode(barcode: Barcode, requestedTypes: BarcodeFormat[]): Barcode;
|
|
288
|
+
/**
|
|
289
|
+
* Scans barcodes in the passed frame with MLKit
|
|
290
|
+
*
|
|
291
|
+
* @param frame Camera frame
|
|
292
|
+
* @param types Array of barcode types to detect (for optimal performance, use less types)
|
|
293
|
+
* @param options Scan options
|
|
294
|
+
* @returns Detected barcodes from MLKit
|
|
295
|
+
*
|
|
296
|
+
* 鸿蒙端不支持:Frame Processor 工作集机制在 @react-native-ohos/react-native-vision-camera v4
|
|
297
|
+
* 中不存在(不导出 useFrameProcessor / FrameProcessorPlugin)。此函数在鸿蒙端导出为抛错 stub,
|
|
298
|
+
* 请改用 `useScanBarcodes` 返回的 codeScanner 并传给 `<Camera codeScanner={...}>`。
|
|
299
|
+
*/
|
|
300
|
+
export declare function scanBarcodes(_frame: Frame, _types: BarcodeFormat[], _options?: CodeScannerOptions): Barcode[];
|
|
301
|
+
export * from './hook';
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hxa-rn/vision-camera-code-scanner",
|
|
3
|
+
"displayName": "@hxa-rn/vision-camera-code-scanner",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"description": "VisionCamera Frame Processor Plugin to read barcodes using MLKit Vision Barcode Scanning",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"module": "./src/index.ts",
|
|
8
|
+
"react-native": "./src/index.ts",
|
|
9
|
+
"types": "./src/index.ts",
|
|
10
|
+
"source": "./src/index.ts",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"private": false,
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/rodgomesc/vision-camera-code-scanner"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"react-native",
|
|
19
|
+
"harmony",
|
|
20
|
+
"harmonyos"
|
|
21
|
+
],
|
|
22
|
+
"homepage": "https://github.com/rodgomesc/vision-camera-code-scanner#readme",
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"registry": "https://registry.npmjs.org/"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist/",
|
|
28
|
+
"src/",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"prepare": "bob build"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"react-native": ">=0.72",
|
|
37
|
+
"react": "*",
|
|
38
|
+
"@react-native-ohos/react-native-vision-camera": "~4.0.3"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"typescript": "5.0.4",
|
|
42
|
+
"@react-native/typescript-config": "0.79.5",
|
|
43
|
+
"react": "18.2.0",
|
|
44
|
+
"@types/react": "18.2.79",
|
|
45
|
+
"react-native": "0.72.5",
|
|
46
|
+
"react-native-vision-camera": "~4.0.3",
|
|
47
|
+
"jest": "^29.6.3",
|
|
48
|
+
"react-native-builder-bob": "^0.18.0",
|
|
49
|
+
"@react-native-oh/react-native-harmony-cli": "^0.77.50",
|
|
50
|
+
"metro": "^0.81.0"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=18"
|
|
54
|
+
},
|
|
55
|
+
"harmony": {
|
|
56
|
+
"alias": "vision-camera-code-scanner"
|
|
57
|
+
},
|
|
58
|
+
"react-native-builder-bob": {
|
|
59
|
+
"source": "src",
|
|
60
|
+
"output": "dist",
|
|
61
|
+
"targets": [
|
|
62
|
+
"commonjs",
|
|
63
|
+
"module",
|
|
64
|
+
"typescript"
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
}
|
package/src/hook.tsx
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Code, CodeScanner, CodeScannerFrame, useCodeScanner } from 'react-native-vision-camera';
|
|
2
|
+
import { useCallback, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
Barcode,
|
|
6
|
+
BarcodeFormat,
|
|
7
|
+
CodeScannerOptions,
|
|
8
|
+
mapFormatsToCodeTypes,
|
|
9
|
+
normalizeReportedBarcode,
|
|
10
|
+
toBarcode,
|
|
11
|
+
} from '.';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 鸿蒙端 useScanBarcodes 重实现。
|
|
15
|
+
*
|
|
16
|
+
* 鸿蒙版 @react-native-ohos/react-native-vision-camera v4 不提供 Frame Processor 机制
|
|
17
|
+
* (不导出 useFrameProcessor / FrameProcessorPlugin),但内置 useCodeScanner + Camera.codeScanner
|
|
18
|
+
* (底层为 Scan Kit customScan)。因此本 hook 基于 useCodeScanner 构造扫码器,在 onCodeScanned
|
|
19
|
+
* 回调中把 Code[] 转换为原 API 的 Barcode[]。
|
|
20
|
+
*
|
|
21
|
+
* ⚠️ API 变更:返回值从 `[frameProcessor, barcodes]` 变为 `[codeScanner, barcodes, clearBarcodes]`。
|
|
22
|
+
* 调用方需将 codeScanner 传给 `<Camera codeScanner={codeScanner}>`,而非 frameProcessor。
|
|
23
|
+
* 停止扫描时应调用 clearBarcodes(),避免下次开始扫描时把上次结果当作新识别上报。
|
|
24
|
+
*
|
|
25
|
+
* @param types 要检测的条码格式数组;包含 ALL_FORMATS 或为空数组时检测所有格式。
|
|
26
|
+
* @param options 扫描选项;checkInverted(反色条码)在鸿蒙端被忽略(Scan Kit 无反色识别选项)。
|
|
27
|
+
* @returns [codeScanner, barcodes, clearBarcodes]
|
|
28
|
+
*/
|
|
29
|
+
export function useScanBarcodes(
|
|
30
|
+
types: BarcodeFormat[],
|
|
31
|
+
_options?: CodeScannerOptions
|
|
32
|
+
): [CodeScanner, Barcode[], () => void] {
|
|
33
|
+
const [barcodes, setBarcodes] = useState<Barcode[]>([]);
|
|
34
|
+
|
|
35
|
+
const clearBarcodes = useCallback(() => {
|
|
36
|
+
setBarcodes([]);
|
|
37
|
+
}, []);
|
|
38
|
+
|
|
39
|
+
const codeScanner = useCodeScanner({
|
|
40
|
+
codeTypes: mapFormatsToCodeTypes(types),
|
|
41
|
+
onCodeScanned: (codes: Code[], _frame: CodeScannerFrame) => {
|
|
42
|
+
setBarcodes(
|
|
43
|
+
codes.map((code) => normalizeReportedBarcode(toBarcode(code), types))
|
|
44
|
+
);
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return [codeScanner, barcodes, clearBarcodes];
|
|
49
|
+
}
|