@octane-xplat/barcode-scanner 0.19.0 → 0.20.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 +2 -63
- package/package.json +6 -6
- package/src/BarcodeScanner.macos.tsrx +5 -6
- package/src/BarcodeScanner.tsrx +96 -88
- package/src/BarcodeScanner.web.tsrx +124 -117
- package/src/barcode-scanner.macos.ts +147 -107
- package/src/barcode-scanner.ts +114 -99
- package/src/barcode-scanner.web.ts +48 -39
- package/src/barcode-scanner.windows.ts +5 -4
- package/src/format-map.ts +16 -16
- package/src/format-map.web.ts +16 -16
- package/src/native-elements.d.ts +3 -3
- package/src/props.ts +31 -31
- package/src/types.ts +27 -27
- package/types/generated/.tsrx-typegen-manifest.json +0 -10
- package/types/generated/BarcodeScanner.macos.d.ts +0 -3
- package/types/generated/BarcodeScanner.web.d.ts +0 -6
- package/types/generated/barcode-scanner.linux.d.ts +0 -1
- package/types/generated/barcode-scanner.macos.d.ts +0 -3
- package/types/generated/barcode-scanner.web.d.ts +0 -3
- package/types/generated/barcode-scanner.windows.d.ts +0 -3
- package/types/generated/format-map.web.d.ts +0 -3
- package/types/generated/index.macos.d.ts +0 -4
- package/types/generated/index.web.d.ts +0 -4
- package/types/generated/native-elements.d.ts +0 -7
|
@@ -2,137 +2,177 @@
|
|
|
2
2
|
// VNDetectBarcodesRequest; live camera scanning is not available on macOS,
|
|
3
3
|
// so ensure() reports 'unsupported'.
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
CGImageGetHeight,
|
|
6
|
+
CGImageGetWidth,
|
|
7
|
+
NSImage,
|
|
8
|
+
NSURL,
|
|
9
|
+
VNDetectBarcodesRequest,
|
|
10
|
+
VNImageRequestHandler,
|
|
11
11
|
} from '@octane-xplat/platform/macos'
|
|
12
|
+
|
|
12
13
|
import type { BarcodeFormat, BarcodeScannerService, PermissionResult, ScannedCode } from './types'
|
|
13
14
|
|
|
14
15
|
// String literals, not the bound constants — the generated metadata targets
|
|
15
16
|
// macOS 11 and omits VNBarcodeSymbologyCodabar.
|
|
16
17
|
const VISION_PREFIX = 'VNBarcodeSymbology'
|
|
17
18
|
const SYMBOLOGIES: Record<BarcodeFormat, readonly string[]> = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
19
|
+
qr: ['QR'],
|
|
20
|
+
ean13: ['EAN13'],
|
|
21
|
+
ean8: ['EAN8'],
|
|
22
|
+
upc_a: ['EAN13'],
|
|
23
|
+
upc_e: ['UPCE'],
|
|
24
|
+
code128: ['Code128'],
|
|
25
|
+
code39: ['Code39', 'Code39Checksum', 'Code39FullASCII', 'Code39FullASCIIChecksum'],
|
|
26
|
+
code93: ['Code93', 'Code93i'],
|
|
27
|
+
codabar: ['Codabar'],
|
|
28
|
+
itf: ['I2of5', 'I2of5Checksum', 'ITF14'],
|
|
29
|
+
datamatrix: ['DataMatrix'],
|
|
30
|
+
pdf417: ['PDF417'],
|
|
31
|
+
aztec: ['Aztec'],
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
// Vision → our vocabulary. EAN13 resolves to ean13; UPC-A is an EAN-13 with
|
|
34
35
|
// a leading 0 and is rewritten on the payload (see below).
|
|
35
36
|
const FORMATS: Record<string, string> = {}
|
|
37
|
+
|
|
36
38
|
for (const [format, names] of Object.entries(SYMBOLOGIES)) {
|
|
37
|
-
|
|
39
|
+
for (const name of names) {
|
|
40
|
+
FORMATS[VISION_PREFIX + name] ??= format
|
|
41
|
+
}
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
/** The deduplicated, prefixed union for the requested formats — every entry
|
|
41
45
|
* when `formats` is missing or empty, so results stay inside our vocabulary. */
|
|
42
46
|
function symbologiesFor(formats?: BarcodeFormat[]): string[] {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const wanted = formats?.length ? formats : (Object.keys(SYMBOLOGIES) as BarcodeFormat[])
|
|
48
|
+
const out = new Set<string>()
|
|
49
|
+
|
|
50
|
+
for (const format of wanted) {
|
|
51
|
+
for (const name of SYMBOLOGIES[format] ?? []) {
|
|
52
|
+
out.add(VISION_PREFIX + name)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return [...out]
|
|
49
57
|
}
|
|
50
58
|
|
|
51
59
|
/** request.results may be a JS array or an NSArray — count/objectAtIndex. */
|
|
52
60
|
function observations(results: any): any[] {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
61
|
+
if (!results) {
|
|
62
|
+
return []
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (Array.isArray(results)) {
|
|
66
|
+
return results
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const out: any[] = []
|
|
70
|
+
|
|
71
|
+
for (let i = 0, count = Number(results.count ?? 0); i < count; i++) {
|
|
72
|
+
out.push(results.objectAtIndex(i))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return out
|
|
60
76
|
}
|
|
61
77
|
|
|
62
78
|
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
63
79
|
export const barcodeScanner: BarcodeScannerService = {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
80
|
+
supported: true,
|
|
81
|
+
ensure: async (): Promise<PermissionResult> => 'unsupported',
|
|
82
|
+
scanImage: async (image: unknown, formats?: BarcodeFormat[]): Promise<ScannedCode[]> => {
|
|
83
|
+
let source: any
|
|
84
|
+
|
|
85
|
+
if (typeof image === 'string' || (image != null && typeof (image as any).uri === 'string')) {
|
|
86
|
+
const input = typeof image === 'string' ? image : (image as any).uri
|
|
87
|
+
|
|
88
|
+
if (/^file:/i.test(input)) {
|
|
89
|
+
source = NSImage.alloc().initWithContentsOfURL(NSURL.URLWithString(input))
|
|
90
|
+
} else if (/^[a-z][a-z0-9+.-]*:/i.test(input)) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
'barcodeScanner.scanImage: macOS decodes local files; download remote images first',
|
|
93
|
+
)
|
|
94
|
+
} else {
|
|
95
|
+
source = NSImage.alloc().initWithContentsOfURL(NSURL.fileURLWithPath(input))
|
|
96
|
+
}
|
|
97
|
+
} else if (
|
|
98
|
+
image != null &&
|
|
99
|
+
typeof (image as any).CGImageForProposedRectContextHints === 'function'
|
|
100
|
+
) {
|
|
101
|
+
source = image
|
|
102
|
+
} else {
|
|
103
|
+
throw new Error(
|
|
104
|
+
'barcodeScanner.scanImage: expected a file path, file URL, {uri} ref, or NSImage',
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const cg = source?.CGImageForProposedRectContextHints?.(null, null, null)
|
|
109
|
+
|
|
110
|
+
if (!cg) {
|
|
111
|
+
throw new Error('barcodeScanner.scanImage: could not read the image')
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const request = VNDetectBarcodesRequest.alloc().init()
|
|
115
|
+
|
|
116
|
+
request.symbologies = symbologiesFor(formats)
|
|
117
|
+
|
|
118
|
+
const handler = VNImageRequestHandler.alloc().initWithCGImageOptions(cg, {})
|
|
119
|
+
|
|
120
|
+
if (!handler.performRequestsError([request], null)) {
|
|
121
|
+
throw new Error('barcodeScanner.scanImage: Vision could not process the image')
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Vision reports UPC-A as EAN-13 with a leading 0.
|
|
125
|
+
const wantsUpcA = !formats?.length || formats.includes('upc_a')
|
|
126
|
+
const wantsEan13 = !formats?.length || formats.includes('ean13')
|
|
127
|
+
const W = Number(CGImageGetWidth(cg))
|
|
128
|
+
const H = Number(CGImageGetHeight(cg))
|
|
129
|
+
const codes: ScannedCode[] = []
|
|
130
|
+
|
|
131
|
+
for (const observation of observations(request.results)) {
|
|
132
|
+
const symbology = String(observation?.symbology ?? '')
|
|
133
|
+
let type =
|
|
134
|
+
FORMATS[symbology] ??
|
|
135
|
+
(symbology.startsWith(VISION_PREFIX) ? symbology.slice(VISION_PREFIX.length) : symbology)
|
|
136
|
+
|
|
137
|
+
let data = String(observation?.payloadStringValue ?? '')
|
|
138
|
+
|
|
139
|
+
if (type === 'ean13') {
|
|
140
|
+
if (data.length === 13 && data.startsWith('0') && wantsUpcA) {
|
|
141
|
+
type = 'upc_a'
|
|
142
|
+
data = data.slice(1)
|
|
143
|
+
} else if (!wantsEan13) {
|
|
144
|
+
continue
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const code: ScannedCode = { type, data }
|
|
149
|
+
const box = observation?.boundingBox
|
|
150
|
+
|
|
151
|
+
if (box) {
|
|
152
|
+
const ox = Number(box.origin?.x)
|
|
153
|
+
const oy = Number(box.origin?.y)
|
|
154
|
+
const width = Number(box.size?.width)
|
|
155
|
+
const height = Number(box.size?.height)
|
|
156
|
+
|
|
157
|
+
if (
|
|
158
|
+
Number.isFinite(ox) &&
|
|
159
|
+
Number.isFinite(oy) &&
|
|
160
|
+
Number.isFinite(width) &&
|
|
161
|
+
Number.isFinite(height)
|
|
162
|
+
) {
|
|
163
|
+
// Vision bounds are normalized with a bottom-left origin.
|
|
164
|
+
code.bounds = {
|
|
165
|
+
x: ox * W,
|
|
166
|
+
y: (1 - oy - height) * H,
|
|
167
|
+
width: width * W,
|
|
168
|
+
height: height * H,
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
codes.push(code)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return codes
|
|
177
|
+
},
|
|
138
178
|
}
|
package/src/barcode-scanner.ts
CHANGED
|
@@ -10,116 +10,131 @@ import { fromNativeFormat, toNativeFormats } from './format-map'
|
|
|
10
10
|
let scanRequestCode = 7723
|
|
11
11
|
|
|
12
12
|
async function ensure(): Promise<PermissionResult> {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
13
|
+
try {
|
|
14
|
+
if (Application.ios) {
|
|
15
|
+
const av = (globalThis as any).AVCaptureDevice
|
|
16
|
+
const video = (globalThis as any).AVMediaTypeVideo
|
|
17
|
+
// 0=notDetermined 1=restricted 2=denied 3=authorized
|
|
18
|
+
const status = av?.authorizationStatusForMediaType?.(video)
|
|
19
|
+
|
|
20
|
+
if (status === 3) {
|
|
21
|
+
return 'granted'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (status === 1 || status === 2) {
|
|
25
|
+
return 'denied'
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof av?.requestAccessForMediaTypeCompletionHandler !== 'function') {
|
|
29
|
+
return 'unsupported'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return await new Promise<PermissionResult>((resolve) => {
|
|
33
|
+
av.requestAccessForMediaTypeCompletionHandler(video, (granted: boolean) =>
|
|
34
|
+
resolve(granted ? 'granted' : 'denied'),
|
|
35
|
+
)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const perm = 'android.permission.CAMERA'
|
|
40
|
+
const activity = Application.android.foregroundActivity ?? Application.android.startActivity
|
|
41
|
+
|
|
42
|
+
if (
|
|
43
|
+
activity?.checkSelfPermission?.(perm) === android.content.pm.PackageManager.PERMISSION_GRANTED
|
|
44
|
+
) {
|
|
45
|
+
return 'granted'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!activity) {
|
|
49
|
+
return 'denied'
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return await new Promise<PermissionResult>((resolve) => {
|
|
53
|
+
const requestCode = scanRequestCode++
|
|
54
|
+
const event = Application.android.activityRequestPermissionsEvent
|
|
55
|
+
const onResult = (args: any) => {
|
|
56
|
+
if (args.requestCode !== requestCode) {
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
Application.android.off(event, onResult)
|
|
61
|
+
resolve(
|
|
62
|
+
args.grantResults?.[0] === android.content.pm.PackageManager.PERMISSION_GRANTED
|
|
63
|
+
? 'granted'
|
|
64
|
+
: 'denied',
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
Application.android.on(event, onResult)
|
|
69
|
+
try {
|
|
70
|
+
activity.requestPermissions([perm], requestCode)
|
|
71
|
+
} catch {
|
|
72
|
+
Application.android.off(event, onResult)
|
|
73
|
+
resolve('denied')
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
} catch {
|
|
77
|
+
return 'denied'
|
|
78
|
+
}
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
async function toImageSource(image: unknown): Promise<any> {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
82
|
+
if (image instanceof ImageSource) {
|
|
83
|
+
return image
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (typeof image === 'string') {
|
|
87
|
+
return ImageSource.fromFile(image)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (
|
|
91
|
+
typeof image === 'object' &&
|
|
92
|
+
image !== null &&
|
|
93
|
+
'uri' in image &&
|
|
94
|
+
typeof image.uri === 'string'
|
|
95
|
+
) {
|
|
96
|
+
return ImageSource.fromFile(image.uri)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
throw new Error('barcodeScanner.scanImage: expected an ImageSource, file path, or {uri} ref')
|
|
88
100
|
}
|
|
89
101
|
|
|
90
102
|
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
91
103
|
export const barcodeScanner: BarcodeScannerService = {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
104
|
+
supported: true,
|
|
105
|
+
|
|
106
|
+
ensure,
|
|
107
|
+
|
|
108
|
+
async scanImage(image: unknown, formats?: BarcodeFormat[]): Promise<ScannedCode[]> {
|
|
109
|
+
const source = await toImageSource(image)
|
|
110
|
+
const result = await detectWithStillImage(source, {
|
|
111
|
+
detectorType: DetectionType.Barcode,
|
|
112
|
+
barcodeScanning: { barcodeFormat: toNativeFormats(formats) as [any] },
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
const list = Array.isArray(result) ? result : Object.values(result ?? {})
|
|
116
|
+
|
|
117
|
+
return list
|
|
118
|
+
.flat()
|
|
119
|
+
.filter((item: any) => item && (item.rawValue !== undefined || item.format !== undefined))
|
|
120
|
+
.map((item: any) => ({
|
|
121
|
+
type: fromNativeFormat(item.format),
|
|
122
|
+
data: String(item.rawValue ?? item.displayValue ?? ''),
|
|
123
|
+
bounds: item.bounds
|
|
124
|
+
? {
|
|
125
|
+
x: item.bounds.origin?.x ?? item.bounds.x ?? 0,
|
|
126
|
+
y: item.bounds.origin?.y ?? item.bounds.y ?? 0,
|
|
127
|
+
width: item.bounds.size?.width ?? item.bounds.width ?? 0,
|
|
128
|
+
height: item.bounds.size?.height ?? item.bounds.height ?? 0,
|
|
129
|
+
}
|
|
130
|
+
: undefined,
|
|
131
|
+
}))
|
|
132
|
+
},
|
|
119
133
|
}
|
|
120
134
|
|
|
121
135
|
// Scanning needs camera access — register the kind this leaf owns for the
|
|
122
136
|
// platform dispatcher (same 'camera' kind media registers; both leafs' owners
|
|
123
137
|
// run identical ensure logic, and the leaf must work with media absent).
|
|
124
138
|
const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
|
|
139
|
+
|
|
125
140
|
permissionOwners.camera = () => barcodeScanner.ensure()
|
|
@@ -5,53 +5,62 @@ import type { BarcodeFormat, BarcodeScannerService, PermissionResult, ScannedCod
|
|
|
5
5
|
import { fromDetectorFormat, toDetectorFormats } from './format-map.web'
|
|
6
6
|
|
|
7
7
|
const detector = () => {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
const Detector = (globalThis as any).BarcodeDetector
|
|
9
|
+
|
|
10
|
+
return typeof Detector === 'function' ? Detector : null
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
async function toImageSource(image: unknown): Promise<any> {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
if (typeof image === 'string') {
|
|
15
|
+
const response = await fetch(image)
|
|
16
|
+
|
|
17
|
+
return createImageBitmap(await response.blob())
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return image
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
21
24
|
export const barcodeScanner: BarcodeScannerService = {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
25
|
+
supported: detector() !== null,
|
|
26
|
+
|
|
27
|
+
ensure(): Promise<PermissionResult> {
|
|
28
|
+
return Promise.resolve(detector() ? 'granted' : 'unsupported')
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
async scanImage(image: unknown, formats?: BarcodeFormat[]): Promise<ScannedCode[]> {
|
|
32
|
+
const Detector = detector()
|
|
33
|
+
|
|
34
|
+
if (!Detector) {
|
|
35
|
+
throw new Error('unsupported: BarcodeDetector is not available')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let d: any
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
d = new Detector({ formats: toDetectorFormats(formats) })
|
|
42
|
+
} catch {
|
|
43
|
+
d = new Detector()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const source = await toImageSource(image)
|
|
47
|
+
const codes = await d.detect(source)
|
|
48
|
+
|
|
49
|
+
return (codes ?? []).map((code: any) => ({
|
|
50
|
+
type: fromDetectorFormat(code.format),
|
|
51
|
+
data: String(code.rawValue ?? ''),
|
|
52
|
+
bounds: code.boundingBox
|
|
53
|
+
? {
|
|
54
|
+
x: code.boundingBox.x,
|
|
55
|
+
y: code.boundingBox.y,
|
|
56
|
+
width: code.boundingBox.width,
|
|
57
|
+
height: code.boundingBox.height,
|
|
58
|
+
}
|
|
59
|
+
: undefined,
|
|
60
|
+
}))
|
|
61
|
+
},
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
|
|
65
|
+
|
|
57
66
|
permissionOwners.camera = () => barcodeScanner.ensure()
|
|
@@ -3,11 +3,12 @@ import type { BarcodeScannerService, PermissionResult } from './types'
|
|
|
3
3
|
|
|
4
4
|
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
5
5
|
export const barcodeScanner: BarcodeScannerService = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
supported: false,
|
|
7
|
+
ensure: async (): Promise<PermissionResult> => 'unsupported',
|
|
8
|
+
scanImage: () =>
|
|
9
|
+
Promise.reject(new Error('unsupported: barcode scanning is not available on windows')),
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
|
|
13
|
+
|
|
13
14
|
permissionOwners.camera = () => barcodeScanner.ensure()
|