OpenCV 和 C++:"Can't resolve variable 'Mat`"

OpenCV and C++: "Can't resolve variable 'Mat`"

我正在尝试使用 C++ 语言将 OpenCV 集成到 Android Java 本机界面中。我已将 OpenCV 放在 jni 文件夹中,我正在其中编写 C++ 代码。我在 HelloNative.c 文件中包含了 Opencv 头文件。但是我在尝试访问 Mat 对象时仍然遇到此错误。 “Can't resolve variable Mat”。

我试过using namespace cv,但是预先声明usingnamespace会报错;这不是解决方案。我在下面发布我的代码,请有人看一下,让我知道我做错了什么。

#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/log.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <OpenCV/include/opencv2/opencv.hpp>
#include <OpenCV/include/opencv/cv.h>
#include <OpenCV/modules/core/include/opencv2/core.hpp>
#include <OpenCV/modules/core/include/opencv2/core/core_c.h>
#include <OpenCV/modules/core/include/opencv2/core/mat.hpp>
#include <OpenCV/modules/imgproc/include/opencv2/imgproc/imgproc.hpp>
#include <OpenCV/modules/imgproc/include/opencv2/imgproc/imgproc_c.h>
#include <OpenCV/modules/legacy/include/opencv2/legacy/legacy.hpp>
#include <OpenCV/modules/videostab/include/opencv2/videostab/motion_stabilizing.hpp>
#include <OpenCV/modules/videostab/include/opencv2/videostab/global_motion.hpp>

JNIEXPORT jint JNICALL
Java_com_example_soimporttest_MainActivity_getStringFromJNI(JNIEnv *env, jobject thisObj,
                                                            jstring imagepath) {
    const char *jnamestr = (*env)->GetStringUTFChars(env, imagepath, 0);
    int width, height, gray;
    FILE *fp = fopen(jnamestr, "rb");
    if (fp) {
        //Read .pgm file
        int scan = fscanf(fp, "P5%d%d%d", &width, &height, &gray);
        if (scan != 3 || &gray > 256 || &width > 0xfffff || &height > 0xfffff || &gray <= 1 ||
            &width < 32 || &height < 32) {
            fclose(fp);
            return 9;
        } else {
            return 0;
        }

    } else {
        return -1;
    }
}

JNIEXPORT void JNICALL
Java_com_example_soimporttest_MainActivity_displayImage(JNIEnv *env, jobject thisObj, jclass type, jlong inpAddr, jlong outAddr) {
    ::Mat &src = *(Mat*)inpAddr;
    Mat &dst = *(Mat*)outAddr;
    applyFilter(src , dst);
}

此文件中有两个函数,第一个函数 getStringFromJNI 运行良好。 谢谢。

经过大量研究,我终于找到了解决问题的好办法。请按照下面提到的教程进行操作: https://github.com/leadrien/opencv_native_androidstudio 集成它时您会遇到一些问题。您可以在评论中 post 这些解决方案,我将指导您完成每个解决方案。 谢谢