Brahmilator has a React-Native client-side component which is fully configured for Android Platform. In order to maximize the efficiency of the pre - process module React-Native has been configured with OpenCV as a native module.
Pre-process module uses the device’s camera to take a photo, processes it with native code and returns the processed image as a Base64 String. Doing it in plain JavaScript would be highly ineffective.
- Download the OpenCV Android SDK In my case, it was v4.5.2
- Extract the zip file
- Open Android Studio and open the 'android' folder inside your React Native project
- File > New > Import Module
- Select
OpenCV-android-sdk/sdk/java
- Change module name to
OpenCV452
and untick all the options on the next screen - From top left, change the display from Android to Project
- Open
build.gradle
of opencv module - Change
apply plugin: 'com.android.application'
toapply plugin: 'com.android.library'
- Delete this line
applicationId: "org.opencv"
- File > Project Structure and click on Dependencies on the left side
- Select app and click on + then Module Dependency and select opencv
- Create a folder inside
android/app/src/main/
named jniLibs - Copy the contents of
OpenCV-android-sdk/sdk/native/libs
to jniLibs - If you are using
react-native-camera
your app will not build. To fix this addmultiDexEnabled true
under defaultConfig insideandroid/app/build.gradle
- Now follow from Step 7 to end from this blog
- After you are done open
RNOpenCvLibraryModule.java
- Whatever function you write under
@ReactMethod
will be accessible from Javascript - Example
public void toGrayscale(String imageAsBase64, Callback errorCallback, Callback successCallback) {
try {
// do your stuff here like Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2GRAY)
// to return your processed image back to js use the following line
successCallback.invoke(abc);
}
catch (Exception e) {
errorCallback.invoke(e.getMessage());
}
}
- Inside the React-Native
OpenCV.toGrayScale(img, (e) => console.log(e), (img) => {
// do whatever you want with the processed img
})
Courtesy: Dark Matter