@octane-xplat/barcode-scanner 0.0.1 → 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 +66 -0
- package/package.json +83 -2
- package/platforms/android/AndroidManifest.xml +3 -0
- package/platforms/ios/Info.plist +8 -0
- package/src/BarcodeScanner.macos.tsrx +16 -0
- package/src/BarcodeScanner.tsrx +105 -0
- package/src/BarcodeScanner.web.tsrx +128 -0
- package/src/barcode-scanner.linux.ts +2 -0
- package/src/barcode-scanner.macos.ts +138 -0
- package/src/barcode-scanner.ts +125 -0
- package/src/barcode-scanner.web.ts +57 -0
- package/src/barcode-scanner.windows.ts +13 -0
- package/src/format-map.ts +31 -0
- package/src/format-map.web.ts +30 -0
- package/src/index.macos.ts +4 -0
- package/src/index.ts +4 -0
- package/src/index.web.ts +4 -0
- package/src/native-elements.d.ts +7 -0
- package/src/props.ts +43 -0
- package/src/types.ts +44 -0
- package/types/generated/.tsrx-typegen-manifest.json +21 -0
- package/types/generated/BarcodeScanner.d.ts +4 -0
- package/types/generated/BarcodeScanner.macos.d.ts +3 -0
- package/types/generated/BarcodeScanner.web.d.ts +6 -0
- package/types/generated/barcode-scanner.d.ts +3 -0
- package/types/generated/barcode-scanner.linux.d.ts +1 -0
- package/types/generated/barcode-scanner.macos.d.ts +3 -0
- package/types/generated/barcode-scanner.web.d.ts +3 -0
- package/types/generated/barcode-scanner.windows.d.ts +3 -0
- package/types/generated/format-map.d.ts +4 -0
- package/types/generated/format-map.web.d.ts +3 -0
- package/types/generated/index.d.ts +4 -0
- package/types/generated/index.macos.d.ts +4 -0
- package/types/generated/index.web.d.ts +4 -0
- package/types/generated/native-elements.d.ts +7 -0
- package/types/generated/props.d.ts +47 -0
- package/types/generated/types.d.ts +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
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:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
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
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
For live capture, configure camera usage text in the native app and request
|
|
50
|
+
permission from a user action before mounting the scanner:
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { BarcodeScanner, barcodeScanner } from '@octane-xplat/barcode-scanner'
|
|
54
|
+
|
|
55
|
+
export async function canOpenScanner() {
|
|
56
|
+
return barcodeScanner.supported && (await barcodeScanner.ensure()) === 'granted'
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function QrScanner({ onCode }: { onCode: (data: string) => void }) {
|
|
60
|
+
return <BarcodeScanner formats={['qr']} onCodeScanned={({ data }) => onCode(data)} />
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
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,11 +1,92 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octane-xplat/barcode-scanner",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
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",
|
|
5
17
|
"license": "MIT",
|
|
6
18
|
"repository": {
|
|
7
19
|
"type": "git",
|
|
8
20
|
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
9
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"
|
|
10
91
|
}
|
|
11
92
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3
|
+
<plist version="1.0">
|
|
4
|
+
<dict>
|
|
5
|
+
<key>NSCameraUsageDescription</key>
|
|
6
|
+
<string>Camera access is required to scan codes.</string>
|
|
7
|
+
</dict>
|
|
8
|
+
</plist>
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/** @jsxImportSource @octane-xplat/jsx */
|
|
2
|
+
import { useImperativeHandle, useCallback, useEffect, useRef } from 'octane'
|
|
3
|
+
import { isAndroid } from '@nativescript/core'
|
|
4
|
+
import { registerElement } from '@nativescript-community/octane'
|
|
5
|
+
import { CameraPosition, DetectionType, MLKitView } from '@nativescript/mlkit-core'
|
|
6
|
+
import type { BarcodeScannerHandle, BarcodeScannerProps } from './props'
|
|
7
|
+
import { fromNativeFormat, toNativeFormats } from './format-map'
|
|
8
|
+
|
|
9
|
+
registerElement('xplatbarcodescanner', MLKitView)
|
|
10
|
+
|
|
11
|
+
function applyEscapeProps(view: any, props: BarcodeScannerProps): void {
|
|
12
|
+
const bag = isAndroid ? props.android : props.ios
|
|
13
|
+
if (bag) {
|
|
14
|
+
Object.assign(view, bag)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Live barcode/QR scanner — ML Kit's camera preview with `detectionType:
|
|
19
|
+
* barcode`. Decoding only; photo capture stays in @octane-xplat/camera. */
|
|
20
|
+
export function BarcodeScanner(props: BarcodeScannerProps): unknown @{
|
|
21
|
+
const host = useRef<MLKitView | null>(null)
|
|
22
|
+
|
|
23
|
+
const bindHost = useCallback((v: MLKitView | null) => {
|
|
24
|
+
host.current = v
|
|
25
|
+
}, [])
|
|
26
|
+
|
|
27
|
+
useImperativeHandle(
|
|
28
|
+
props.ref,
|
|
29
|
+
(): BarcodeScannerHandle => ({
|
|
30
|
+
setPaused: (paused) => {
|
|
31
|
+
if (host.current) {
|
|
32
|
+
host.current.pause = paused
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
toggleCamera: () => host.current?.toggleCamera(),
|
|
36
|
+
native: host.current?.nativeViewProtected ?? host.current,
|
|
37
|
+
}),
|
|
38
|
+
[],
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
// Mount wiring — listeners before any prop assignment.
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
const v = host.current
|
|
44
|
+
if (!v) {
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
v.detectionType = DetectionType.Barcode
|
|
49
|
+
v.on(MLKitView.detectionEvent, (e: any) => {
|
|
50
|
+
const data = e?.data ?? e?.data?.data ?? e
|
|
51
|
+
const list = Array.isArray(data) ? data : [data]
|
|
52
|
+
for (const item of list) {
|
|
53
|
+
if (!item) {
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const bounds = item.bounds
|
|
58
|
+
props.onCodeScanned?.({
|
|
59
|
+
type: fromNativeFormat(item.format),
|
|
60
|
+
data: String(item.rawValue ?? item.displayValue ?? ''),
|
|
61
|
+
bounds: bounds
|
|
62
|
+
? {
|
|
63
|
+
x: bounds.origin?.x ?? bounds.x ?? 0,
|
|
64
|
+
y: bounds.origin?.y ?? bounds.y ?? 0,
|
|
65
|
+
width: bounds.size?.width ?? bounds.width ?? 0,
|
|
66
|
+
height: bounds.size?.height ?? bounds.height ?? 0,
|
|
67
|
+
}
|
|
68
|
+
: undefined,
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
applyEscapeProps(v, props)
|
|
74
|
+
}, [])
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
const v = host.current
|
|
78
|
+
if (v) {
|
|
79
|
+
v.barcodeFormats = toNativeFormats(props.formats)
|
|
80
|
+
}
|
|
81
|
+
}, [JSON.stringify(props.formats)])
|
|
82
|
+
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
const v = host.current
|
|
85
|
+
if (v) {
|
|
86
|
+
v.cameraPosition = props.facing === 'front' ? CameraPosition.FRONT : CameraPosition.BACK
|
|
87
|
+
}
|
|
88
|
+
}, [props.facing])
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
const v = host.current
|
|
92
|
+
if (v) {
|
|
93
|
+
v.pause = props.paused ?? false
|
|
94
|
+
}
|
|
95
|
+
}, [props.paused])
|
|
96
|
+
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
const v = host.current
|
|
99
|
+
if (v) {
|
|
100
|
+
v.torchOn = props.torch ?? false
|
|
101
|
+
}
|
|
102
|
+
}, [props.torch])
|
|
103
|
+
|
|
104
|
+
<xplatbarcodescanner ref={bindHost} style={props.style} className={props.className} />
|
|
105
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/** @jsxImportSource @octane-xplat/jsx/dom */
|
|
2
|
+
import { useImperativeHandle, useEffect, useRef } from 'octane'
|
|
3
|
+
import type { BarcodeScannerHandle, BarcodeScannerProps } from './props'
|
|
4
|
+
import { fromDetectorFormat, toDetectorFormats } from './format-map.web'
|
|
5
|
+
import type { JSX } from 'octane/jsx-runtime'
|
|
6
|
+
|
|
7
|
+
/** Live barcode/QR scanner — getUserMedia + the BarcodeDetector API
|
|
8
|
+
* (Chromium/Safari 18.4+; Firefox lacks the detector). Polling runs on
|
|
9
|
+
* requestVideoFrameCallback where available, else a 250ms interval. */
|
|
10
|
+
export function BarcodeScanner(props: BarcodeScannerProps): JSX.Element @{
|
|
11
|
+
const video = useRef<HTMLVideoElement | null>(null)
|
|
12
|
+
const latest = useRef(props)
|
|
13
|
+
latest.current = props
|
|
14
|
+
|
|
15
|
+
const handle = (v: HTMLVideoElement | null): BarcodeScannerHandle => ({
|
|
16
|
+
setPaused: () => {},
|
|
17
|
+
toggleCamera: () => {},
|
|
18
|
+
native: v,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
useImperativeHandle(props.ref, () => handle(video.current), [])
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const Detector = (globalThis as any).BarcodeDetector
|
|
25
|
+
const mediaDevices = (globalThis as any).navigator?.mediaDevices
|
|
26
|
+
if (!Detector || typeof mediaDevices?.getUserMedia !== 'function') {
|
|
27
|
+
props.onError?.({ message: 'unsupported: BarcodeDetector/getUserMedia is not available' })
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const el = video.current
|
|
32
|
+
if (!el) {
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let detector: any
|
|
37
|
+
try {
|
|
38
|
+
detector = new Detector({ formats: toDetectorFormats(props.formats) })
|
|
39
|
+
} catch {
|
|
40
|
+
detector = new Detector()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let stream: MediaStream | null = null
|
|
44
|
+
let timer: any = null
|
|
45
|
+
let cancelled = false
|
|
46
|
+
const pausedRef = { current: false }
|
|
47
|
+
|
|
48
|
+
const poll = async () => {
|
|
49
|
+
if (cancelled || !el || pausedRef.current) {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const codes = await detector.detect(el)
|
|
55
|
+
for (const code of codes ?? []) {
|
|
56
|
+
const box = code.boundingBox
|
|
57
|
+
latest.current.onCodeScanned?.({
|
|
58
|
+
type: fromDetectorFormat(code.format),
|
|
59
|
+
data: String(code.rawValue ?? ''),
|
|
60
|
+
bounds: box ? { x: box.x, y: box.y, width: box.width, height: box.height } : undefined,
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
} catch {
|
|
64
|
+
// A transient frame failure ends detection for this tick only.
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
schedule()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const schedule = () => {
|
|
71
|
+
if (cancelled) {
|
|
72
|
+
return
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const rvfc = (el as any).requestVideoFrameCallback
|
|
76
|
+
if (typeof rvfc === 'function') {
|
|
77
|
+
rvfc.call(el, () => poll())
|
|
78
|
+
} else {
|
|
79
|
+
timer = setTimeout(poll, 250)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// props.paused is read through latest in the poll loop.
|
|
84
|
+
Object.defineProperty(pausedRef, 'current', {
|
|
85
|
+
get: () => latest.current.paused ?? false,
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
mediaDevices
|
|
89
|
+
.getUserMedia({
|
|
90
|
+
video: { facingMode: props.facing === 'front' ? 'user' : 'environment' },
|
|
91
|
+
audio: false,
|
|
92
|
+
})
|
|
93
|
+
.then((s: MediaStream) => {
|
|
94
|
+
if (cancelled) {
|
|
95
|
+
s.getTracks().forEach((t) => t.stop())
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
stream = s
|
|
100
|
+
el.srcObject = s
|
|
101
|
+
el.play().catch(() => {})
|
|
102
|
+
schedule()
|
|
103
|
+
})
|
|
104
|
+
.catch((e: any) => {
|
|
105
|
+
latest.current.onError?.({ message: e?.message ?? 'camera unavailable' })
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
return () => {
|
|
109
|
+
cancelled = true
|
|
110
|
+
if (timer) {
|
|
111
|
+
clearTimeout(timer)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
stream?.getTracks().forEach((t) => t.stop())
|
|
115
|
+
el.srcObject = null
|
|
116
|
+
}
|
|
117
|
+
}, [props.facing, JSON.stringify(props.formats)])
|
|
118
|
+
|
|
119
|
+
<video
|
|
120
|
+
ref={(el: HTMLVideoElement | null) => {
|
|
121
|
+
video.current = el
|
|
122
|
+
}}
|
|
123
|
+
muted
|
|
124
|
+
playsInline
|
|
125
|
+
style={props.style}
|
|
126
|
+
className={props.className}
|
|
127
|
+
/>
|
|
128
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
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'
|
|
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. */
|
|
63
|
+
export const barcodeScanner: BarcodeScannerService = {
|
|
64
|
+
supported: true,
|
|
65
|
+
ensure: async (): Promise<PermissionResult> => 'unsupported',
|
|
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
|
+
}
|
|
94
|
+
|
|
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
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Barcode scanner — native service leaf. Live scanning is the <BarcodeScanner>
|
|
2
|
+
// component (mlkit-core's MLKitView); this file owns the camera permission
|
|
3
|
+
// ensure (platform primitives, media's pattern) and headless still decode.
|
|
4
|
+
import { Application, ImageSource } from '@nativescript/core'
|
|
5
|
+
import { DetectionType, detectWithStillImage } from '@nativescript/mlkit-core'
|
|
6
|
+
import { android } from '@octane-xplat/platform/android'
|
|
7
|
+
import type { BarcodeFormat, BarcodeScannerService, PermissionResult, ScannedCode } from './types'
|
|
8
|
+
import { fromNativeFormat, toNativeFormats } from './format-map'
|
|
9
|
+
|
|
10
|
+
let scanRequestCode = 7723
|
|
11
|
+
|
|
12
|
+
async function ensure(): Promise<PermissionResult> {
|
|
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
|
+
if (status === 3) {
|
|
20
|
+
return 'granted'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (status === 1 || status === 2) {
|
|
24
|
+
return 'denied'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (typeof av?.requestAccessForMediaTypeCompletionHandler !== 'function') {
|
|
28
|
+
return 'unsupported'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return await new Promise<PermissionResult>((resolve) => {
|
|
32
|
+
av.requestAccessForMediaTypeCompletionHandler(video, (granted: boolean) =>
|
|
33
|
+
resolve(granted ? 'granted' : 'denied'),
|
|
34
|
+
)
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const perm = 'android.permission.CAMERA'
|
|
39
|
+
const activity = Application.android.foregroundActivity ?? Application.android.startActivity
|
|
40
|
+
if (
|
|
41
|
+
activity?.checkSelfPermission?.(perm) === android.content.pm.PackageManager.PERMISSION_GRANTED
|
|
42
|
+
) {
|
|
43
|
+
return 'granted'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!activity) {
|
|
47
|
+
return 'denied'
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return await new Promise<PermissionResult>((resolve) => {
|
|
51
|
+
const requestCode = scanRequestCode++
|
|
52
|
+
const event = Application.android.activityRequestPermissionsEvent
|
|
53
|
+
const onResult = (args: any) => {
|
|
54
|
+
if (args.requestCode !== requestCode) {
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
Application.android.off(event, onResult)
|
|
58
|
+
resolve(
|
|
59
|
+
args.grantResults?.[0] === android.content.pm.PackageManager.PERMISSION_GRANTED
|
|
60
|
+
? 'granted'
|
|
61
|
+
: 'denied',
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
Application.android.on(event, onResult)
|
|
65
|
+
try {
|
|
66
|
+
activity.requestPermissions([perm], requestCode)
|
|
67
|
+
} catch {
|
|
68
|
+
Application.android.off(event, onResult)
|
|
69
|
+
resolve('denied')
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
} catch {
|
|
73
|
+
return 'denied'
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function toImageSource(image: unknown): Promise<any> {
|
|
78
|
+
if (image instanceof ImageSource) {
|
|
79
|
+
return image
|
|
80
|
+
}
|
|
81
|
+
if (typeof image === 'string') {
|
|
82
|
+
return ImageSource.fromFile(image)
|
|
83
|
+
}
|
|
84
|
+
if (image && typeof (image as { uri?: string }).uri === 'string') {
|
|
85
|
+
return ImageSource.fromFile((image as { uri: string }).uri)
|
|
86
|
+
}
|
|
87
|
+
throw new Error('barcodeScanner.scanImage: expected an ImageSource, file path, or {uri} ref')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
91
|
+
export const barcodeScanner: BarcodeScannerService = {
|
|
92
|
+
supported: true,
|
|
93
|
+
|
|
94
|
+
ensure,
|
|
95
|
+
|
|
96
|
+
async scanImage(image: unknown, formats?: BarcodeFormat[]): Promise<ScannedCode[]> {
|
|
97
|
+
const source = await toImageSource(image)
|
|
98
|
+
const result = await detectWithStillImage(source, {
|
|
99
|
+
detectorType: DetectionType.Barcode,
|
|
100
|
+
barcodeScanning: { barcodeFormat: toNativeFormats(formats) as [any] },
|
|
101
|
+
})
|
|
102
|
+
const list = Array.isArray(result) ? result : Object.values(result ?? {})
|
|
103
|
+
return list
|
|
104
|
+
.flat()
|
|
105
|
+
.filter((item: any) => item && (item.rawValue !== undefined || item.format !== undefined))
|
|
106
|
+
.map((item: any) => ({
|
|
107
|
+
type: fromNativeFormat(item.format),
|
|
108
|
+
data: String(item.rawValue ?? item.displayValue ?? ''),
|
|
109
|
+
bounds: item.bounds
|
|
110
|
+
? {
|
|
111
|
+
x: item.bounds.origin?.x ?? item.bounds.x ?? 0,
|
|
112
|
+
y: item.bounds.origin?.y ?? item.bounds.y ?? 0,
|
|
113
|
+
width: item.bounds.size?.width ?? item.bounds.width ?? 0,
|
|
114
|
+
height: item.bounds.size?.height ?? item.bounds.height ?? 0,
|
|
115
|
+
}
|
|
116
|
+
: undefined,
|
|
117
|
+
}))
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Scanning needs camera access — register the kind this leaf owns for the
|
|
122
|
+
// platform dispatcher (same 'camera' kind media registers; both leafs' owners
|
|
123
|
+
// run identical ensure logic, and the leaf must work with media absent).
|
|
124
|
+
const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
|
|
125
|
+
permissionOwners.camera = () => barcodeScanner.ensure()
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Barcode scanner — web service leaf over the BarcodeDetector API. Live
|
|
2
|
+
// scanning is <BarcodeScanner> (getUserMedia + detector poll); this owns
|
|
3
|
+
// still-image decode and the availability flag.
|
|
4
|
+
import type { BarcodeFormat, BarcodeScannerService, PermissionResult, ScannedCode } from './types'
|
|
5
|
+
import { fromDetectorFormat, toDetectorFormats } from './format-map.web'
|
|
6
|
+
|
|
7
|
+
const detector = () => {
|
|
8
|
+
const Detector = (globalThis as any).BarcodeDetector
|
|
9
|
+
return typeof Detector === 'function' ? Detector : null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function toImageSource(image: unknown): Promise<any> {
|
|
13
|
+
if (typeof image === 'string') {
|
|
14
|
+
const response = await fetch(image)
|
|
15
|
+
return createImageBitmap(await response.blob())
|
|
16
|
+
}
|
|
17
|
+
return image
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
21
|
+
export const barcodeScanner: BarcodeScannerService = {
|
|
22
|
+
supported: detector() !== null,
|
|
23
|
+
|
|
24
|
+
ensure(): Promise<PermissionResult> {
|
|
25
|
+
return Promise.resolve(detector() ? 'granted' : 'unsupported')
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
async scanImage(image: unknown, formats?: BarcodeFormat[]): Promise<ScannedCode[]> {
|
|
29
|
+
const Detector = detector()
|
|
30
|
+
if (!Detector) {
|
|
31
|
+
throw new Error('unsupported: BarcodeDetector is not available')
|
|
32
|
+
}
|
|
33
|
+
let d: any
|
|
34
|
+
try {
|
|
35
|
+
d = new Detector({ formats: toDetectorFormats(formats) })
|
|
36
|
+
} catch {
|
|
37
|
+
d = new Detector()
|
|
38
|
+
}
|
|
39
|
+
const source = await toImageSource(image)
|
|
40
|
+
const codes = await d.detect(source)
|
|
41
|
+
return (codes ?? []).map((code: any) => ({
|
|
42
|
+
type: fromDetectorFormat(code.format),
|
|
43
|
+
data: String(code.rawValue ?? ''),
|
|
44
|
+
bounds: code.boundingBox
|
|
45
|
+
? {
|
|
46
|
+
x: code.boundingBox.x,
|
|
47
|
+
y: code.boundingBox.y,
|
|
48
|
+
width: code.boundingBox.width,
|
|
49
|
+
height: code.boundingBox.height,
|
|
50
|
+
}
|
|
51
|
+
: undefined,
|
|
52
|
+
}))
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
|
|
57
|
+
permissionOwners.camera = () => barcodeScanner.ensure()
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Windows leaf — no ML Kit/BarcodeDetector backend; report unsupported.
|
|
2
|
+
import type { BarcodeScannerService, PermissionResult } from './types'
|
|
3
|
+
|
|
4
|
+
/** Headless barcode service — permission + still-image decode. See BarcodeScannerService. */
|
|
5
|
+
export const barcodeScanner: BarcodeScannerService = {
|
|
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
|
+
}
|
|
11
|
+
|
|
12
|
+
const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
|
|
13
|
+
permissionOwners.camera = () => barcodeScanner.ensure()
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Shared BarcodeFormat ↔ native-vocabulary mapping.
|
|
2
|
+
import { BarcodeFormats } from '@nativescript/mlkit-core'
|
|
3
|
+
import type { BarcodeFormat } from './types'
|
|
4
|
+
|
|
5
|
+
const TO_MLKIT: Record<BarcodeFormat, BarcodeFormats> = {
|
|
6
|
+
qr: BarcodeFormats.QR_CODE,
|
|
7
|
+
ean13: BarcodeFormats.EAN_13,
|
|
8
|
+
ean8: BarcodeFormats.EAN_8,
|
|
9
|
+
upc_a: BarcodeFormats.UPC_A,
|
|
10
|
+
upc_e: BarcodeFormats.UPC_E,
|
|
11
|
+
code128: BarcodeFormats.CODE_128,
|
|
12
|
+
code39: BarcodeFormats.CODE_39,
|
|
13
|
+
code93: BarcodeFormats.CODE_93,
|
|
14
|
+
codabar: BarcodeFormats.CODABAR,
|
|
15
|
+
itf: BarcodeFormats.ITF,
|
|
16
|
+
datamatrix: BarcodeFormats.DATA_MATRIX,
|
|
17
|
+
pdf417: BarcodeFormats.PDF417,
|
|
18
|
+
aztec: BarcodeFormats.AZTEC,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const FROM_MLKIT: Record<string, string> = Object.fromEntries(
|
|
22
|
+
Object.entries(TO_MLKIT).map(([ours, theirs]) => [String(theirs), ours]),
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
export function toNativeFormats(formats?: BarcodeFormat[]): BarcodeFormats[] {
|
|
26
|
+
return formats?.length ? formats.map((f) => TO_MLKIT[f]) : [BarcodeFormats.ALL]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function fromNativeFormat(format: unknown): string {
|
|
30
|
+
return FROM_MLKIT[String(format)] ?? String(format ?? 'unknown')
|
|
31
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Shared BarcodeFormat ↔ web BarcodeDetector-vocabulary mapping.
|
|
2
|
+
import type { BarcodeFormat } from './types'
|
|
3
|
+
|
|
4
|
+
const TO_DETECTOR: Record<BarcodeFormat, string> = {
|
|
5
|
+
qr: 'qr_code',
|
|
6
|
+
ean13: 'ean_13',
|
|
7
|
+
ean8: 'ean_8',
|
|
8
|
+
upc_a: 'upc_a',
|
|
9
|
+
upc_e: 'upc_e',
|
|
10
|
+
code128: 'code_128',
|
|
11
|
+
code39: 'code_39',
|
|
12
|
+
code93: 'code_93',
|
|
13
|
+
codabar: 'codabar',
|
|
14
|
+
itf: 'itf',
|
|
15
|
+
datamatrix: 'data_matrix',
|
|
16
|
+
pdf417: 'pdf417',
|
|
17
|
+
aztec: 'aztec',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const FROM_DETECTOR: Record<string, string> = Object.fromEntries(
|
|
21
|
+
Object.entries(TO_DETECTOR).map(([ours, theirs]) => [theirs, ours]),
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
export function toDetectorFormats(formats?: BarcodeFormat[]): string[] | undefined {
|
|
25
|
+
return formats?.length ? formats.map((f) => TO_DETECTOR[f]) : undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function fromDetectorFormat(format: unknown): string {
|
|
29
|
+
return FROM_DETECTOR[String(format)] ?? String(format ?? 'unknown')
|
|
30
|
+
}
|
package/src/index.ts
ADDED
package/src/index.web.ts
ADDED
package/src/props.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Octane } from 'octane/jsx-runtime'
|
|
2
|
+
import type { BarcodeFormat, ScannedCode } from './types'
|
|
3
|
+
|
|
4
|
+
/** Imperative handle exposed through the BarcodeScanner `ref`. */
|
|
5
|
+
export interface BarcodeScannerHandle {
|
|
6
|
+
/** Pause/resume preview processing (not the camera feed itself). */
|
|
7
|
+
setPaused(paused: boolean): void
|
|
8
|
+
/** Switch between the front and back cameras while scanning. */
|
|
9
|
+
toggleCamera(): void
|
|
10
|
+
/** The underlying MLKitView on native, the <video> element on web. */
|
|
11
|
+
native: unknown
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Live barcode/QR scanning view — ML Kit on native, BarcodeDetector on web.
|
|
15
|
+
* Decoding only: photo capture stays in `@octane-xplat/camera`, gallery
|
|
16
|
+
* picking in `@octane-xplat/media`. */
|
|
17
|
+
export interface BarcodeScannerProps {
|
|
18
|
+
/** Class list applied to the preview element. */
|
|
19
|
+
className?: any
|
|
20
|
+
/** Inline style applied to the preview element (string CSS or object). */
|
|
21
|
+
style?: any
|
|
22
|
+
/** Restrict decoded symbologies — all supported formats by default. */
|
|
23
|
+
formats?: BarcodeFormat[]
|
|
24
|
+
/** Fires for each detected code (the OS may deliver batches). */
|
|
25
|
+
onCodeScanned?: (e: { type: string; data: string; bounds?: ScannedCode['bounds'] }) => void
|
|
26
|
+
/** Freeze detection; preview keeps running. Default false. */
|
|
27
|
+
paused?: boolean
|
|
28
|
+
/** Illuminate the camera torch while scanning. Default false. */
|
|
29
|
+
torch?: boolean
|
|
30
|
+
/** 'back' | 'front'. Default 'back'. */
|
|
31
|
+
facing?: 'back' | 'front'
|
|
32
|
+
/** Detector/preview failures (permission is requested separately via the
|
|
33
|
+
* `barcodeScanner` service, not surfaced here). */
|
|
34
|
+
onError?: (e: { message: string }) => void
|
|
35
|
+
/** Imperative handle — see BarcodeScannerHandle. */
|
|
36
|
+
ref?: Octane.Ref<BarcodeScannerHandle>
|
|
37
|
+
/** Platform escape hatches, applied after the shared props. */
|
|
38
|
+
ios?: Record<string, any>
|
|
39
|
+
/** Platform escape hatch — same bag contract as `ios`. */
|
|
40
|
+
android?: Record<string, any>
|
|
41
|
+
/** Platform escape hatch — same bag contract as `ios`. */
|
|
42
|
+
web?: Record<string, any>
|
|
43
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** Result of ensure(): 'unsupported' means the platform has no camera/permission path. */
|
|
2
|
+
export type PermissionResult = 'granted' | 'denied' | 'unsupported'
|
|
3
|
+
|
|
4
|
+
/** Code symbologies — expo-camera spellings; each platform maps them onto
|
|
5
|
+
* its own detector vocabulary (ML Kit BarcodeFormats / BarcodeDetector). */
|
|
6
|
+
export type BarcodeFormat =
|
|
7
|
+
| 'qr'
|
|
8
|
+
| 'ean13'
|
|
9
|
+
| 'ean8'
|
|
10
|
+
| 'upc_a'
|
|
11
|
+
| 'upc_e'
|
|
12
|
+
| 'code128'
|
|
13
|
+
| 'code39'
|
|
14
|
+
| 'code93'
|
|
15
|
+
| 'codabar'
|
|
16
|
+
| 'itf'
|
|
17
|
+
| 'datamatrix'
|
|
18
|
+
| 'pdf417'
|
|
19
|
+
| 'aztec'
|
|
20
|
+
|
|
21
|
+
/** One decoded barcode/QR value. */
|
|
22
|
+
export interface ScannedCode {
|
|
23
|
+
/** The symbology, in our BarcodeFormat spelling. */
|
|
24
|
+
type: string
|
|
25
|
+
/** Decoded payload. */
|
|
26
|
+
data: string
|
|
27
|
+
/** Corner points / bounding rect where the detector reports it. */
|
|
28
|
+
bounds?: { x: number; y: number; width: number; height: number }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Headless decode + permission service — the scanning view is
|
|
32
|
+
* `<BarcodeScanner>` from the same package. */
|
|
33
|
+
export interface BarcodeScannerService {
|
|
34
|
+
/** Synchronous fast check — a detector backend exists on this platform. */
|
|
35
|
+
readonly supported: boolean
|
|
36
|
+
/** Ensure camera permission for live scanning. */
|
|
37
|
+
ensure(): Promise<PermissionResult>
|
|
38
|
+
/** Decode codes from a still image (platform image ref: a path or
|
|
39
|
+
* ImageSource on native, Blob/HTMLImageElement/data URL on web).
|
|
40
|
+
* Returns [] when the detector finds nothing. */
|
|
41
|
+
scanImage(image: unknown, formats?: BarcodeFormat[]): Promise<ScannedCode[]>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export declare const barcodeScanner: BarcodeScannerService
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"files": [
|
|
4
|
+
"BarcodeScanner.d.ts",
|
|
5
|
+
"BarcodeScanner.macos.d.ts",
|
|
6
|
+
"BarcodeScanner.web.d.ts",
|
|
7
|
+
"barcode-scanner.d.ts",
|
|
8
|
+
"barcode-scanner.linux.d.ts",
|
|
9
|
+
"barcode-scanner.macos.d.ts",
|
|
10
|
+
"barcode-scanner.web.d.ts",
|
|
11
|
+
"barcode-scanner.windows.d.ts",
|
|
12
|
+
"format-map.d.ts",
|
|
13
|
+
"format-map.web.d.ts",
|
|
14
|
+
"index.d.ts",
|
|
15
|
+
"index.macos.d.ts",
|
|
16
|
+
"index.web.d.ts",
|
|
17
|
+
"native-elements.d.ts",
|
|
18
|
+
"props.d.ts",
|
|
19
|
+
"types.d.ts"
|
|
20
|
+
]
|
|
21
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { BarcodeScannerProps } from './props.js';
|
|
2
|
+
/** Live barcode/QR scanner — ML Kit's camera preview with `detectionType:
|
|
3
|
+
* barcode`. Decoding only; photo capture stays in @octane-xplat/camera. */
|
|
4
|
+
export declare function BarcodeScanner(props: BarcodeScannerProps): unknown;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { BarcodeScannerProps } from './props.js';
|
|
2
|
+
import type { JSX } from 'octane/jsx-runtime';
|
|
3
|
+
/** Live barcode/QR scanner — getUserMedia + the BarcodeDetector API
|
|
4
|
+
* (Chromium/Safari 18.4+; Firefox lacks the detector). Polling runs on
|
|
5
|
+
* requestVideoFrameCallback where available, else a 250ms interval. */
|
|
6
|
+
export declare function BarcodeScanner(props: BarcodeScannerProps): JSX.Element;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { barcodeScanner } from './barcode-scanner.web.js';
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Octane } from 'octane/jsx-runtime';
|
|
2
|
+
import type { BarcodeFormat, ScannedCode } from './types.js';
|
|
3
|
+
/** Imperative handle exposed through the BarcodeScanner `ref`. */
|
|
4
|
+
export interface BarcodeScannerHandle {
|
|
5
|
+
/** Pause/resume preview processing (not the camera feed itself). */
|
|
6
|
+
setPaused(paused: boolean): void;
|
|
7
|
+
/** Switch between the front and back cameras while scanning. */
|
|
8
|
+
toggleCamera(): void;
|
|
9
|
+
/** The underlying MLKitView on native, the <video> element on web. */
|
|
10
|
+
native: unknown;
|
|
11
|
+
}
|
|
12
|
+
/** Live barcode/QR scanning view — ML Kit on native, BarcodeDetector on web.
|
|
13
|
+
* Decoding only: photo capture stays in `@octane-xplat/camera`, gallery
|
|
14
|
+
* picking in `@octane-xplat/media`. */
|
|
15
|
+
export interface BarcodeScannerProps {
|
|
16
|
+
/** Class list applied to the preview element. */
|
|
17
|
+
className?: any;
|
|
18
|
+
/** Inline style applied to the preview element (string CSS or object). */
|
|
19
|
+
style?: any;
|
|
20
|
+
/** Restrict decoded symbologies — all supported formats by default. */
|
|
21
|
+
formats?: BarcodeFormat[];
|
|
22
|
+
/** Fires for each detected code (the OS may deliver batches). */
|
|
23
|
+
onCodeScanned?: (e: {
|
|
24
|
+
type: string;
|
|
25
|
+
data: string;
|
|
26
|
+
bounds?: ScannedCode['bounds'];
|
|
27
|
+
}) => void;
|
|
28
|
+
/** Freeze detection; preview keeps running. Default false. */
|
|
29
|
+
paused?: boolean;
|
|
30
|
+
/** Illuminate the camera torch while scanning. Default false. */
|
|
31
|
+
torch?: boolean;
|
|
32
|
+
/** 'back' | 'front'. Default 'back'. */
|
|
33
|
+
facing?: 'back' | 'front';
|
|
34
|
+
/** Detector/preview failures (permission is requested separately via the
|
|
35
|
+
* `barcodeScanner` service, not surfaced here). */
|
|
36
|
+
onError?: (e: {
|
|
37
|
+
message: string;
|
|
38
|
+
}) => void;
|
|
39
|
+
/** Imperative handle — see BarcodeScannerHandle. */
|
|
40
|
+
ref?: Octane.Ref<BarcodeScannerHandle>;
|
|
41
|
+
/** Platform escape hatches, applied after the shared props. */
|
|
42
|
+
ios?: Record<string, any>;
|
|
43
|
+
/** Platform escape hatch — same bag contract as `ios`. */
|
|
44
|
+
android?: Record<string, any>;
|
|
45
|
+
/** Platform escape hatch — same bag contract as `ios`. */
|
|
46
|
+
web?: Record<string, any>;
|
|
47
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Result of ensure(): 'unsupported' means the platform has no camera/permission path. */
|
|
2
|
+
export type PermissionResult = 'granted' | 'denied' | 'unsupported';
|
|
3
|
+
/** Code symbologies — expo-camera spellings; each platform maps them onto
|
|
4
|
+
* its own detector vocabulary (ML Kit BarcodeFormats / BarcodeDetector). */
|
|
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. */
|
|
7
|
+
export interface ScannedCode {
|
|
8
|
+
/** The symbology, in our BarcodeFormat spelling. */
|
|
9
|
+
type: string;
|
|
10
|
+
/** Decoded payload. */
|
|
11
|
+
data: string;
|
|
12
|
+
/** Corner points / bounding rect where the detector reports it. */
|
|
13
|
+
bounds?: {
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/** Headless decode + permission service — the scanning view is
|
|
21
|
+
* `<BarcodeScanner>` from the same package. */
|
|
22
|
+
export interface BarcodeScannerService {
|
|
23
|
+
/** Synchronous fast check — a detector backend exists on this platform. */
|
|
24
|
+
readonly supported: boolean;
|
|
25
|
+
/** Ensure camera permission for live scanning. */
|
|
26
|
+
ensure(): Promise<PermissionResult>;
|
|
27
|
+
/** Decode codes from a still image (platform image ref: a path or
|
|
28
|
+
* ImageSource on native, Blob/HTMLImageElement/data URL on web).
|
|
29
|
+
* Returns [] when the detector finds nothing. */
|
|
30
|
+
scanImage(image: unknown, formats?: BarcodeFormat[]): Promise<ScannedCode[]>;
|
|
31
|
+
}
|
|
32
|
+
export declare const barcodeScanner: BarcodeScannerService;
|