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’ //配置地址修改
}
}
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
}
}
}
}
}
}
}
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
现象
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/**’
}
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;
}
}
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;
}