Skip to content

Commit aed775c

Browse files
feat: worklet example
1 parent 21045f6 commit aed775c

File tree

6 files changed

+66
-52
lines changed

6 files changed

+66
-52
lines changed

example/babel.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = getConfig(
88
{
99
presets: ['module:@react-native/babel-preset'],
1010
plugins: [
11-
// ['react-native-worklets-core/plugin'],
11+
['react-native-worklets-core/plugin'],
1212
'react-native-reanimated/plugin',
1313
],
1414
},

example/ios/Podfile.lock

-42
Original file line numberDiff line numberDiff line change
@@ -1257,40 +1257,6 @@ PODS:
12571257
- ReactCommon/turbomodule/core
12581258
- Yoga
12591259
- SocketRocket (0.7.0)
1260-
- vision-camera-resize-plugin (3.1.0):
1261-
- DoubleConversion
1262-
- glog
1263-
- hermes-engine
1264-
- RCT-Folly (= 2024.01.01.00)
1265-
- RCTRequired
1266-
- RCTTypeSafety
1267-
- React-Codegen
1268-
- React-Core
1269-
- React-debug
1270-
- React-Fabric
1271-
- React-featureflags
1272-
- React-graphics
1273-
- React-ImageManager
1274-
- React-NativeModulesApple
1275-
- React-RCTFabric
1276-
- React-rendererdebug
1277-
- React-utils
1278-
- ReactCommon/turbomodule/bridging
1279-
- ReactCommon/turbomodule/core
1280-
- VisionCamera
1281-
- Yoga
1282-
- VisionCamera (4.5.2):
1283-
- VisionCamera/Core (= 4.5.2)
1284-
- VisionCamera/FrameProcessors (= 4.5.2)
1285-
- VisionCamera/React (= 4.5.2)
1286-
- VisionCamera/Core (4.5.2)
1287-
- VisionCamera/FrameProcessors (4.5.2):
1288-
- React
1289-
- React-callinvoker
1290-
- react-native-worklets-core
1291-
- VisionCamera/React (4.5.2):
1292-
- React-Core
1293-
- VisionCamera/FrameProcessors
12941260
- Yoga (0.0.0)
12951261

12961262
DEPENDENCIES:
@@ -1354,8 +1320,6 @@ DEPENDENCIES:
13541320
- React-utils (from `../node_modules/react-native/ReactCommon/react/utils`)
13551321
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
13561322
- RNReanimated (from `../node_modules/react-native-reanimated`)
1357-
- vision-camera-resize-plugin (from `../node_modules/vision-camera-resize-plugin`)
1358-
- VisionCamera (from `../node_modules/react-native-vision-camera`)
13591323
- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)
13601324

13611325
SPEC REPOS:
@@ -1481,10 +1445,6 @@ EXTERNAL SOURCES:
14811445
:path: "../node_modules/react-native/ReactCommon"
14821446
RNReanimated:
14831447
:path: "../node_modules/react-native-reanimated"
1484-
vision-camera-resize-plugin:
1485-
:path: "../node_modules/vision-camera-resize-plugin"
1486-
VisionCamera:
1487-
:path: "../node_modules/react-native-vision-camera"
14881448
Yoga:
14891449
:path: "../node_modules/react-native/ReactCommon/yoga"
14901450

@@ -1549,8 +1509,6 @@ SPEC CHECKSUMS:
15491509
ReactCommon: eebffb37a90138c6db6eb8b2d952e7e5c6bc083c
15501510
RNReanimated: f4ff116e33e0afc3d127f70efe928847c7c66355
15511511
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
1552-
vision-camera-resize-plugin: 4306d5df9bce0e603bbe6ab04337f21a606f4ad1
1553-
VisionCamera: 057aff621f7801b7d99a00d157fa39244bbd4fd2
15541512
Yoga: 6259f968a4fdf516d76a4432dead624d71aa0fb1
15551513

15561514
PODFILE CHECKSUM: ded8a41f26047703e900afe99b8a72ca375b02ca

example/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"react": "18.2.0",
1515
"react-native": "0.74.4",
1616
"react-native-image-picker": "^7.1.2",
17-
"react-native-reanimated": "^3.14.0"
17+
"react-native-reanimated": "^3.14.0",
18+
"react-native-worklets-core": "^1.3.3"
1819
},
1920
"devDependencies": {
2021
"@babel/core": "^7.20.0",

example/src/ImageExample.tsx

+43-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type Rect,
1111
} from 'react-native-fast-opencv';
1212
import { launchImageLibrary, type Asset } from 'react-native-image-picker';
13+
import { useRunOnJS, useWorklet, Worklets } from 'react-native-worklets-core';
1314

1415
export function ImageExample() {
1516
const [photo, setPhoto] = useState<Asset | null>(null);
@@ -28,12 +29,17 @@ export function ImageExample() {
2829
const src = OpenCV.base64ToMat(photo.base64);
2930
const dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
3031

31-
OpenCV.invoke('cvtColor', src, dst, ColorConversionCodes.COLOR_BGR2HSV);
32+
await OpenCV.invokeAsync(
33+
'cvtColor',
34+
src,
35+
dst,
36+
ColorConversionCodes.COLOR_BGR2HSV
37+
);
3238

3339
const lowerBound = OpenCV.createObject(ObjectType.Scalar, 40, 40, 40);
3440
const upperBound = OpenCV.createObject(ObjectType.Scalar, 100, 255, 255);
3541

36-
OpenCV.invoke('inRange', dst, lowerBound, upperBound, dst);
42+
await OpenCV.invokeAsync('inRange', dst, lowerBound, upperBound, dst);
3743

3844
// const channels = OpenCV.createObject(ObjectType.MatVector);
3945
// OpenCV.invoke('split', dst, channels);
@@ -65,20 +71,51 @@ export function ImageExample() {
6571

6672
// console.log(rectangles);
6773

68-
69-
const result = OpenCV.toJSValue(dst);
70-
setB64(result.base64);
74+
// const result = OpenCV.toJSValue(dst);
75+
// setB64(result.base64);
7176
// console.log(array.length);
7277
}
7378
};
7479

80+
const test2 = useRunOnJS((data: string) => {
81+
setB64(data);
82+
}, []);
83+
84+
const worklet = useWorklet(
85+
'default',
86+
() => {
87+
'worklet';
88+
if (photo && photo.base64 && photo.width && photo.height) {
89+
const src = OpenCV.base64ToMat(photo.base64);
90+
const dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
91+
92+
OpenCV.invoke('cvtColor', src, dst, ColorConversionCodes.COLOR_BGR2HSV);
93+
94+
const lowerBound = OpenCV.createObject(ObjectType.Scalar, 40, 40, 40);
95+
const upperBound = OpenCV.createObject(
96+
ObjectType.Scalar,
97+
100,
98+
255,
99+
255
100+
);
101+
102+
OpenCV.invoke('inRange', dst, lowerBound, upperBound, dst);
103+
104+
const result = OpenCV.toJSValue(dst);
105+
106+
test2(result.base64);
107+
}
108+
},
109+
[]
110+
);
111+
75112
return (
76113
<SafeAreaView style={{ backgroundColor: 'white', flex: 1 }}>
77114
<Button title="Select photo" onPress={test} />
78115
<Text>
79116
URI: {photo?.uri} {photo?.height} {photo?.width}
80117
</Text>
81-
<Button title="Process" onPress={process} />
118+
<Button title="Process" onPress={() => worklet()} />
82119
{photo?.base64 && photo?.width && photo?.height && (
83120
<Image
84121
source={{ uri: 'data:image/jpg;base64,' + photo.base64 }}

src/NativeFastOpencv.ts

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ export interface Spec extends TurboModule {
77

88
const OpenCVInstaller = TurboModuleRegistry.getEnforcing<Spec>('FastOpencv');
99

10-
console.log('try to install', OpenCVInstaller);
11-
1210
if (global.__loadOpenCV == null) {
1311
if (
1412
OpenCVInstaller == null ||

yarn.lock

+20
Original file line numberDiff line numberDiff line change
@@ -10934,6 +10934,7 @@ __metadata:
1093410934
react-native-builder-bob: ^0.29.0
1093510935
react-native-image-picker: ^7.1.2
1093610936
react-native-reanimated: ^3.14.0
10937+
react-native-worklets-core: ^1.3.3
1093710938
languageName: unknown
1093810939
linkType: soft
1093910940

@@ -11014,6 +11015,18 @@ __metadata:
1101411015
languageName: node
1101511016
linkType: hard
1101611017

11018+
"react-native-worklets-core@npm:^1.3.3":
11019+
version: 1.3.3
11020+
resolution: "react-native-worklets-core@npm:1.3.3"
11021+
dependencies:
11022+
string-hash-64: ^1.0.3
11023+
peerDependencies:
11024+
react: "*"
11025+
react-native: "*"
11026+
checksum: bf2f0eb5852ac1e4f58e20fd5912bed46c0f2bd5a671dadb596b7edf2aa5816874c694f472adaaefdb9ef19e5355c05ceb2877946bfaa0cd081c9cc0c95aa404
11027+
languageName: node
11028+
linkType: hard
11029+
1101711030
"react-native@npm:0.74.4":
1101811031
version: 0.74.4
1101911032
resolution: "react-native@npm:0.74.4"
@@ -12158,6 +12171,13 @@ __metadata:
1215812171
languageName: node
1215912172
linkType: hard
1216012173

12174+
"string-hash-64@npm:^1.0.3":
12175+
version: 1.0.3
12176+
resolution: "string-hash-64@npm:1.0.3"
12177+
checksum: 79de8431b4fa3e85a2429cd52a34f7948221ff167b7a094e05d6bcfd0173474b232e0c9845c96f74b0d7b6b0c8bbe2c3532a4cacb21635293ef0cf3cc8e77f06
12178+
languageName: node
12179+
linkType: hard
12180+
1216112181
"string-length@npm:^4.0.1":
1216212182
version: 4.0.2
1216312183
resolution: "string-length@npm:4.0.2"

0 commit comments

Comments
 (0)