libc++_shared.so
之前默认集成在 opencv_java3.so
,但是在OpenCV4.x以后,该动态库默认不集成在opencv_java4.so
。
参考 https://github.com/opencv/opencv/issues/15567#issuecomment-1016603238。这个是OpenCV安卓SDK的问题,不影响正常使用OpenCV库。
从OpenCV官网下载OpenCV4.6.0的OpenCV for Android压缩文件opencv-4.6.0-android-sdk.zip。
利用 Android Studio 3.0.1 创建一个android项目,这里我创建的是OpenCV4Android项目。
/**
* Loads and initializes OpenCV library using OpenCV Engine service.
* @param Version OpenCV library version.
* @param AppContext application context for connecting to the service.
* @param Callback object, that implements LoaderCallbackInterface for handling the connection status.
* @return Returns true if initialization of OpenCV is successful.
*/
// public static boolean initAsync(String Version, Context AppContext,
// LoaderCallbackInterface Callback)
// {
// return AsyncServiceHelper.initOpenCV(Version, AppContext, Callback);
// }
复制 OpenCV-android-sdk/sdk/java/res/values/attrs.xml 到 opencv-android/src/main/res/values/ 文件内;
修改 opencv-android/src/main/res/values/string.xml ,修改后如下所示:
OpenCV-4.6.0
把 OpenCV-android-sdk/sdk/native/libs 文件夹下的子文件夹和 OpenCV-android-sdk/sdk/native/3rdparty/libs 文件夹下的子文件夹拷贝 opencv-android/src/main 文件下创建的 jniLibs 文件夹内。
为
添加id,修改后代码片段如下所示:
+
号,选择 3 Module Dependency ,在弹出的对话框中,选择 :opencv-android ,最后依次点击 OK,OK。添加TextView对象,如果点击该对象,则弹出一个短消息!
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView) findViewById(R.id.text_view);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "You clicked textView!", Toast.LENGTH_SHORT).show();
if (OpenCVLoader.initDebug()) {
textView.setText("OpenCV-4.6.0 loaded successful!");
}
}
});
}
}
/home/tianzx/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so
到 opencv-android/src/main/jniLibs/arm64-v8a/
/home/tianzx/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libc++_shared.so
到opencv-android/src/main/jniLibs/armeabi-v7a/
/home/tianzx/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/i686-linux-android/libc++_shared.so
到 opencv-android/src/main/jniLibs/x86/
/home/tianzx/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/libc++_shared.so
到 opencv-android/src/main/jniLibs/x86_64/
点击 Run->Run ‘app’ ,连接手机的界面如下所示:
点击,Hello World!后的界面如下所示:
在opencv-android/build/outputs/aar文件夹下,生成的debug和release两种格式的aar文件夹。
opencv-android-debug.aar
opencv-android-release.aar
参考这里
修改app和opencv-android文件夹内的build.gradle文件中的buildTypes
字段,仅生成适合指定平台的apk文件。
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
// added by JohnTian
splits {
abi {
enable true
reset()
include 'armeabi-v7a', 'arm64-v8a'
universalApk false
}
}
}