2023-07-20 15:30:04 +02:00
|
|
|
import { DependencyList, useMemo } from 'react';
|
|
|
|
import type { DrawableFrame, Frame, FrameInternal } from '../Frame';
|
|
|
|
import { FrameProcessor } from '../CameraProps';
|
2023-02-13 15:22:45 +01:00
|
|
|
// Install RN Worklets by importing it
|
|
|
|
import 'react-native-worklets/src';
|
2021-05-06 14:11:55 +02:00
|
|
|
|
|
|
|
/**
|
2023-07-20 15:30:04 +02:00
|
|
|
* Returns a memoized Frame Processor function wich you can pass to the `<Camera>`.
|
|
|
|
* (See ["Frame Processors"](https://mrousavy.github.io/react-native-vision-camera/docs/guides/frame-processors))
|
2021-05-06 14:11:55 +02:00
|
|
|
*
|
2021-05-27 11:08:57 +02:00
|
|
|
* Make sure to add the `'worklet'` directive to the top of the Frame Processor function, otherwise it will not get compiled into a worklet.
|
2021-05-06 14:11:55 +02:00
|
|
|
*
|
|
|
|
* @param frameProcessor The Frame Processor
|
|
|
|
* @param dependencies The React dependencies which will be copied into the VisionCamera JS-Runtime.
|
|
|
|
* @returns The memoized Frame Processor.
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* const frameProcessor = useFrameProcessor((frame) => {
|
|
|
|
* 'worklet'
|
|
|
|
* const qrCodes = scanQRCodes(frame)
|
2021-07-30 10:27:45 +02:00
|
|
|
* console.log(`QR Codes: ${qrCodes}`)
|
2021-05-06 14:11:55 +02:00
|
|
|
* }, [])
|
|
|
|
* ```
|
|
|
|
*/
|
2023-07-20 15:30:04 +02:00
|
|
|
export function useFrameProcessor(frameProcessor: (frame: Frame) => void, dependencies: DependencyList): FrameProcessor {
|
|
|
|
return useMemo(
|
|
|
|
() => ({
|
|
|
|
frameProcessor: (frame: Frame) => {
|
|
|
|
'worklet';
|
|
|
|
// Increment ref-count by one
|
|
|
|
(frame as FrameInternal).incrementRefCount();
|
|
|
|
try {
|
|
|
|
// Call sync frame processor
|
|
|
|
frameProcessor(frame);
|
|
|
|
} finally {
|
|
|
|
// Potentially delete Frame if we were the last ref (no runAsync)
|
|
|
|
(frame as FrameInternal).decrementRefCount();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
type: 'frame-processor',
|
|
|
|
}),
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
dependencies,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a memoized Skia Frame Processor function wich you can pass to the `<Camera>`.
|
|
|
|
* The Skia Frame Processor allows you to draw anything onto the Frame using react-native-skia.
|
|
|
|
* (See ["Frame Processors"](https://mrousavy.github.io/react-native-vision-camera/docs/guides/frame-processors))
|
|
|
|
*
|
|
|
|
* Make sure to add the `'worklet'` directive to the top of the Frame Processor function, otherwise it will not get compiled into a worklet.
|
|
|
|
*
|
|
|
|
* @param frameProcessor The Frame Processor
|
|
|
|
* @param dependencies The React dependencies which will be copied into the VisionCamera JS-Runtime.
|
|
|
|
* @returns The memoized Frame Processor.
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* const frameProcessor = useSkiaFrameProcessor((frame) => {
|
|
|
|
* 'worklet'
|
|
|
|
* const qrCodes = scanQRCodes(frame)
|
|
|
|
* frame.drawRect(...)
|
|
|
|
* console.log(`QR Codes: ${qrCodes}`)
|
|
|
|
* }, [])
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export function useSkiaFrameProcessor(frameProcessor: (frame: DrawableFrame) => void, dependencies: DependencyList): FrameProcessor {
|
|
|
|
return useMemo(
|
|
|
|
() => ({
|
|
|
|
frameProcessor: (frame: DrawableFrame) => {
|
|
|
|
'worklet';
|
|
|
|
// Increment ref-count by one
|
|
|
|
(frame as FrameInternal).incrementRefCount();
|
|
|
|
try {
|
|
|
|
// Call sync frame processor
|
|
|
|
frameProcessor(frame);
|
|
|
|
} finally {
|
|
|
|
// Potentially delete Frame if we were the last ref (no runAsync)
|
|
|
|
(frame as FrameInternal).decrementRefCount();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
type: 'skia-frame-processor',
|
|
|
|
}),
|
feat: Sync Frame Processors (plus `runAsync` and `runAtTargetFps`) (#1472)
Before, Frame Processors ran on a separate Thread.
After, Frame Processors run fully synchronous and always at the same FPS as the Camera.
Two new functions have been introduced:
* `runAtTargetFps(fps: number, func: () => void)`: Runs the given code as often as the given `fps`, effectively throttling it's calls.
* `runAsync(frame: Frame, func: () => void)`: Runs the given function on a separate Thread for Frame Processing. A strong reference to the Frame is held as long as the function takes to execute.
You can use `runAtTargetFps` to throttle calls to a specific API (e.g. if your Camera is running at 60 FPS, but you only want to run face detection at ~25 FPS, use `runAtTargetFps(25, ...)`.)
You can use `runAsync` to run a heavy algorithm asynchronous, so that the Camera is not blocked while your algorithm runs. This is useful if your main sync processor draws something, and your async processor is doing some image analysis on the side.
You can also combine both functions.
Examples:
```js
const frameProcessor = useFrameProcessor((frame) => {
'worklet'
console.log("I'm running at 60 FPS!")
}, [])
```
```js
const frameProcessor = useFrameProcessor((frame) => {
'worklet'
console.log("I'm running at 60 FPS!")
runAtTargetFps(10, () => {
'worklet'
console.log("I'm running at 10 FPS!")
})
}, [])
```
```js
const frameProcessor = useFrameProcessor((frame) => {
'worklet'
console.log("I'm running at 60 FPS!")
runAsync(frame, () => {
'worklet'
console.log("I'm running on another Thread, I can block for longer!")
})
}, [])
```
```js
const frameProcessor = useFrameProcessor((frame) => {
'worklet'
console.log("I'm running at 60 FPS!")
runAtTargetFps(10, () => {
'worklet'
runAsync(frame, () => {
'worklet'
console.log("I'm running on another Thread at 10 FPS, I can block for longer!")
})
})
}, [])
```
2023-02-15 16:47:09 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-07-20 15:30:04 +02:00
|
|
|
dependencies,
|
|
|
|
);
|
2021-07-30 10:27:45 +02:00
|
|
|
}
|