@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 CHANGED
@@ -1,66 +1,5 @@
1
1
  # @octane-xplat/barcode-scanner
2
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.
3
+ [Documentation](https://octane-xplat.goddardai.org/package-barcode-scanner).
8
4
 
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.
5
+ [API reference](https://octane-xplat.goddardai.org/api-barcode-scanner).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octane-xplat/barcode-scanner",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
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
5
  "keywords": [
6
6
  "android",
@@ -59,10 +59,10 @@
59
59
  "@nativescript-community/octane": "0.2.5",
60
60
  "@nativescript/core": "9.1.3",
61
61
  "@nativescript/types": "9.1.4",
62
- "@tsrx/typescript-plugin": "0.4.11",
63
62
  "octane": "0.16.3",
64
- "tsrx-typegen": "0.5.0",
65
- "typescript": "5.9.3"
63
+ "tsrx-typegen": "0.6.1",
64
+ "typescript": "7.1.0-beta",
65
+ "@tsrx/content-mapper": "0.1.2"
66
66
  },
67
67
  "peerDependencies": {
68
68
  "@nativescript-community/octane": ">=0.2.1 <1",
@@ -86,7 +86,7 @@
86
86
  "dependencies": {
87
87
  "@nativescript/mlkit-barcode-scanning": "3.0.4",
88
88
  "@nativescript/mlkit-core": "3.0.4",
89
- "@octane-xplat/jsx": "0.19.0",
90
- "@octane-xplat/platform": "0.19.0"
89
+ "@octane-xplat/jsx": "0.20.0",
90
+ "@octane-xplat/platform": "0.20.0"
91
91
  }
92
92
  }
@@ -5,12 +5,11 @@
5
5
  // (decision #140). Renders nothing and reports once via onError.
6
6
  import { useEffect } from 'octane'
7
7
  import type { BarcodeScannerProps } from './props'
8
- import type { JSX } from 'octane/jsx-runtime'
9
8
 
10
- export function BarcodeScanner(props: BarcodeScannerProps): JSX.Element @{
11
- useEffect(() => {
12
- props.onError?.({ message: 'unsupported: live barcode scanning is not wired on macOS' })
13
- }, [])
9
+ export function BarcodeScanner(props: BarcodeScannerProps): unknown @{
10
+ useEffect(() => {
11
+ props.onError?.({ message: 'unsupported: live barcode scanning is not wired on macOS' })
12
+ }, [])
14
13
 
15
- <masonview style={props.style} className={props.className} />
14
+ <masonview style={props.style} className={props.className} />
16
15
  }
@@ -9,97 +9,105 @@ import { fromNativeFormat, toNativeFormats } from './format-map'
9
9
  registerElement('xplatbarcodescanner', MLKitView)
10
10
 
11
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
- }
12
+ const bag = isAndroid ? props.android : props.ios
13
+
14
+ if (bag) {
15
+ Object.assign(view, bag)
16
+ }
16
17
  }
17
18
 
18
19
  /** Live barcode/QR scanner — ML Kit's camera preview with `detectionType:
19
20
  * barcode`. Decoding only; photo capture stays in @octane-xplat/camera. */
20
21
  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} />
22
+ const host = useRef<MLKitView | null>(null)
23
+
24
+ const bindHost = useCallback((v: MLKitView | null) => {
25
+ host.current = v
26
+ }, [])
27
+
28
+ useImperativeHandle(
29
+ props.ref,
30
+ (): BarcodeScannerHandle => ({
31
+ setPaused: (paused) => {
32
+ if (host.current) {
33
+ host.current.pause = paused
34
+ }
35
+ },
36
+ toggleCamera: () => host.current?.toggleCamera(),
37
+ native: host.current?.nativeViewProtected ?? host.current,
38
+ }),
39
+ [],
40
+ )
41
+
42
+ // Mount wiring — listeners before any prop assignment.
43
+ useEffect(() => {
44
+ const v = host.current
45
+
46
+ if (!v) {
47
+ return
48
+ }
49
+
50
+ v.detectionType = DetectionType.Barcode
51
+ v.on(MLKitView.detectionEvent, (e: any) => {
52
+ const data = e?.data ?? e?.data?.data ?? e
53
+ const list = Array.isArray(data) ? data : [data]
54
+
55
+ for (const item of list) {
56
+ if (!item) {
57
+ continue
58
+ }
59
+
60
+ const bounds = item.bounds
61
+
62
+ props.onCodeScanned?.({
63
+ type: fromNativeFormat(item.format),
64
+ data: String(item.rawValue ?? item.displayValue ?? ''),
65
+ bounds: bounds
66
+ ? {
67
+ x: bounds.origin?.x ?? bounds.x ?? 0,
68
+ y: bounds.origin?.y ?? bounds.y ?? 0,
69
+ width: bounds.size?.width ?? bounds.width ?? 0,
70
+ height: bounds.size?.height ?? bounds.height ?? 0,
71
+ }
72
+ : undefined,
73
+ })
74
+ }
75
+ })
76
+
77
+ applyEscapeProps(v, props)
78
+ }, [])
79
+
80
+ useEffect(() => {
81
+ const v = host.current
82
+
83
+ if (v) {
84
+ v.barcodeFormats = toNativeFormats(props.formats)
85
+ }
86
+ }, [JSON.stringify(props.formats)])
87
+
88
+ useEffect(() => {
89
+ const v = host.current
90
+
91
+ if (v) {
92
+ v.cameraPosition = props.facing === 'front' ? CameraPosition.FRONT : CameraPosition.BACK
93
+ }
94
+ }, [props.facing])
95
+
96
+ useEffect(() => {
97
+ const v = host.current
98
+
99
+ if (v) {
100
+ v.pause = props.paused ?? false
101
+ }
102
+ }, [props.paused])
103
+
104
+ useEffect(() => {
105
+ const v = host.current
106
+
107
+ if (v) {
108
+ v.torchOn = props.torch ?? false
109
+ }
110
+ }, [props.torch])
111
+
112
+ <xplatbarcodescanner ref={bindHost} style={props.style} className={props.className} />
105
113
  }
@@ -8,121 +8,128 @@ import type { JSX } from 'octane/jsx-runtime'
8
8
  * (Chromium/Safari 18.4+; Firefox lacks the detector). Polling runs on
9
9
  * requestVideoFrameCallback where available, else a 250ms interval. */
10
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
- />
11
+ const video = useRef<HTMLVideoElement | null>(null)
12
+ const latest = useRef(props)
13
+
14
+ latest.current = props
15
+
16
+ const handle = (v: HTMLVideoElement | null): BarcodeScannerHandle => ({
17
+ setPaused: () => {},
18
+ toggleCamera: () => {},
19
+ native: v,
20
+ })
21
+
22
+ useImperativeHandle(props.ref, () => handle(video.current), [])
23
+
24
+ useEffect(() => {
25
+ const Detector = (globalThis as any).BarcodeDetector
26
+ const mediaDevices = (globalThis as any).navigator?.mediaDevices
27
+
28
+ if (!Detector || typeof mediaDevices?.getUserMedia !== 'function') {
29
+ props.onError?.({ message: 'unsupported: BarcodeDetector/getUserMedia is not available' })
30
+ return
31
+ }
32
+
33
+ const el = video.current
34
+
35
+ if (!el) {
36
+ return
37
+ }
38
+
39
+ let detector: any
40
+
41
+ try {
42
+ detector = new Detector({ formats: toDetectorFormats(props.formats) })
43
+ } catch {
44
+ detector = new Detector()
45
+ }
46
+
47
+ let stream: MediaStream | null = null
48
+ let timer: any = null
49
+ let cancelled = false
50
+ const pausedRef = { current: false }
51
+
52
+ const poll = async () => {
53
+ if (cancelled || !el || pausedRef.current) {
54
+ return
55
+ }
56
+
57
+ try {
58
+ const codes = await detector.detect(el)
59
+
60
+ for (const code of codes ?? []) {
61
+ const box = code.boundingBox
62
+
63
+ latest.current.onCodeScanned?.({
64
+ type: fromDetectorFormat(code.format),
65
+ data: String(code.rawValue ?? ''),
66
+ bounds: box ? { x: box.x, y: box.y, width: box.width, height: box.height } : undefined,
67
+ })
68
+ }
69
+ } catch {
70
+ // A transient frame failure ends detection for this tick only.
71
+ }
72
+
73
+ schedule()
74
+ }
75
+
76
+ const schedule = () => {
77
+ if (cancelled) {
78
+ return
79
+ }
80
+
81
+ const rvfc = (el as any).requestVideoFrameCallback
82
+
83
+ if (typeof rvfc === 'function') {
84
+ rvfc.call(el, () => poll())
85
+ } else {
86
+ timer = setTimeout(poll, 250)
87
+ }
88
+ }
89
+
90
+ // props.paused is read through latest in the poll loop.
91
+ Object.defineProperty(pausedRef, 'current', {
92
+ get: () => latest.current.paused ?? false,
93
+ })
94
+
95
+ mediaDevices
96
+ .getUserMedia({
97
+ video: { facingMode: props.facing === 'front' ? 'user' : 'environment' },
98
+ audio: false,
99
+ })
100
+ .then((s: MediaStream) => {
101
+ if (cancelled) {
102
+ s.getTracks().forEach((t) => t.stop())
103
+ return
104
+ }
105
+
106
+ stream = s
107
+ el.srcObject = s
108
+ el.play().catch(() => {})
109
+ schedule()
110
+ })
111
+ .catch((e: any) => {
112
+ latest.current.onError?.({ message: e?.message ?? 'camera unavailable' })
113
+ })
114
+
115
+ return () => {
116
+ cancelled = true
117
+ if (timer) {
118
+ clearTimeout(timer)
119
+ }
120
+
121
+ stream?.getTracks().forEach((t) => t.stop())
122
+ el.srcObject = null
123
+ }
124
+ }, [props.facing, JSON.stringify(props.formats)])
125
+
126
+ <video
127
+ ref={(el: HTMLVideoElement | null) => {
128
+ video.current = el
129
+ }}
130
+ muted
131
+ playsInline
132
+ style={props.style}
133
+ className={props.className}
134
+ />
128
135
  }