feat: Code Scanner API (#1912)
* feat: CodeScanner JS API * feat: iOS * Use guard * Format * feat: Android base * fix: Attach Surfaces * Use isBusy var * fix: Use separate Queue * feat: Finish iOS types * feat: Implement all other code types on Android * fix: Call JS event * fix: Pass codetypes on Android * fix: iOS use Preview coordinate system * docs: Add comments * chore: Format code * Update CameraView+AVCaptureSession.swift * docs: Add Code Scanner docs * docs: Update * feat: Use lazily downloaded model on Android * Revert changes in CameraPage * Format * fix: Fix empty QR codes * Update README.md
This commit is contained in:
23
package/src/hooks/useCodeScanner.ts
Normal file
23
package/src/hooks/useCodeScanner.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { useCallback, useMemo, useRef } from 'react'
|
||||
import { Code, CodeScanner } from '../CodeScanner'
|
||||
|
||||
export function useCodeScanner(codeScanner: CodeScanner): CodeScanner {
|
||||
const { onCodeScanned, ...codeScannerOptions } = codeScanner
|
||||
|
||||
// Memoize the function once and use a ref on any identity changes
|
||||
const ref = useRef(onCodeScanned)
|
||||
ref.current = onCodeScanned
|
||||
const callback = useCallback((codes: Code[]) => {
|
||||
ref.current(codes)
|
||||
}, [])
|
||||
|
||||
// CodeScanner needs to be memoized so it doesn't trigger a Camera Session re-build
|
||||
return useMemo(
|
||||
() => ({
|
||||
...codeScannerOptions,
|
||||
onCodeScanned: callback,
|
||||
}),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[JSON.stringify(codeScannerOptions), callback],
|
||||
)
|
||||
}
|
@@ -42,8 +42,8 @@ export function createFrameProcessor(frameProcessor: FrameProcessor['frameProces
|
||||
* ```ts
|
||||
* const frameProcessor = useFrameProcessor((frame) => {
|
||||
* 'worklet'
|
||||
* const qrCodes = scanQRCodes(frame)
|
||||
* console.log(`QR Codes: ${qrCodes}`)
|
||||
* const faces = scanFaces(frame)
|
||||
* console.log(`Faces: ${faces}`)
|
||||
* }, [])
|
||||
* ```
|
||||
*/
|
||||
|
Reference in New Issue
Block a user