Android Studio链接OpenCV静态库

时间:2016-01-10 01:27:18

标签: android c++ opencv android-studio android-ndk

目前我正在开发一个使用OpenCV3进行实时视频处理的项目(应用一组彩色滤镜),我设法让它与Java一起使用没有任何问题。

我接下来要做的是通过NDK将所有Java逻辑实现到C ++(以提高性能)。这样我就不需要将MAT对象转换回字节缓冲区和堡垒,从而节省了几个周期。但我真的不想尝试使用gradle链接.so,.a静态库和头文件,以便在我的.CPP文件中使用。

这是我使用的gradle文件:

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

def opencvandroid_sdk_path = file(project(':opencvandroid').projectDir).absolutePath + "/src/main"

model {

    repositories {
        libs(PrebuiltLibraries) {
            opencv {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_calib3d.a")
                }
            }

            opencv1 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_core.a")
                }
            }

            opencv2 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_features2d.a")
                }
            }

            opencv3 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_flann.a")
                }
            }

            opencv4 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_highgui.a")
                }
            }

            opencv5 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_imgcodecs.a")
                }
            }

            opencv6 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_imgproc.a")
                }
            }

            opencv7 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_ml.a")
                }
            }

            opencv8 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_objdetect.a")
                }
            }

            opencv9 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_photo.a")
                }
            }

            opencv10 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_shape.a")
                }
            }

            opencv11 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_stitching.a")
                }
            }

            opencv12 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_superres.a")
                }
            }

            opencv13 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_video.a")
                }
            }

            opencv14 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_videoio.a")
                }
            }

            opencv15 {
                headers.srcDir "${opencvandroid_sdk_path}/jni/include/opencv2"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${opencvandroid_sdk_path}/jniLibs/${targetPlatform.getName()}/libopencv_videostab.a")
                }
            }
        }
    }

    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "com.example"
            minSdkVersion.apiLevel = 16
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }
    }

    /*
     * native build settings
     */
    android.ndk {
        moduleName = "hello-jni"
        /*
         * Other ndk flags configurable here are
         * cppFlags += "-fno-rtti"
         * cppFlags += "-fno-exceptions"
         * ldLibs    = ["android", "log"]
         * stl       = "system"
         */
        platformVersion = 16 //same as minSdkVersion.apiLevel for better compatibility

        /*def jniPath = opencvandroid_sdk_path + "jniLibs"
        cppFlags += "-I${file(jniPath)}".toString()
        file(jniPath).eachDirRecurse { dir ->
            cppFlags += "-I${file(dir)}".toString()
        }*/

        stl = "c++_static"
        ldLibs.addAll(["atomic", "log", "android"])
    }

    android.sources {
        main {
            jni {
                dependencies {
                    library "opencv1" linkage "static"
                    library "opencv2" linkage "static"
                    library "opencv3" linkage "static"
                    library "opencv4" linkage "static"
                    library "opencv5" linkage "static"
                    library "opencv6" linkage "static"
                    library "opencv7" linkage "static"
                    library "opencv8" linkage "static"
                    library "opencv9" linkage "static"
                    library "opencv10" linkage "static"
                    library "opencv11" linkage "static"
                    library "opencv12" linkage "static"
                    library "opencv13" linkage "static"
                    library "opencv14" linkage "static"
                    library "opencv15" linkage "static"
                }
            }
        }
    }

    android.buildTypes {
        release {
            minifyEnabled = false
        }
    }
}

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

我将我的gradle文件基于我在github上找到的this示例。 现在,我发现尝试这样做的一个问题是OPENCV有几个静态库文件,所以这个post说,我必须在gradle上的不同lib上添加每个文件,以便链接它们,这就是为什么我的gradle文件充满了重复的库定义。

现在,当我试图在我的.CPP代码中包含头文件时,Android Studio找不到它们(甚至不能使用命名空间)。这是我的CPP代码:

#include <jni.h>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"
#include <vector>

using namespace std;
using namespace cv;

extern "C" {
JNIEXPORT void JNICALL Java_com_example_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba);

JNIEXPORT void JNICALL Java_com_example_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba)
{
    Mat& mGr  = *(Mat*)addrGray;
    Mat& mRgb = *(Mat*)addrRgba;
    vector<KeyPoint> v;

    Ptr<FeatureDetector> detector = FastFeatureDetector::create(50);
    detector->detect(mGr, v);
    for( unsigned int i = 0; i < v.size(); i++ )
    {
        const KeyPoint& kp = v[i];
        circle(mRgb, Point(kp.pt.x, kp.pt.y), 10, Scalar(255,0,0,255));
    }
}
}

我是否遗漏了某些内容以便正确链接?

1 个答案:

答案 0 :(得分:3)

OpenCV静态库之间存在依赖关系。所以你需要以正确的顺序包含它们。 .a和.o包含依赖于GCC的顺序,具有依赖关系的文件必须包含在它们所依赖的文件之前:

CV_LIBS = \
    $(CV_LIB_FOLDER)/libopencv_calib3d.a \
    $(CV_LIB_FOLDER)/libopencv_highgui.a \
    $(CV_LIB_FOLDER)/libopencv_video.a \
    $(CV_LIB_FOLDER)/libopencv_objdetect.a \
    $(CV_LIB_FOLDER)/libopencv_imgproc.a \
    $(CV_LIB_FOLDER)/libopencv_imgcodecs.a \
    $(CV_LIB_FOLDER)/libopencv_core.a \
    $(CV_LIB_FOLDER)/libopencv_hal.a