From 248a08b18c005637e91d77f674408fe8f5d2b01b Mon Sep 17 00:00:00 2001 From: Rodrigo Gomes Date: Tue, 12 Dec 2023 07:21:21 -0300 Subject: [PATCH] feat: Support for C++ Frame Processor Plugins by publishing VisionCamera as a prefab (#2251) * update docs * add prefabs support --- docs/docs/guides/FRAME_PROCESSORS.mdx | 30 +++++++++++++++++++++++++++ package/android/build.gradle | 15 ++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/docs/docs/guides/FRAME_PROCESSORS.mdx b/docs/docs/guides/FRAME_PROCESSORS.mdx index a9c0961..046fbea 100644 --- a/docs/docs/guides/FRAME_PROCESSORS.mdx +++ b/docs/docs/guides/FRAME_PROCESSORS.mdx @@ -98,6 +98,36 @@ const frameProcessor = useFrameProcessor((frame) => { It is however recommended to use native **Frame Processor Plugins** for processing, as those are much faster than JavaScript and can sometimes operate with the GPU buffer directly. You can simply pass a `Frame` to a native Frame Processor Plugin directly. +## Advanced Frame Handling in C++ + +**vision-camera** is built using prefabs, which allow you to easily extend the library with your own native code. +you can link it in your `CMakeLists.txt` like this: + +```make +find_package(react-native-vision-camera REQUIRED CONFIG) +# ... +target_link_libraries( + ${PACKAGE_NAME} + # ... other libs + react-native-vision-camera::VisionCamera +) +``` + +then pass a `jsi::Object` to your C++ code. and then use the `FrameHostObject` to access the frame data: + +```cpp + #include "frameprocessor/FrameHostObject.h" + +# in your jsi::Function +jni::global_ref unwrappedFrame = frame->frame; +auto frameWidth = unwrappedFrame->getWidth(); +__android_log_print(ANDROID_LOG_DEBUG, "VisionCamera", + "frame width: %d", frameWidth); +auto frameHeight = unwrappedFrame->getHeight(); +__android_log_print(ANDROID_LOG_DEBUG, "VisionCamera", + "frame height: %d", frameHeight); +``` + ## Interacting with Frame Processors ### Access JS values diff --git a/package/android/build.gradle b/package/android/build.gradle index 4dc216c..44c89cc 100644 --- a/package/android/build.gradle +++ b/package/android/build.gradle @@ -92,6 +92,13 @@ android { buildFeatures { prefab true + prefabPublishing true + } + + prefab { + VisionCamera { + headers "${project.buildDir}/headers/visioncamera/" + } } defaultConfig { @@ -171,6 +178,14 @@ task deleteCmakeCache() { } } +task prepareHeaders(type: Copy) { + from fileTree('./src/main/cpp').filter { it.isFile() } + into "${project.buildDir}/headers/visioncamera/react-native-vision-camera/" + includeEmptyDirs = false +} + +preBuild.dependsOn(prepareHeaders) + tasks.configureEach { task -> // C++ build if (task.name.contains("configureCMakeDebug")) {