Android Studio 配置 OpenCV4+

https://stackoverflow.com/questions/63382489/cant-import-opencv-module-in-android-studio

 

1. 新建Project,选择Native C++。

2. 下载并解压OpenCV Android SDK,如OpenCV4.5.0 Android SDK。

3. File/New/Import Module, 选择sdk文件夹(不是sdk/java文件夹!)

4. File -> Project Structure -> Dependencies -> Module Dependency -> add OpenCV as 'Module Dependency' of 'app'

5. Clean Project

6. 注意代码中需要OpenCVLoader。此时可在Java中使用OpenCV。

 1 package com.example.testopencv;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.os.Bundle;
 6 import android.util.Log;
 7 import android.widget.TextView;
 8 
 9 import org.opencv.android.OpenCVLoader;
10 import org.opencv.core.Mat;
11 import org.opencv.core.CvType;
12 
13 public class MainActivity extends AppCompatActivity {
14     private static final String TAG = "main_activity";
15     // Used to load the 'native-lib' library on application startup.
16     static {
17         if (!OpenCVLoader.initDebug()) {
18             // Handle initialization error
19             Log.d(TAG, "OPENCV DİD NOT LOAD");
20         }
21         System.loadLibrary("native-lib");
22     }
23 
24     @Override
25     protected void onCreate(Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27         setContentView(R.layout.activity_main);
28         Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
29         // Example of a call to a native method
30         TextView tv = findViewById(R.id.sample_text);
31         tv.setText(stringFromJNI());
32     }
33 
34     /**
35      * A native method that is implemented by the 'native-lib' native library,
36      * which is packaged with this application.
37      */
38     public native String stringFromJNI();
39 }

 6. 在native code中使用opencv:

a. 修改CMakeLists.txt,添加内容:

1 find_package(OpenCV 4.5 REQUIRED java)
2 target_link_libraries(native-lib ${OpenCV_LIBRARIES})

b. 修改cmake相关选项(注意是在defaultConfig中的):

1 defaultConfig {
2         ...
3         externalNativeBuild {
4             cmake {
5                 cppFlags "-std=c++14 -frtti -fexceptions"
6                 arguments "-DOpenCV_DIR=" + "/mnt/ntfs_sdb2/linux_partition/android_projects/TestOpencv/sdk/native/jni" // , "-DANDROID_ARM_NEON=TRUE"
7             }
8         }

 


 

OpenCV 官方说明

 1 //
 2 // Notes about integration OpenCV into existed Android Studio application project are below (application 'app' module should exist).
 3 //
 4 // This file is located in <OpenCV-android-sdk>/sdk directory (near 'etc', 'java', 'native' subdirectories)
 5 //
 6 // Add module into Android Studio application project:
 7 //
 8 // - Android Studio way:
 9 //   (will copy almost all OpenCV Android SDK into your project, ~200Mb)
10 //
11 //   Import module: Menu -> "File" -> "New" -> "Module" -> "Import Gradle project":
12 //   Source directory: select this "sdk" directory
13 //   Module name: ":opencv"
14 //
15 // - or attach library module from OpenCV Android SDK
16 //   (without copying into application project directory, allow to share the same module between projects)
17 //
18 //   Edit "settings.gradle" and add these lines:
19 //
20 //   def opencvsdk='<path_to_opencv_android_sdk_rootdir>'
21 //   // You can put declaration above into gradle.properties file instead (including file in HOME directory),
22 //   // but without 'def' and apostrophe symbols ('): opencvsdk=<path_to_opencv_android_sdk_rootdir>
23 //   include ':opencv'
24 //   project(':opencv').projectDir = new File(opencvsdk + '/sdk')
25 //
26 //
27 //
28 // Add dependency into application module:
29 //
30 // - Android Studio way:
31 //   "Open Module Settings" (F4) -> "Dependencies" tab
32 //
33 // - or add "project(':opencv')" dependency into app/build.gradle:
34 //
35 //   dependencies {
36 //       implementation fileTree(dir: 'libs', include: ['*.jar'])
37 //       ...
38 //       implementation project(':opencv')
39 //   }
40 //
41 //
42 //
43 // Load OpenCV native library before using:
44 //
45 // - avoid using of "OpenCVLoader.initAsync()" approach - it is deprecated
46 //   It may load library with different version (from OpenCV Android Manager, which is installed separatelly on device)
47 //
48 // - use "System.loadLibrary("opencv_java4")" or "OpenCVLoader.initDebug()"
49 //   TODO: Add accurate API to load OpenCV native library
50 //
51 //
52 //
53 // Native C++ support (necessary to use OpenCV in native code of application only):
54 //
55 // - Use find_package() in app/CMakeLists.txt:
56 //
57 //   find_package(OpenCV 3.4 REQUIRED java)
58 //   ...
59 //   target_link_libraries(native-lib ${OpenCV_LIBRARIES})
60 //
61 // - Add "OpenCV_DIR" and enable C++ exceptions/RTTI support via app/build.gradle
62 //   Documentation about CMake options: https://developer.android.com/ndk/guides/cmake.html
63 //
64 //   defaultConfig {
65 //       ...
66 //       externalNativeBuild {
67 //           cmake {
68 //               cppFlags "-std=c++11 -frtti -fexceptions"
69 //               arguments "-DOpenCV_DIR=" + opencvsdk + "/sdk/native/jni" // , "-DANDROID_ARM_NEON=TRUE"
70 //           }
71 //       }
72 //   }
73 //
74 // - (optional) Limit/filter ABIs to build ('android' scope of 'app/build.gradle'):
75 //   Useful information: https://developer.android.com/studio/build/gradle-tips.html (Configure separate APKs per ABI)
76 //
77 //   splits {
78 //       abi {
79 //           enable true
80 //           universalApk false
81 //           reset()
82 //           include 'armeabi-v7a' // , 'x86', 'x86_64', 'arm64-v8a'
83 //       }
84 //   }
85 //

 


 https://www.cnblogs.com/imoon22/p/14902927.html

posted @ 2021-06-19 10:54  imoon22  阅读(477)  评论(0编辑  收藏  举报