https://*.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 }