in VisionCamera v1 & v2 there were two ObjC macros that were helping in creation/registration of Frame Processors, but these were removed with v3 This PR reintroduces such macros, which will not only make FP development easier, but also it will also fix issues people had with registration of Swift Frame Processors (+load vs +initialize issues) Docs were also updated to reflect that the macros should be used to correctly initialize and register ObjC/Swift Frame Processors
202 lines
7.0 KiB
Plaintext
202 lines
7.0 KiB
Plaintext
---
|
|
id: frame-processors-plugins-android
|
|
title: Creating Frame Processor Plugins
|
|
sidebar_label: Creating Frame Processor Plugins (Android)
|
|
---
|
|
|
|
import Tabs from '@theme/Tabs'
|
|
import TabItem from '@theme/TabItem'
|
|
|
|
## Creating a Frame Processor Plugin for Android
|
|
|
|
The Frame Processor Plugin API is built to be as extensible as possible, which allows you to create custom Frame Processor Plugins.
|
|
In this guide we will create a custom Face Detector Plugin which can be used from JS.
|
|
|
|
Android Frame Processor Plugins can be written in either **Java**, **Kotlin** or **C++ (JNI)**.
|
|
|
|
### Mostly automatic setup
|
|
|
|
1. Run [Vision Camera Plugin Builder CLI](https://github.com/mateusz1913/vision-camera-plugin-builder)
|
|
|
|
```sh
|
|
npx vision-camera-plugin-builder android
|
|
```
|
|
|
|
:::info
|
|
The CLI will ask you for the path to project's Android Manifest file, name of the plugin (e.g. `FaceDetectorFrameProcessorPlugin`), name of the exposed method (e.g. `detectFaces`) and language you want to use for plugin development (Java or Kotlin).
|
|
For reference see the [CLI's docs](https://github.com/mateusz1913/vision-camera-plugin-builder#%EF%B8%8F-options).
|
|
:::
|
|
|
|
2. Register the package in MainApplication.java
|
|
|
|
```java
|
|
@Override
|
|
protected List<ReactPackage> getPackages() {
|
|
@SuppressWarnings("UnnecessaryLocalVariable")
|
|
List<ReactPackage> packages = new PackageList(this).getPackages();
|
|
// ...
|
|
// highlight-next-line
|
|
packages.add(new FaceDetectorFrameProcessorPluginPackage()); // <- add
|
|
return packages;
|
|
}
|
|
```
|
|
|
|
### Manual setup
|
|
|
|
<Tabs
|
|
defaultValue="java"
|
|
values={[
|
|
{label: 'Java', value: 'java'},
|
|
{label: 'Kotlin', value: 'kotlin'}
|
|
]}>
|
|
<TabItem value="java">
|
|
|
|
1. Open your Project in Android Studio
|
|
2. Create a Java source file, for the Face Detector Plugin this will be called `FaceDetectorFrameProcessorPlugin.java`.
|
|
3. Add the following code:
|
|
|
|
```java
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import com.mrousavy.camera.frameprocessor.Frame;
|
|
import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin;
|
|
|
|
public class FaceDetectorFrameProcessorPlugin extends FrameProcessorPlugin {
|
|
FaceDetectorFrameProcessorPlugin(@Nullable Map<String, Object> options) {}
|
|
|
|
@Nullable
|
|
@Override
|
|
public Object callback(@NonNull Frame frame, @Nullable Map<String, Object> arguments) {
|
|
// highlight-next-line
|
|
// code goes here
|
|
return null;
|
|
}
|
|
}
|
|
```
|
|
|
|
4. **Implement your Frame Processing.** See the [Example Plugin (Java)](https://github.com/mrousavy/react-native-vision-camera/blob/main/package/example/android/app/src/main/java/com/mrousavy/camera/example/ExampleFrameProcessorPlugin.java) for reference.
|
|
5. Create a new Java file which registers the Frame Processor Plugin in a React Package, for the Face Detector plugin this file will be called `FaceDetectorFrameProcessorPluginPackage.java`:
|
|
|
|
```java
|
|
import androidx.annotation.NonNull;
|
|
import com.facebook.react.ReactPackage;
|
|
import com.facebook.react.bridge.NativeModule;
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
import com.facebook.react.uimanager.ViewManager;
|
|
import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin;
|
|
import com.mrousavy.camera.frameprocessor.FrameProcessorPluginRegistry;
|
|
|
|
public class FaceDetectorFrameProcessorPluginPackage implements ReactPackage {
|
|
// highlight-start
|
|
FaceDetectorFrameProcessorPluginPackage() {
|
|
FrameProcessorPluginRegistry.addFrameProcessorPlugin("detectFaces", options -> new FaceDetectorFrameProcessorPlugin(options));
|
|
}
|
|
// highlight-end
|
|
|
|
@NonNull
|
|
@Override
|
|
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
|
|
return Collections.emptyList();
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
|
|
return Collections.emptyList();
|
|
}
|
|
}
|
|
```
|
|
|
|
:::note
|
|
The Frame Processor Plugin will be exposed to JS through the `VisionCameraProxy` object. In this case, it would be `VisionCameraProxy.getFrameProcessorPlugin("detectFaces")`.
|
|
:::
|
|
|
|
6. Register the package in MainApplication.java
|
|
|
|
```java
|
|
@Override
|
|
protected List<ReactPackage> getPackages() {
|
|
@SuppressWarnings("UnnecessaryLocalVariable")
|
|
List<ReactPackage> packages = new PackageList(this).getPackages();
|
|
// ...
|
|
// highlight-next-line
|
|
packages.add(new FaceDetectorFrameProcessorPluginPackage()); // <- add
|
|
return packages;
|
|
}
|
|
```
|
|
|
|
</TabItem>
|
|
<TabItem value="kotlin">
|
|
|
|
1. Open your Project in Android Studio
|
|
2. Create a Kotlin source file, for the Face Detector Plugin this will be called `FaceDetectorFrameProcessorPlugin.kt`.
|
|
3. Add the following code:
|
|
|
|
```kotlin
|
|
import com.mrousavy.camera.frameprocessor.Frame
|
|
import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin
|
|
|
|
class FaceDetectorFrameProcessorPlugin(options: Map<String, Any>?): FrameProcessorPlugin(options) {
|
|
|
|
override fun callback(frame: Frame, arguments: Map<String, Any>?): Any? {
|
|
// highlight-next-line
|
|
// code goes here
|
|
return null
|
|
}
|
|
}
|
|
```
|
|
|
|
4. **Implement your Frame Processing.** See the [Example Plugin (Java)](https://github.com/mrousavy/react-native-vision-camera/blob/main/package/example/android/app/src/main/java/com/mrousavy/camera/example/ExampleFrameProcessorPlugin.java) for reference.
|
|
5. Create a new Kotlin file which registers the Frame Processor Plugin in a React Package, for the Face Detector plugin this file will be called `FaceDetectorFrameProcessorPluginPackage.kt`:
|
|
|
|
```kotlin
|
|
import com.facebook.react.ReactPackage
|
|
import com.facebook.react.bridge.NativeModule
|
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
import com.facebook.react.uimanager.ViewManager
|
|
import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin
|
|
|
|
class FaceDetectorFrameProcessorPluginPackage : ReactPackage {
|
|
// highlight-start
|
|
init {
|
|
FrameProcessorPluginRegistry.addFrameProcessorPlugin("detectFaces") { options ->
|
|
FaceDetectorFrameProcessorPlugin(options)
|
|
}
|
|
}
|
|
// highlight-end
|
|
|
|
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
|
|
return emptyList()
|
|
}
|
|
|
|
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
|
|
return emptyList()
|
|
}
|
|
}
|
|
```
|
|
|
|
:::note
|
|
The Frame Processor Plugin will be exposed to JS through the `VisionCameraProxy` object. In this case, it would be `VisionCameraProxy.getFrameProcessorPlugin("detectFaces")`.
|
|
:::
|
|
|
|
6. Register the package in MainApplication.java
|
|
|
|
```java
|
|
@Override
|
|
protected List<ReactPackage> getPackages() {
|
|
@SuppressWarnings("UnnecessaryLocalVariable")
|
|
List<ReactPackage> packages = new PackageList(this).getPackages();
|
|
// ...
|
|
// highlight-next-line
|
|
packages.add(new FaceDetectorFrameProcessorPluginPackage()); // <- add
|
|
return packages;
|
|
}
|
|
```
|
|
|
|
</TabItem>
|
|
</Tabs>
|
|
|
|
<br />
|
|
|
|
#### 🚀 Next section: [Finish creating your Frame Processor Plugin](frame-processors-plugins-final) (or [add iOS support to your Frame Processor Plugin](frame-processors-plugins-ios))
|