OpenCV in Android Studio

前端 未结 8 1785
独厮守ぢ
独厮守ぢ 2020-11-22 02:43

I want to use OpenCV library in my app with Android Studio. I followed instructions found here but I get error

Configuration with name \'default\' no

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 03:03

    Android Studio 3.4 + OpenCV 4.1

    1. Download the latest OpenCV zip file from here (current newest version is 4.1.0) and unzip it in your workspace or in another folder.

    2. Create new Android Studio project normally. Click File->New->Import Module, navigate to /path_to_unzipped_files/OpenCV-android-sdk/sdk/java, set Module name as opencv, click Next and uncheck all options in the screen.

    3. Enable Project file view mode (default mode is Android). In the opencv/build.gradle file change apply plugin: 'com.android.application' to apply plugin: 'com.android.library' and replace application ID "org.opencv" with

      minSdkVersion 21
      targetSdkVersion 28
      

      (according the values in app/build.gradle). Sync project with Gradle files.

    4. Add this string to the dependencies block in the app/build.gradle file

      dependencies {
          ...
          implementation project(path: ':opencv')
          ...
      }
      
    5. Select again Android file view mode. Right click on app module and goto New->Folder->JNI Folder. Select change folder location and set src/main/jniLibs/.

    6. Select again Project file view mode and copy all folders from /path_to_unzipped_files/OpenCV-android-sdk/sdk/native/libs to app/src/main/jniLibs.

    7. Again in Android file view mode right click on app module and choose Link C++ Project with Gradle. Select Build System ndk-build and path to OpenCV.mk file /path_to_unzipped_files/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk.

      path_to_unzipped_files must not contain any spaces, or you will get error!

    To check OpenCV initialization add Toast message in MainActivity onCreate() method:

    Toast.makeText(MainActivity.this, String.valueOf(OpenCVLoader.initDebug()), Toast.LENGTH_LONG).show();
    

    If initialization is successful you will see true in Toast message else you will see false.

提交回复
热议问题