如何在Android中使用jni调用编译OpenCV本机

时间:2016-07-15 09:08:13

标签: java c++ opencv android-ndk java-native-interface

我是使用Android和JNI调用的新手。我用C ++编写了一个简单的算法,使用OpenCV来检测面部。 现在要对Android做同样的事情,我想用Java调用C ++中的算法。 问题是当我尝试编译OpenCV本机时,Gradle构建说:"错误:opencv2 / opencv.hpp:没有这样的文件或目录" 我无法从本机OpenCV编译头文件。

这是我的Android.mk文件:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)


# OpenCV
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=on
OPENCV_LIB_TYPE:=SHARED

include <path_to_android_OpenCV>/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk

LOCAL_MODULE := opencvjni
LOCAL_CFLAGS := -I<path_to_android_OpenCV>/OpenCV-android-sdk/sdk/native/jni/include -Wall -Werror
LOCAL_SRC_FILES := openCVJNI.c detect_face.cpp

LOCAL_C_INCLUDES += <path_to_project>/app/src/main/jni
LOCAL_C_INCLUDES += <path_to_android_OpenCV>/OpenCV-android-sdk/sdk/native/jni/include

LOCAL_STATIC_LIBRARIES := <path_to_android_OpenCV>/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/*.a

LOCAL_LDLIBS += -llog -ldl

include $(BUILD_SHARED_LIBRARY)

那是我的Application.mk:

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8

那是我的gradle.build:

apply plugin: 'com.android.model.application'

model {
  android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion.apiLevel 22
        targetSdkVersion.apiLevel 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles.add(file('proguard-android.txt'))
        }
    }
    ndk {
        moduleName "openCVJNI"
        cppFlags.add("-std=c++11") // Add provisions to allow C++11 functionality
        cppFlags.add("-fexceptions")
        stl = "gnustl_shared"
    }
    task ndkBuild(type: Exec) {
        commandLine '<_path_to_NDK>/ndk-bundle/build/ndk-build', '-C', file('app/src/main/jni').absolutePath
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
  }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.android.support:design:23.0.0'
}

如果有人能帮助我,我们会感激不尽。我对此感到害怕。

谢谢!

1 个答案:

答案 0 :(得分:0)

https://developer.android.com/ndk/guides/android_mk.html

在下面的链接中,它在LOCAL_C_INCLUDES部分提到:

  

在设置任何相应的包含标志之前定义此变量   通过LOCAL_CFLAGS或LOCAL_CPPFLAGS。