OpenCV in Android Studio

前端 未结 8 1582
独厮守ぢ
独厮守ぢ 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 02:56
    Download
    

    Get the latest pre-built OpenCV for Android release from https://github.com/opencv/opencv/releases and unpack it (for example, opencv-4.4.0-android-sdk.zip).

    Create an empty Android Studio project

    Open Android Studio. Start a new project. 
    

    Keep default target settings.

    Use "Empty Activity" template. Name activity as MainActivity with a corresponding layout activity_main. Plug in your device and run the project. It should be installed and launched successfully before we'll go next.

     Add OpenCV dependency
    

    Go to File->New->Import module

    and provide a path to unpacked_OpenCV_package/sdk/java. The name of module detects automatically. Disable all features that Android Studio will suggest you on the next window.

    Configure your library build.gradle (openCVLibrary build.gradle)

    apply plugin: 'com.android.library'
    
    android {
        compileSdkVersion 28
        buildToolsVersion "28.0.3"
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    }
    

    Implement the library to the project (application build.gradle)

     implementation project(':openCVLibrary330')
    
    0 讨论(0)
  • 2020-11-22 02:57

    This worked for me and was as easy as adding a gradle dependancy:

    https://bintray.com/seesaa/maven/opencv#

    https://github.com/seesaa/opencv-android

    The one caveat being that I had to use a hardware debugging device as arm emulators were running too slow for me (as AVD Manager says they will), and, as described at the repo README, this version does not include x86 or x86_64 support.

    It seems to build and the suggested test:

    static {
        OpenCVLoader.initDebug();
    }
    

    spits out a bunch of output that looks about right to me.

    0 讨论(0)
  • 2020-11-22 03:00

    The below steps for using Android OpenCV sdk in Android Studio. This is a simplified version of this(1) SO answer.

    1. Download latest OpenCV sdk for Android from OpenCV.org and decompress the zip file.
    2. Import OpenCV to Android Studio, From File -> New -> Import Module, choose sdk/java folder in the unzipped opencv archive.
    3. Update build.gradle under imported OpenCV module to update 4 fields to match your project build.gradle a) compileSdkVersion b) buildToolsVersion c) minSdkVersion and d) targetSdkVersion.
    4. Add module dependency by Application -> Module Settings, and select the Dependencies tab. Click + icon at bottom, choose Module Dependency and select the imported OpenCV module.
      • For Android Studio v1.2.2, to access to Module Settings : in the project view, right-click the dependent module -> Open Module Settings
    5. Copy libs folder under sdk/native to Android Studio under app/src/main.
    6. In Android Studio, rename the copied libs directory to jniLibs and we are done.

    Step (6) is since Android studio expects native libs in app/src/main/jniLibs instead of older libs folder. For those new to Android OpenCV, don't miss below steps

    • include static{ System.loadLibrary("opencv_java"); } (Note: for OpenCV version 3 at this step you should instead load the library opencv_java3.)
    • For step(5), if you ignore any platform libs like x86, make sure your device/emulator is not on that platform.

    OpenCV written is in C/C++. Java wrappers are

    1. Android OpenCV SDK - OpenCV.org maintained Android Java wrapper. I suggest this one.
    2. OpenCV Java - OpenCV.org maintained auto generated desktop Java wrapper.
    3. JavaCV - Popular Java wrapper maintained by independent developer(s). Not Android specific. This library might get out of sync with OpenCV newer versions.
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 03:04

    In your build.gradle

    repositories {
      jcenter()
    }
    
    implementation 'com.quickbirdstudios:opencv:4.1.0'
    

    More infos

    0 讨论(0)
  • 2020-11-22 03:06

    Anybody facing problemn while creating jniLibs cpp is shown ..just add ndk ..

    0 讨论(0)
提交回复
热议问题