Opency官方例程是用glade4.2.1构建的,所以想使用glade7对官方例程重新构建。由于自己对android studio的glade架构不熟悉,走了不少弯路,刚开始使用glade7重新编译opencv官方例程,编译能通过,但是app总是会闪退。
下面这篇文章很有借鉴意义,分享一下:
https://juejin.cn/post/7160337743552675847
看完这篇文章重新构建例程后app可以使用了。
1.glade版本:
2.新建工程导入官方例程
修改AndroidManifest.xml
其它需要的资源文件如res,xml按要求copy进来就好。
3.导入官方sdk,重新命名为libopencv,并设置为app的module依赖
4.重新写project的settings.gradle
5.重新写project的build.gradle
6.重新写app的build.gradle
下面是glade文件源码,保存一下大家不用看。
settings.gradle:
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = “MyCameraCalibration”
include ‘:app’
include ‘:libopencv’
build.gradle (project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id ‘com.android.application’ version ‘7.4.2’ apply false
id ‘com.android.library’ version ‘7.4.2’ apply false
}
build.gradle (app)
plugins {
id ‘com.android.application’
}
android {
compileSdkVersion 33
defaultConfig {
applicationId “org.opencv.samples.cameracalibration”
minSdkVersion 21
targetSdkVersion 33
versionCode 301
versionName “3.01”
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
}
sourceSets {
main {
java.srcDirs = [‘src’]
aidl.srcDirs = [‘src’]
res.srcDirs = [‘res’]
manifest.srcFile ‘AndroidManifest.xml’
}
}
}
dependencies {
//implementation fileTree(dir: ‘libs’, include: [‘*.jar’])
// implementation project(‘:opencv’)
implementation project(path: ‘:libopencv’)
}
build.gradle (libopnecv module) ,保持官方sdk没有变
apply plugin: ‘com.android.library’
//apply plugin: ‘kotlin-android’
def openCVersionName = “4.6.0”
def openCVersionCode = ((4 * 100 + 6) * 100 + 0) * 10 + 0
println "OpenCV: " +openCVersionName + " " + project.buildscript.sourceFile
android {
compileSdkVersion 26
defaultConfig {
minSdkVersion 21
targetSdkVersion 26
versionCode openCVersionCode
versionName openCVersionName
externalNativeBuild {
cmake {
arguments "-DANDROID_STL=c++_shared"
targets "opencv_jni_shared"
}
}
}
buildTypes {
debug {
packagingOptions {
doNotStrip '**/*.so' // controlled by OpenCV CMake scripts
}
}
release {
packagingOptions {
doNotStrip '**/*.so' // controlled by OpenCV CMake scripts
}
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['native/libs']
java.srcDirs = ['java/src']
aidl.srcDirs = ['java/src']
res.srcDirs = ['java/res']
manifest.srcFile 'java/AndroidManifest.xml'
}
}
externalNativeBuild {
cmake {
path (project.projectDir.toString() + '/libcxx_helper/CMakeLists.txt')
}
}
}
dependencies {
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.michael.mycameracalibration”
<application
android:label="@string/app_name"
android:icon="@drawable/icon">
<activity android:name="CameraCalibrationActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<supports-screens android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>