@octane-xplat/barcode-scanner 0.18.0 → 0.19.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 +55 -16
- package/package.json +91 -97
- package/src/BarcodeScanner.macos.tsrx +16 -0
- package/src/BarcodeScanner.tsrx +5 -2
- package/src/BarcodeScanner.web.tsrx +14 -2
- package/src/barcode-scanner.macos.ts +134 -8
- package/src/barcode-scanner.ts +6 -0
- package/src/barcode-scanner.web.ts +1 -0
- package/src/barcode-scanner.windows.ts +3 -1
- package/src/index.macos.ts +1 -0
- package/src/native-elements.d.ts +4 -5
- package/src/props.ts +9 -0
- package/src/types.ts +3 -0
- package/types/generated/.tsrx-typegen-manifest.json +1 -0
- package/types/generated/BarcodeScanner.macos.d.ts +3 -0
- package/types/generated/BarcodeScanner.web.d.ts +2 -1
- package/types/generated/barcode-scanner.d.ts +1 -0
- package/types/generated/barcode-scanner.macos.d.ts +1 -0
- package/types/generated/barcode-scanner.web.d.ts +1 -0
- package/types/generated/barcode-scanner.windows.d.ts +1 -0
- package/types/generated/index.macos.d.ts +1 -0
- package/types/generated/native-elements.d.ts +4 -5
- package/types/generated/props.d.ts +9 -0
- package/types/generated/types.d.ts +3 -0
- package/LICENSE +0 -21
package/README.md
CHANGED
|
@@ -1,27 +1,66 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @octane-xplat/barcode-scanner
|
|
2
|
+
|
|
3
|
+
Read QR codes and barcodes from a live camera or an existing image. iOS and
|
|
4
|
+
Android use ML Kit; native macOS decodes images with Apple's Vision framework;
|
|
5
|
+
browser availability depends on `BarcodeDetector`.
|
|
6
|
+
Use [media picking](https://octane-xplat.goddardai.org/platform-services#pick-and-capture-images)
|
|
7
|
+
when the app needs to choose or capture a photo first.
|
|
8
|
+
|
|
9
|
+
## Decode a QR image
|
|
10
|
+
|
|
11
|
+
Install in an existing Xplat app:
|
|
2
12
|
|
|
3
13
|
```sh
|
|
4
|
-
|
|
14
|
+
bun add @octane-xplat/barcode-scanner
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Call this function with a readable image: a native path or image source on
|
|
18
|
+
phones, or a browser Blob/image element. It returns the first QR payload,
|
|
19
|
+
`null` when no QR code is found, or `null` when decoding is unavailable:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { barcodeScanner } from '@octane-xplat/barcode-scanner'
|
|
23
|
+
|
|
24
|
+
export async function readQr(image: unknown): Promise<string | null> {
|
|
25
|
+
if (!barcodeScanner.supported) return null
|
|
26
|
+
const codes = await barcodeScanner.scanImage(image, ['qr'])
|
|
27
|
+
return codes[0]?.data ?? null
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Try a known QR image, then an image without a code. Surface read/decode errors
|
|
32
|
+
to the person using the app; do not treat a decoding failure as a valid code.
|
|
33
|
+
|
|
34
|
+
On native macOS, `scanImage` reads a local file path, a `file:` URL, a picked
|
|
35
|
+
file's `{ uri }` or an `NSImage`. Remote URLs are rejected, so download the
|
|
36
|
+
image first. Bounds are in the image's pixels, measured from the top-left
|
|
37
|
+
corner. Live camera scanning is not available on macOS, so `ensure()` returns
|
|
38
|
+
`'unsupported'` there. Decoding has not yet been checked on a running Mac.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { barcodeScanner } from '@octane-xplat/barcode-scanner'
|
|
42
|
+
|
|
43
|
+
export async function readProductCode(path: string): Promise<string | null> {
|
|
44
|
+
const codes = await barcodeScanner.scanImage(path, ['ean13', 'upc_a'])
|
|
45
|
+
return codes[0]?.data ?? null
|
|
46
|
+
}
|
|
5
47
|
```
|
|
6
48
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
`BarcodeDetector` API on web. Decoding only: photo capture stays in
|
|
10
|
-
`@octane-xplat/camera`, gallery picking in `@octane-xplat/media`.
|
|
49
|
+
For live capture, configure camera usage text in the native app and request
|
|
50
|
+
permission from a user action before mounting the scanner:
|
|
11
51
|
|
|
12
52
|
```tsx
|
|
13
53
|
import { BarcodeScanner, barcodeScanner } from '@octane-xplat/barcode-scanner'
|
|
14
54
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
torch={false}
|
|
19
|
-
/>
|
|
55
|
+
export async function canOpenScanner() {
|
|
56
|
+
return barcodeScanner.supported && (await barcodeScanner.ensure()) === 'granted'
|
|
57
|
+
}
|
|
20
58
|
|
|
21
|
-
|
|
22
|
-
|
|
59
|
+
export function QrScanner({ onCode }: { onCode: (data: string) => void }) {
|
|
60
|
+
return <BarcodeScanner formats={['qr']} onCodeScanned={({ data }) => onCode(data)} />
|
|
61
|
+
}
|
|
23
62
|
```
|
|
24
63
|
|
|
25
|
-
`
|
|
26
|
-
|
|
27
|
-
|
|
64
|
+
Render `QrScanner` only after `canOpenScanner()` succeeds. Treat the decoded
|
|
65
|
+
text as input for your app to interpret. See [device capability handling](https://octane-xplat.goddardai.org/platform-services#the-shared-service-shape)
|
|
66
|
+
for unavailable features and permission results.
|
package/package.json
CHANGED
|
@@ -1,98 +1,92 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
"scripts": {
|
|
94
|
-
"typegen": "tsrx-typegen --target octane",
|
|
95
|
-
"pack:check": "tsrx-typegen --pack-check",
|
|
96
|
-
"typecheck": "node ../../scripts/typecheck.ts"
|
|
97
|
-
}
|
|
98
|
-
}
|
|
2
|
+
"name": "@octane-xplat/barcode-scanner",
|
|
3
|
+
"version": "0.19.0",
|
|
4
|
+
"description": "Barcode and QR scanning for Octane xplat — ML Kit camera preview + still decode on iOS/Android, Vision still decode on macOS, BarcodeDetector on web",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"android",
|
|
7
|
+
"cross-platform",
|
|
8
|
+
"nativescript",
|
|
9
|
+
"octane",
|
|
10
|
+
"xplat",
|
|
11
|
+
"barcode",
|
|
12
|
+
"qr",
|
|
13
|
+
"scanner",
|
|
14
|
+
"mlkit"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/barcode-scanner",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
21
|
+
"directory": "packages/barcode-scanner"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"platforms",
|
|
26
|
+
"types",
|
|
27
|
+
"!src/**/*.test.*",
|
|
28
|
+
"!src/**/*.spec.*"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./types/generated/index.d.ts",
|
|
34
|
+
"macos": "./src/index.macos.ts",
|
|
35
|
+
"web": "./src/index.web.ts",
|
|
36
|
+
"native": "./src/index.ts",
|
|
37
|
+
"default": "./src/index.ts"
|
|
38
|
+
},
|
|
39
|
+
"./*": {
|
|
40
|
+
"types": "./types/generated/*.d.ts",
|
|
41
|
+
"default": "./src/*"
|
|
42
|
+
},
|
|
43
|
+
"./*.ts": {
|
|
44
|
+
"types": "./types/generated/*.d.ts",
|
|
45
|
+
"default": "./src/*.ts"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"typegen": "tsrx-typegen --target octane",
|
|
53
|
+
"prepack": "bun run typegen && tsrx-typegen --pack-check",
|
|
54
|
+
"pack:check": "tsrx-typegen --pack-check",
|
|
55
|
+
"test": "node --test tests/*.test.ts",
|
|
56
|
+
"typecheck": "node ../../scripts/typecheck.ts"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@nativescript-community/octane": "0.2.5",
|
|
60
|
+
"@nativescript/core": "9.1.3",
|
|
61
|
+
"@nativescript/types": "9.1.4",
|
|
62
|
+
"@tsrx/typescript-plugin": "0.4.11",
|
|
63
|
+
"octane": "0.16.3",
|
|
64
|
+
"tsrx-typegen": "0.5.0",
|
|
65
|
+
"typescript": "5.9.3"
|
|
66
|
+
},
|
|
67
|
+
"peerDependencies": {
|
|
68
|
+
"@nativescript-community/octane": ">=0.2.1 <1",
|
|
69
|
+
"@nativescript/core": ">=9.1.0 <10",
|
|
70
|
+
"octane": ">=0.6.3 <1"
|
|
71
|
+
},
|
|
72
|
+
"peerDependenciesMeta": {
|
|
73
|
+
"@nativescript-community/octane": {
|
|
74
|
+
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"@nativescript/core": {
|
|
77
|
+
"optional": true
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
"nativescript": {
|
|
81
|
+
"platforms": {
|
|
82
|
+
"android": "9.1.0",
|
|
83
|
+
"ios": "9.1.0"
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"dependencies": {
|
|
87
|
+
"@nativescript/mlkit-barcode-scanning": "3.0.4",
|
|
88
|
+
"@nativescript/mlkit-core": "3.0.4",
|
|
89
|
+
"@octane-xplat/jsx": "0.19.0",
|
|
90
|
+
"@octane-xplat/platform": "0.19.0"
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** @jsxImportSource @octane-xplat/jsx */
|
|
2
|
+
// Live barcode scanner — AppKit stub. Still-image decoding works through
|
|
3
|
+
// barcodeScanner.scanImage (Vision); live camera scanning needs an
|
|
4
|
+
// AVCaptureVideoDataOutput frame path the camera leaf does not expose yet
|
|
5
|
+
// (decision #140). Renders nothing and reports once via onError.
|
|
6
|
+
import { useEffect } from 'octane'
|
|
7
|
+
import type { BarcodeScannerProps } from './props'
|
|
8
|
+
import type { JSX } from 'octane/jsx-runtime'
|
|
9
|
+
|
|
10
|
+
export function BarcodeScanner(props: BarcodeScannerProps): JSX.Element @{
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
props.onError?.({ message: 'unsupported: live barcode scanning is not wired on macOS' })
|
|
13
|
+
}, [])
|
|
14
|
+
|
|
15
|
+
<masonview style={props.style} className={props.className} />
|
|
16
|
+
}
|
package/src/BarcodeScanner.tsrx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @jsxImportSource @
|
|
1
|
+
/** @jsxImportSource @octane-xplat/jsx */
|
|
2
2
|
import { useImperativeHandle, useCallback, useEffect, useRef } from 'octane'
|
|
3
3
|
import { isAndroid } from '@nativescript/core'
|
|
4
4
|
import { registerElement } from '@nativescript-community/octane'
|
|
@@ -17,7 +17,7 @@ function applyEscapeProps(view: any, props: BarcodeScannerProps): void {
|
|
|
17
17
|
|
|
18
18
|
/** Live barcode/QR scanner — ML Kit's camera preview with `detectionType:
|
|
19
19
|
* barcode`. Decoding only; photo capture stays in @octane-xplat/camera. */
|
|
20
|
-
export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
20
|
+
export function BarcodeScanner(props: BarcodeScannerProps): unknown @{
|
|
21
21
|
const host = useRef<MLKitView | null>(null)
|
|
22
22
|
|
|
23
23
|
const bindHost = useCallback((v: MLKitView | null) => {
|
|
@@ -44,6 +44,7 @@ export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
|
44
44
|
if (!v) {
|
|
45
45
|
return
|
|
46
46
|
}
|
|
47
|
+
|
|
47
48
|
v.detectionType = DetectionType.Barcode
|
|
48
49
|
v.on(MLKitView.detectionEvent, (e: any) => {
|
|
49
50
|
const data = e?.data ?? e?.data?.data ?? e
|
|
@@ -52,6 +53,7 @@ export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
|
52
53
|
if (!item) {
|
|
53
54
|
continue
|
|
54
55
|
}
|
|
56
|
+
|
|
55
57
|
const bounds = item.bounds
|
|
56
58
|
props.onCodeScanned?.({
|
|
57
59
|
type: fromNativeFormat(item.format),
|
|
@@ -67,6 +69,7 @@ export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
|
67
69
|
})
|
|
68
70
|
}
|
|
69
71
|
})
|
|
72
|
+
|
|
70
73
|
applyEscapeProps(v, props)
|
|
71
74
|
}, [])
|
|
72
75
|
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
/** @jsxImportSource @octane-xplat/jsx/dom */
|
|
1
2
|
import { useImperativeHandle, useEffect, useRef } from 'octane'
|
|
2
3
|
import type { BarcodeScannerHandle, BarcodeScannerProps } from './props'
|
|
3
4
|
import { fromDetectorFormat, toDetectorFormats } from './format-map.web'
|
|
5
|
+
import type { JSX } from 'octane/jsx-runtime'
|
|
4
6
|
|
|
5
7
|
/** Live barcode/QR scanner — getUserMedia + the BarcodeDetector API
|
|
6
8
|
* (Chromium/Safari 18.4+; Firefox lacks the detector). Polling runs on
|
|
7
9
|
* requestVideoFrameCallback where available, else a 250ms interval. */
|
|
8
|
-
export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
10
|
+
export function BarcodeScanner(props: BarcodeScannerProps): JSX.Element @{
|
|
9
11
|
const video = useRef<HTMLVideoElement | null>(null)
|
|
10
12
|
const latest = useRef(props)
|
|
11
13
|
latest.current = props
|
|
@@ -15,6 +17,7 @@ export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
|
15
17
|
toggleCamera: () => {},
|
|
16
18
|
native: v,
|
|
17
19
|
})
|
|
20
|
+
|
|
18
21
|
useImperativeHandle(props.ref, () => handle(video.current), [])
|
|
19
22
|
|
|
20
23
|
useEffect(() => {
|
|
@@ -24,6 +27,7 @@ export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
|
24
27
|
props.onError?.({ message: 'unsupported: BarcodeDetector/getUserMedia is not available' })
|
|
25
28
|
return
|
|
26
29
|
}
|
|
30
|
+
|
|
27
31
|
const el = video.current
|
|
28
32
|
if (!el) {
|
|
29
33
|
return
|
|
@@ -45,6 +49,7 @@ export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
|
45
49
|
if (cancelled || !el || pausedRef.current) {
|
|
46
50
|
return
|
|
47
51
|
}
|
|
52
|
+
|
|
48
53
|
try {
|
|
49
54
|
const codes = await detector.detect(el)
|
|
50
55
|
for (const code of codes ?? []) {
|
|
@@ -58,6 +63,7 @@ export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
|
58
63
|
} catch {
|
|
59
64
|
// A transient frame failure ends detection for this tick only.
|
|
60
65
|
}
|
|
66
|
+
|
|
61
67
|
schedule()
|
|
62
68
|
}
|
|
63
69
|
|
|
@@ -65,6 +71,7 @@ export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
|
65
71
|
if (cancelled) {
|
|
66
72
|
return
|
|
67
73
|
}
|
|
74
|
+
|
|
68
75
|
const rvfc = (el as any).requestVideoFrameCallback
|
|
69
76
|
if (typeof rvfc === 'function') {
|
|
70
77
|
rvfc.call(el, () => poll())
|
|
@@ -79,12 +86,16 @@ export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
|
79
86
|
})
|
|
80
87
|
|
|
81
88
|
mediaDevices
|
|
82
|
-
.getUserMedia({
|
|
89
|
+
.getUserMedia({
|
|
90
|
+
video: { facingMode: props.facing === 'front' ? 'user' : 'environment' },
|
|
91
|
+
audio: false,
|
|
92
|
+
})
|
|
83
93
|
.then((s: MediaStream) => {
|
|
84
94
|
if (cancelled) {
|
|
85
95
|
s.getTracks().forEach((t) => t.stop())
|
|
86
96
|
return
|
|
87
97
|
}
|
|
98
|
+
|
|
88
99
|
stream = s
|
|
89
100
|
el.srcObject = s
|
|
90
101
|
el.play().catch(() => {})
|
|
@@ -99,6 +110,7 @@ export function BarcodeScanner(props: BarcodeScannerProps) @{
|
|
|
99
110
|
if (timer) {
|
|
100
111
|
clearTimeout(timer)
|
|
101
112
|
}
|
|
113
|
+
|
|
102
114
|
stream?.getTracks().forEach((t) => t.stop())
|
|
103
115
|
el.srcObject = null
|
|
104
116
|
}
|
|
@@ -1,12 +1,138 @@
|
|
|
1
|
-
// Barcode scanner — macOS leaf. Vision's
|
|
2
|
-
//
|
|
3
|
-
|
|
1
|
+
// Barcode scanner — macOS leaf. Still images decode through Vision's
|
|
2
|
+
// VNDetectBarcodesRequest; live camera scanning is not available on macOS,
|
|
3
|
+
// so ensure() reports 'unsupported'.
|
|
4
|
+
import {
|
|
5
|
+
CGImageGetHeight,
|
|
6
|
+
CGImageGetWidth,
|
|
7
|
+
NSImage,
|
|
8
|
+
NSURL,
|
|
9
|
+
VNDetectBarcodesRequest,
|
|
10
|
+
VNImageRequestHandler,
|
|
11
|
+
} from '@octane-xplat/platform/macos'
|
|
12
|
+
import type { BarcodeFormat, BarcodeScannerService, PermissionResult, ScannedCode } from './types'
|
|
4
13
|
|
|
14
|
+
// String literals, not the bound constants — the generated metadata targets
|
|
15
|
+
// macOS 11 and omits VNBarcodeSymbologyCodabar.
|
|
16
|
+
const VISION_PREFIX = 'VNBarcodeSymbology'
|
|
17
|
+
const SYMBOLOGIES: Record<BarcodeFormat, readonly string[]> = {
|
|
18
|
+
qr: ['QR'],
|
|
19
|
+
ean13: ['EAN13'],
|
|
20
|
+
ean8: ['EAN8'],
|
|
21
|
+
upc_a: ['EAN13'],
|
|
22
|
+
upc_e: ['UPCE'],
|
|
23
|
+
code128: ['Code128'],
|
|
24
|
+
code39: ['Code39', 'Code39Checksum', 'Code39FullASCII', 'Code39FullASCIIChecksum'],
|
|
25
|
+
code93: ['Code93', 'Code93i'],
|
|
26
|
+
codabar: ['Codabar'],
|
|
27
|
+
itf: ['I2of5', 'I2of5Checksum', 'ITF14'],
|
|
28
|
+
datamatrix: ['DataMatrix'],
|
|
29
|
+
pdf417: ['PDF417'],
|
|
30
|
+
aztec: ['Aztec'],
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Vision → our vocabulary. EAN13 resolves to ean13; UPC-A is an EAN-13 with
|
|
34
|
+
// a leading 0 and is rewritten on the payload (see below).
|
|
35
|
+
const FORMATS: Record<string, string> = {}
|
|
36
|
+
for (const [format, names] of Object.entries(SYMBOLOGIES)) {
|
|
37
|
+
for (const name of names) {FORMATS[VISION_PREFIX + name] ??= format}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The deduplicated, prefixed union for the requested formats — every entry
|
|
41
|
+
* when `formats` is missing or empty, so results stay inside our vocabulary. */
|
|
42
|
+
function symbologiesFor(formats?: BarcodeFormat[]): string[] {
|
|
43
|
+
const wanted = formats?.length ? formats : (Object.keys(SYMBOLOGIES) as BarcodeFormat[])
|
|
44
|
+
const out = new Set<string>()
|
|
45
|
+
for (const format of wanted) {
|
|
46
|
+
for (const name of SYMBOLOGIES[format] ?? []) {out.add(VISION_PREFIX + name)}
|
|
47
|
+
}
|
|
48
|
+
return [...out]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** request.results may be a JS array or an NSArray — count/objectAtIndex. */
|
|
52
|
+
function observations(results: any): any[] {
|
|
53
|
+
if (!results) {return []}
|
|
54
|
+
if (Array.isArray(results)) {return results}
|
|
55
|
+
const out: any[] = []
|
|
56
|
+
for (let i = 0, count = Number(results.count ?? 0); i < count; i++) {
|
|
57
|
+
out.push(results.objectAtIndex(i))
|
|
58
|
+
}
|
|
59
|
+
return out
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
5
63
|
export const barcodeScanner: BarcodeScannerService = {
|
|
6
|
-
supported:
|
|
64
|
+
supported: true,
|
|
7
65
|
ensure: async (): Promise<PermissionResult> => 'unsupported',
|
|
8
|
-
scanImage:
|
|
9
|
-
|
|
66
|
+
scanImage: async (image: unknown, formats?: BarcodeFormat[]): Promise<ScannedCode[]> => {
|
|
67
|
+
let source: any
|
|
68
|
+
if (typeof image === 'string' || (image != null && typeof (image as any).uri === 'string')) {
|
|
69
|
+
const input = typeof image === 'string' ? image : (image as any).uri
|
|
70
|
+
if (/^file:/i.test(input)) {
|
|
71
|
+
source = NSImage.alloc().initWithContentsOfURL(NSURL.URLWithString(input))
|
|
72
|
+
} else if (/^[a-z][a-z0-9+.-]*:/i.test(input)) {
|
|
73
|
+
throw new Error('barcodeScanner.scanImage: macOS decodes local files; download remote images first')
|
|
74
|
+
} else {
|
|
75
|
+
source = NSImage.alloc().initWithContentsOfURL(NSURL.fileURLWithPath(input))
|
|
76
|
+
}
|
|
77
|
+
} else if (image != null && typeof (image as any).CGImageForProposedRectContextHints === 'function') {
|
|
78
|
+
source = image
|
|
79
|
+
} else {
|
|
80
|
+
throw new Error('barcodeScanner.scanImage: expected a file path, file URL, {uri} ref, or NSImage')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const cg = source?.CGImageForProposedRectContextHints?.(null, null, null)
|
|
84
|
+
if (!cg) {
|
|
85
|
+
throw new Error('barcodeScanner.scanImage: could not read the image')
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const request = VNDetectBarcodesRequest.alloc().init()
|
|
89
|
+
request.symbologies = symbologiesFor(formats)
|
|
90
|
+
const handler = VNImageRequestHandler.alloc().initWithCGImageOptions(cg, {})
|
|
91
|
+
if (!handler.performRequestsError([request], null)) {
|
|
92
|
+
throw new Error('barcodeScanner.scanImage: Vision could not process the image')
|
|
93
|
+
}
|
|
10
94
|
|
|
11
|
-
|
|
12
|
-
|
|
95
|
+
// Vision reports UPC-A as EAN-13 with a leading 0.
|
|
96
|
+
const wantsUpcA = !formats?.length || formats.includes('upc_a')
|
|
97
|
+
const wantsEan13 = !formats?.length || formats.includes('ean13')
|
|
98
|
+
const W = Number(CGImageGetWidth(cg))
|
|
99
|
+
const H = Number(CGImageGetHeight(cg))
|
|
100
|
+
const codes: ScannedCode[] = []
|
|
101
|
+
for (const observation of observations(request.results)) {
|
|
102
|
+
const symbology = String(observation?.symbology ?? '')
|
|
103
|
+
let type =
|
|
104
|
+
FORMATS[symbology] ??
|
|
105
|
+
(symbology.startsWith(VISION_PREFIX) ? symbology.slice(VISION_PREFIX.length) : symbology)
|
|
106
|
+
let data = String(observation?.payloadStringValue ?? '')
|
|
107
|
+
if (type === 'ean13') {
|
|
108
|
+
if (data.length === 13 && data.startsWith('0') && wantsUpcA) {
|
|
109
|
+
type = 'upc_a'
|
|
110
|
+
data = data.slice(1)
|
|
111
|
+
} else if (!wantsEan13) {
|
|
112
|
+
continue
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const code: ScannedCode = { type, data }
|
|
117
|
+
const box = observation?.boundingBox
|
|
118
|
+
if (box) {
|
|
119
|
+
const ox = Number(box.origin?.x)
|
|
120
|
+
const oy = Number(box.origin?.y)
|
|
121
|
+
const width = Number(box.size?.width)
|
|
122
|
+
const height = Number(box.size?.height)
|
|
123
|
+
if (Number.isFinite(ox) && Number.isFinite(oy) && Number.isFinite(width) && Number.isFinite(height)) {
|
|
124
|
+
// Vision bounds are normalized with a bottom-left origin.
|
|
125
|
+
code.bounds = {
|
|
126
|
+
x: ox * W,
|
|
127
|
+
y: (1 - oy - height) * H,
|
|
128
|
+
width: width * W,
|
|
129
|
+
height: height * H,
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
codes.push(code)
|
|
135
|
+
}
|
|
136
|
+
return codes
|
|
137
|
+
},
|
|
138
|
+
}
|
package/src/barcode-scanner.ts
CHANGED
|
@@ -19,12 +19,15 @@ async function ensure(): Promise<PermissionResult> {
|
|
|
19
19
|
if (status === 3) {
|
|
20
20
|
return 'granted'
|
|
21
21
|
}
|
|
22
|
+
|
|
22
23
|
if (status === 1 || status === 2) {
|
|
23
24
|
return 'denied'
|
|
24
25
|
}
|
|
26
|
+
|
|
25
27
|
if (typeof av?.requestAccessForMediaTypeCompletionHandler !== 'function') {
|
|
26
28
|
return 'unsupported'
|
|
27
29
|
}
|
|
30
|
+
|
|
28
31
|
return await new Promise<PermissionResult>((resolve) => {
|
|
29
32
|
av.requestAccessForMediaTypeCompletionHandler(video, (granted: boolean) =>
|
|
30
33
|
resolve(granted ? 'granted' : 'denied'),
|
|
@@ -39,9 +42,11 @@ async function ensure(): Promise<PermissionResult> {
|
|
|
39
42
|
) {
|
|
40
43
|
return 'granted'
|
|
41
44
|
}
|
|
45
|
+
|
|
42
46
|
if (!activity) {
|
|
43
47
|
return 'denied'
|
|
44
48
|
}
|
|
49
|
+
|
|
45
50
|
return await new Promise<PermissionResult>((resolve) => {
|
|
46
51
|
const requestCode = scanRequestCode++
|
|
47
52
|
const event = Application.android.activityRequestPermissionsEvent
|
|
@@ -82,6 +87,7 @@ async function toImageSource(image: unknown): Promise<any> {
|
|
|
82
87
|
throw new Error('barcodeScanner.scanImage: expected an ImageSource, file path, or {uri} ref')
|
|
83
88
|
}
|
|
84
89
|
|
|
90
|
+
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
85
91
|
export const barcodeScanner: BarcodeScannerService = {
|
|
86
92
|
supported: true,
|
|
87
93
|
|
|
@@ -17,6 +17,7 @@ async function toImageSource(image: unknown): Promise<any> {
|
|
|
17
17
|
return image
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
20
21
|
export const barcodeScanner: BarcodeScannerService = {
|
|
21
22
|
supported: detector() !== null,
|
|
22
23
|
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// Windows leaf — no ML Kit/BarcodeDetector backend; report unsupported.
|
|
2
2
|
import type { BarcodeScannerService, PermissionResult } from './types'
|
|
3
3
|
|
|
4
|
+
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
4
5
|
export const barcodeScanner: BarcodeScannerService = {
|
|
5
6
|
supported: false,
|
|
6
7
|
ensure: async (): Promise<PermissionResult> => 'unsupported',
|
|
7
|
-
scanImage: () =>
|
|
8
|
+
scanImage: () =>
|
|
9
|
+
Promise.reject(new Error('unsupported: barcode scanning is not available on windows')),
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
|
package/src/index.macos.ts
CHANGED
package/src/native-elements.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { Attributes } from '@nativescript-community/octane/intrinsics'
|
|
1
|
+
import type { Attributes } from '@octane-xplat/jsx/intrinsics'
|
|
3
2
|
|
|
4
|
-
declare module '@
|
|
5
|
-
interface
|
|
6
|
-
xplatbarcodescanner: Attributes<
|
|
3
|
+
declare module '@octane-xplat/jsx/intrinsics' {
|
|
4
|
+
interface NativeElements<TElement> {
|
|
5
|
+
xplatbarcodescanner: Attributes<TElement>
|
|
7
6
|
}
|
|
8
7
|
}
|
package/src/props.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { Octane } from 'octane/jsx-runtime'
|
|
2
2
|
import type { BarcodeFormat, ScannedCode } from './types'
|
|
3
3
|
|
|
4
|
+
/** Imperative handle exposed through the BarcodeScanner `ref`. */
|
|
4
5
|
export interface BarcodeScannerHandle {
|
|
5
6
|
/** Pause/resume preview processing (not the camera feed itself). */
|
|
6
7
|
setPaused(paused: boolean): void
|
|
8
|
+
/** Switch between the front and back cameras while scanning. */
|
|
7
9
|
toggleCamera(): void
|
|
8
10
|
/** The underlying MLKitView on native, the <video> element on web. */
|
|
9
11
|
native: unknown
|
|
@@ -13,7 +15,9 @@ export interface BarcodeScannerHandle {
|
|
|
13
15
|
* Decoding only: photo capture stays in `@octane-xplat/camera`, gallery
|
|
14
16
|
* picking in `@octane-xplat/media`. */
|
|
15
17
|
export interface BarcodeScannerProps {
|
|
18
|
+
/** Class list applied to the preview element. */
|
|
16
19
|
className?: any
|
|
20
|
+
/** Inline style applied to the preview element (string CSS or object). */
|
|
17
21
|
style?: any
|
|
18
22
|
/** Restrict decoded symbologies — all supported formats by default. */
|
|
19
23
|
formats?: BarcodeFormat[]
|
|
@@ -25,10 +29,15 @@ export interface BarcodeScannerProps {
|
|
|
25
29
|
torch?: boolean
|
|
26
30
|
/** 'back' | 'front'. Default 'back'. */
|
|
27
31
|
facing?: 'back' | 'front'
|
|
32
|
+
/** Detector/preview failures (permission is requested separately via the
|
|
33
|
+
* `barcodeScanner` service, not surfaced here). */
|
|
28
34
|
onError?: (e: { message: string }) => void
|
|
35
|
+
/** Imperative handle — see BarcodeScannerHandle. */
|
|
29
36
|
ref?: Octane.Ref<BarcodeScannerHandle>
|
|
30
37
|
/** Platform escape hatches, applied after the shared props. */
|
|
31
38
|
ios?: Record<string, any>
|
|
39
|
+
/** Platform escape hatch — same bag contract as `ios`. */
|
|
32
40
|
android?: Record<string, any>
|
|
41
|
+
/** Platform escape hatch — same bag contract as `ios`. */
|
|
33
42
|
web?: Record<string, any>
|
|
34
43
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** Result of ensure(): 'unsupported' means the platform has no camera/permission path. */
|
|
1
2
|
export type PermissionResult = 'granted' | 'denied' | 'unsupported'
|
|
2
3
|
|
|
3
4
|
/** Code symbologies — expo-camera spellings; each platform maps them onto
|
|
@@ -17,6 +18,7 @@ export type BarcodeFormat =
|
|
|
17
18
|
| 'pdf417'
|
|
18
19
|
| 'aztec'
|
|
19
20
|
|
|
21
|
+
/** One decoded barcode/QR value. */
|
|
20
22
|
export interface ScannedCode {
|
|
21
23
|
/** The symbology, in our BarcodeFormat spelling. */
|
|
22
24
|
type: string
|
|
@@ -29,6 +31,7 @@ export interface ScannedCode {
|
|
|
29
31
|
/** Headless decode + permission service — the scanning view is
|
|
30
32
|
* `<BarcodeScanner>` from the same package. */
|
|
31
33
|
export interface BarcodeScannerService {
|
|
34
|
+
/** Synchronous fast check — a detector backend exists on this platform. */
|
|
32
35
|
readonly supported: boolean
|
|
33
36
|
/** Ensure camera permission for live scanning. */
|
|
34
37
|
ensure(): Promise<PermissionResult>
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BarcodeScannerProps } from './props.js';
|
|
2
|
+
import type { JSX } from 'octane/jsx-runtime';
|
|
2
3
|
/** Live barcode/QR scanner — getUserMedia + the BarcodeDetector API
|
|
3
4
|
* (Chromium/Safari 18.4+; Firefox lacks the detector). Polling runs on
|
|
4
5
|
* requestVideoFrameCallback where available, else a 250ms interval. */
|
|
5
|
-
export declare function BarcodeScanner(props: BarcodeScannerProps):
|
|
6
|
+
export declare function BarcodeScanner(props: BarcodeScannerProps): JSX.Element;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { Attributes } from '@nativescript-community/octane/intrinsics'
|
|
1
|
+
import type { Attributes } from '@octane-xplat/jsx/intrinsics'
|
|
3
2
|
|
|
4
|
-
declare module '@
|
|
5
|
-
interface
|
|
6
|
-
xplatbarcodescanner: Attributes<
|
|
3
|
+
declare module '@octane-xplat/jsx/intrinsics' {
|
|
4
|
+
interface NativeElements<TElement> {
|
|
5
|
+
xplatbarcodescanner: Attributes<TElement>
|
|
7
6
|
}
|
|
8
7
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { Octane } from 'octane/jsx-runtime';
|
|
2
2
|
import type { BarcodeFormat, ScannedCode } from './types.js';
|
|
3
|
+
/** Imperative handle exposed through the BarcodeScanner `ref`. */
|
|
3
4
|
export interface BarcodeScannerHandle {
|
|
4
5
|
/** Pause/resume preview processing (not the camera feed itself). */
|
|
5
6
|
setPaused(paused: boolean): void;
|
|
7
|
+
/** Switch between the front and back cameras while scanning. */
|
|
6
8
|
toggleCamera(): void;
|
|
7
9
|
/** The underlying MLKitView on native, the <video> element on web. */
|
|
8
10
|
native: unknown;
|
|
@@ -11,7 +13,9 @@ export interface BarcodeScannerHandle {
|
|
|
11
13
|
* Decoding only: photo capture stays in `@octane-xplat/camera`, gallery
|
|
12
14
|
* picking in `@octane-xplat/media`. */
|
|
13
15
|
export interface BarcodeScannerProps {
|
|
16
|
+
/** Class list applied to the preview element. */
|
|
14
17
|
className?: any;
|
|
18
|
+
/** Inline style applied to the preview element (string CSS or object). */
|
|
15
19
|
style?: any;
|
|
16
20
|
/** Restrict decoded symbologies — all supported formats by default. */
|
|
17
21
|
formats?: BarcodeFormat[];
|
|
@@ -27,12 +31,17 @@ export interface BarcodeScannerProps {
|
|
|
27
31
|
torch?: boolean;
|
|
28
32
|
/** 'back' | 'front'. Default 'back'. */
|
|
29
33
|
facing?: 'back' | 'front';
|
|
34
|
+
/** Detector/preview failures (permission is requested separately via the
|
|
35
|
+
* `barcodeScanner` service, not surfaced here). */
|
|
30
36
|
onError?: (e: {
|
|
31
37
|
message: string;
|
|
32
38
|
}) => void;
|
|
39
|
+
/** Imperative handle — see BarcodeScannerHandle. */
|
|
33
40
|
ref?: Octane.Ref<BarcodeScannerHandle>;
|
|
34
41
|
/** Platform escape hatches, applied after the shared props. */
|
|
35
42
|
ios?: Record<string, any>;
|
|
43
|
+
/** Platform escape hatch — same bag contract as `ios`. */
|
|
36
44
|
android?: Record<string, any>;
|
|
45
|
+
/** Platform escape hatch — same bag contract as `ios`. */
|
|
37
46
|
web?: Record<string, any>;
|
|
38
47
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
/** Result of ensure(): 'unsupported' means the platform has no camera/permission path. */
|
|
1
2
|
export type PermissionResult = 'granted' | 'denied' | 'unsupported';
|
|
2
3
|
/** Code symbologies — expo-camera spellings; each platform maps them onto
|
|
3
4
|
* its own detector vocabulary (ML Kit BarcodeFormats / BarcodeDetector). */
|
|
4
5
|
export type BarcodeFormat = 'qr' | 'ean13' | 'ean8' | 'upc_a' | 'upc_e' | 'code128' | 'code39' | 'code93' | 'codabar' | 'itf' | 'datamatrix' | 'pdf417' | 'aztec';
|
|
6
|
+
/** One decoded barcode/QR value. */
|
|
5
7
|
export interface ScannedCode {
|
|
6
8
|
/** The symbology, in our BarcodeFormat spelling. */
|
|
7
9
|
type: string;
|
|
@@ -18,6 +20,7 @@ export interface ScannedCode {
|
|
|
18
20
|
/** Headless decode + permission service — the scanning view is
|
|
19
21
|
* `<BarcodeScanner>` from the same package. */
|
|
20
22
|
export interface BarcodeScannerService {
|
|
23
|
+
/** Synchronous fast check — a detector backend exists on this platform. */
|
|
21
24
|
readonly supported: boolean;
|
|
22
25
|
/** Ensure camera permission for live scanning. */
|
|
23
26
|
ensure(): Promise<PermissionResult>;
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Alec Larson
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|