OpenCV开发之——将官方示例迁移到项目上,androidui开发教程

android{}

sourceSets { //配置地址修改

main {

java.srcDirs = [‘src/main/java’]

aidl.srcDirs = [‘src/main/java’]

res.srcDirs = [‘src/main/res’]

manifest.srcFile ‘src/main/AndroidManifest.xml’

}

}

externalNativeBuild {

cmake {

path ‘src/main/jni/CMakeLists.txt’ //配置地址修改

}

}

4.5.2 project/build.gradle(APP_ABI)

gradle.afterProject { project ->

if (project.pluginManager.hasPlugin(‘com.android.application’)

|| project.pluginManager.hasPlugin(‘com.android.library’)

|| project.pluginManager.hasPlugin(‘com.android.test’)

|| project.pluginManager.hasPlugin(‘com.android.feature’) ) {

if (true) {

gradle.println(“Override build ABIs for the project ${project.name}”)

project.android {

splits {

abi {

enable true

universalApk false

//reset()

//include ‘armeabi-v7a’

//include ‘arm64-v8a’

//include ‘x86’

//include ‘x86_64’

}

}

}

}

if (true) {

gradle.println(“Override lintOptions for the project ${project.name}”)

project.android {

lintOptions {

// checkReleaseBuilds false

abortOnError false

}

}

}

// (you still need to re-build OpenCV with debug information to debug it)

if (true) {

gradle.println(“Override doNotStrip-debug for the project ${project.name}”)

project.android {

buildTypes {

debug {

packagingOptions {

doNotStrip ‘**/*.so’ // controlled by OpenCV CMake scripts

}

}

}

}

}

if (false || project.hasProperty(“doNotStrip”)) {

gradle.println(“Override doNotStrip-release for the project ${project.name}”)

project.android {

buildTypes {

release {

packagingOptions {

doNotStrip ‘**/*.so’ // controlled by OpenCV CMake scripts

}

}

}

}

}

}

}

4.5.3 OpenCV API level is android-21(opencv-sdk的minSdkVersion为21)

D:\Code\Android\MyOpenCV\app\src\main\jni\CMakeLists.txt : C/C++ debug|x86 : CMake Warning at D:/Code/Android/MyOpenCV/sdk/native/jni/abi-x86/OpenCVConfig.cmake:105 (message):

Minimum required by OpenCV API level is android-21

Call Stack (most recent call first):

D:/Code/Android/MyOpenCV/sdk/native/jni/OpenCVConfig.cmake:44 (include)

CMakeLists.txt:8 (find_package)

请将minSdkVersion设置为21

minSdkVeOpenCV开发之——将官方示例迁移到项目上,androidui开发教程_第1张图片
rsion 21

4.5.4 OS independent 冲突

现象

More than one file was found with OS independent path ‘META-INF/native-image/ios-x86_64/jnijavacpp/reflect-config.json’.

解决

packagingOptions {

exclude ‘META-INF/proguard/androidx-annotations.pro’

exclude ‘META-INF/native-image/**’

}

4.5.5 修改DetectionBasedTracker_jni.cpp文件

将示例项目中的头文件copy到DetectionBasedTracker_jni.cpp头部

#include

#include

#include

#include

#include

#include

#define LOG_TAG “FaceDetection/DetectionBasedTracker”

#define LOGD(…) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, VA_ARGS))

using namespace std;

using namespace cv;

inline void vector_Rect_to_Mat(vector & v_rect, Mat& mat)

{

mat = Mat(v_rect, true);

}

class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector

{

public:

CascadeDetectorAdapter(cv::Ptrcv::CascadeClassifier detector):

IDetector(),

Detector(detector)

{

LOGD(“CascadeDetectorAdapter::Detect::Detect”);

CV_Assert(detector);

}

void detect(const cv::Mat &Image, std::vectorcv::Rect &objects)

{

LOGD(“CascadeDetectorAdapter::Detect: begin”);

LOGD(“CascadeDetectorAdapter::Detect: scaleFactor=%.2f, minNeighbours=%d, minObjSize=(%dx%d), maxObjSize=(%dx%d)”, scaleFactor, minNeighbours, minObjSize.width, minObjSize.height, maxObjSize.width, maxObjSize.height);

Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize);

LOGD(“CascadeDetectorAdapter::Detect: end”);

}

virtual ~CascadeDetectorAdapter()

{

LOGD(“CascadeDetectorAdapter::Detect::~Detect”);

}

private:

CascadeDetectorAdapter();

cv::Ptrcv::CascadeClassifier Detector;

};

struct DetectorAgregator

{

cv::Ptr mainDetector;

cv::Ptr trackingDetector;

cv::Ptr tracker;

DetectorAgregator(cv::Ptr& _mainDetector, cv::Ptr& _trackingDetector):

mainDetector(_mainDetector),

trackingDetector(_trackingDetector)

{

CV_Assert(_mainDetector);

CV_Assert(_trackingDetector);

DetectionBasedTracker::Parameters DetectorParams;

tracker = makePtr(mainDetector, trackingDetector, DetectorParams);

}

};

将示例项目中每个方法的实现copcy到对应方法上(nativeCreateObject为例)

修改前

/*

  • Class: com_example_myopencv_DetectionBasedTracker

  • Method: nativeCreateObject

  • Signature: (Ljava/lang/String;I)J

*/

JNIEXPORT jlong JNICALL Java_com_example_myopencv_DetectionBasedTracker_nativeCreateObject

(JNIEnv *, jclass, jstring, jint);

修改后

/*

  • Class: com_example_myopencv_DetectionBasedTracker

  • Method: nativeCreateObject

  • Signature: (Ljava/lang/String;I)J

*/

JNIEXPORT jlong JNICALL Java_com_example_myopencv_DetectionBasedTracker_nativeCreateObject

(JNIEnv * jenv, jclass, jstring jFileName, jint faceSize)

{

LOGD(“Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject enter”);

const char* jnamestr = jenv->GetStringUTFChars(jFileName, NULL);

string stdFileName(jnamestr);

jlong result = 0;

LOGD(“Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject”);

try

{

cv::Ptr mainDetector = makePtr(

makePtr(stdFileName));

cv::Ptr trackingDetector = makePtr(

makePtr(stdFileName));

result = (jlong)new DetectorAgregator(mainDetector, trackingDetector);

if (faceSize > 0)

{

mainDetector->setMinObjectSize(Size(faceSize, faceSize));

//trackingDetector->setMinObjectSize(Size(faceSize, faceSize));

}

}

catch(const cv::Exception& e)

{

LOGD(“nativeCreateObject caught cv::Exception: %s”, e.what());

jclass je = jenv->FindClass(“org/opencv/core/CvException”);

if(!je)

je = jenv->FindClass(“java/lang/Exception”);

jenv->ThrowNew(je, e.what());

}

catch (…)

{

LOGD(“nativeCreateObject caught unknown exception”);

jclass je = jenv->FindClass(“java/lang/Exception”);

jenv->ThrowNew(je, “Unknown exception in JNI code of DetectionBasedTracker.nativeCreateObject()”);

return 0;

}

LOGD(“Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject exit”);

return result;

}

4.6 添加权限

}

catch (…)

{

LOGD(“nativeCreateObject caught unknown exception”);

jclass je = jenv->FindClass(“java/lang/Exception”);

jenv->ThrowNew(je, “Unknown exception in JNI code of DetectionBasedTracker.nativeCreateObject()”);

return 0;

}

LOGD(“Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeCreateObject exit”);

return result;

}

4.6 添加权限

你可能感兴趣的:(程序员,架构,移动开发,android)