如何在 React Native 中使用 OpenCV [英] How to use OpenCV in React Native

查看:124
本文介绍了如何在 React Native 中使用 OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现的所有类似问题或多或少都没有答案.我想检测边缘并改变图像的视角,OpenCV 似乎是正确的选择.Google 搜索中会弹出一个指南,该指南非常冗长且令人困惑.

All the similar questions I found were more or less unanswered. I want to detect edges and change the perspective of images and OpenCV seems to be the right choice. There is one guide that pops up on Google search which is very long and confusing.

推荐答案

我终于能够将 OpenCV 与 React Native 结合使用.由于 React Native 并未正式支持 OpenCV,我们将不得不使用 Native Modules.请注意,您必须使用 Java 才能使用 OpenCV 功能.

I was finally able to use OpenCV with React Native. Since OpenCV isn't officially supported by React Native we will have to use Native Modules. Beware, you will have to use Java to use OpenCV functionalities.

  1. 下载 OpenCV Android SDK.
  2. 解压 zip 文件.
  3. 打开 Android Studio 并打开 React Native 项目中的android"文件夹.
  4. 文件 >新 >导入模块
  5. 选择OpenCV-android-sdk/sdk/java
  6. 将模块名称更改为 opencv(或其他任何内容)并取消选中下一个屏幕上的所有选项.
  7. 从左上角,将显示从 Android 更改为 Project
  8. 打开opencv模块的build.gradle
  9. apply plugin: 'com.android.application' 更改为 apply plugin: 'com.android.library'(第一行)
  10. 删除这一行applicationIdorg.opencv"
  11. 文件 >项目结构,然后单击左侧的依赖项.
  12. 选择app并点击+然后Module Dependency并选择opencv
  13. android/app/src/main/中创建一个名为jniLibs
  14. 的文件夹
  15. OpenCV-android-sdk/sdk/native/libs的内容复制到jniLibs
  16. 如果您使用的是 react-native-camera,您的应用将无法构建.要解决此问题,请在 android/app/build.gradle
  17. 内的 defaultConfig 下添加 multiDexEnabled true
  18. 现在从第 7 步开始到 本博客.
  19. 完成后打开RNOpenCvLibraryModule.java
  20. 您在 @ReactMethod 下编写的任何函数都可以通过 Javascript 访问.
  21. 示例
  1. Download the OpenCV Android SDK.
  2. Extract the zip file.
  3. Open Android Studio and open the 'android' folder inside your React Native project.
  4. File > New > Import Module
  5. Select OpenCV-android-sdk/sdk/java
  6. Change module name to opencv (or anything else) and untick all the options on the next screen.
  7. From top left, change the display from Android to Project
  8. Open build.gradle of opencv module
  9. Change apply plugin: 'com.android.application' to apply plugin: 'com.android.library' (first line)
  10. Delete this line applicationId "org.opencv"
  11. File > Project Structure and click on Dependencies on the left side.
  12. Select app and click on + then Module Dependency and select opencv
  13. Create a folder inside android/app/src/main/ named jniLibs
  14. Copy the contents of OpenCV-android-sdk/sdk/native/libs to jniLibs
  15. If you are using react-native-camera your app will not build. To fix this add multiDexEnabled true under defaultConfig inside android/app/build.gradle
  16. Now follow from Step 7 to end from this blog.
  17. After you are done open RNOpenCvLibraryModule.java
  18. Whatever function you write under @ReactMethod will be accessible from Javascript.
  19. 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()); 
        }
}

  1. Javascript 内部

  OpenCV.toGrayScale(img, (e) => console.log(e), (img) => {
    // do whatever you want with the processed img
  })

这篇关于如何在 React Native 中使用 OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆