Android Studio 配置 OpenCV4+

Posted imoon22

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 -> 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 }

 

以上是关于Android Studio 配置 OpenCV4+的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio配置及使用OpenCV

Android Studio中配置及使用OpenCV示例

Android Studio中集成OpenCV

OpenCV学习笔记04:在Visual Studio上使用OpenCV4.5.5

Win10-cmake-gui与Visual Studio 2019配置OpenCV4.2

Windows编译OpenCV4Android解决undefined reference to std错误