简体   繁体   中英

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

I'm trying to integrate OpenCV in Android Java native interface using C++ language. I have placed the OpenCV inside the jni folder, where I'm writing my C++ code. I have included the Opencv header files in my HelloNative.c file. But I'm still getting this error while trying to access Mat object. " Can't resolve variable Mat ".

I have tried using namespace cv , but it gives an error to predeclare using and namespace ; which is not a solution. I'm posting my code below, please somebody have a look at it and let me know what I'm doing wrong.

#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);
}

There are two functions in this file, the first function getStringFromJNI is working perfectly fine. Thank you.

After a lot of research, I finally managed to find out a good solution for my question. Please follow below mentioned tutorial: https://github.com/leadrien/opencv_native_androidstudio You'll be facing some issues while integrating it. You can post those solutions in the comments and I will guide you through each. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM