c++ code of opencv project , run by java jni

asked 2019-04-17 01:22:10 -0600

updated 2019-04-17 01:36:29 -0600

berak gravatar image

Hi i have c++ code of opencv project , i will run this code by java jni my os is ubuntu , i create .so file by c++ code and try to run by java , but show this error :

main.so: undefined symbol: _ZN2cv6imreadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi

my c++ code :

#include <iostream>
#include "jni.h"
#include "OpencvCpp.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <string.h>

using namespace cv;
using namespace std;


JNIEXPORT jfloat JNICALL Java_OpencvCpp_myFirsttest
        (JNIEnv *, jobject){

//int main() {

    Mat img, img2;

    string filename = "/home/mohsen/Desktop/1.jpg";
    img = imread(filename);
    float m;
    for (int i = 0; i < 96; ++i) {
        for (int j = 0; j < 96; ++j) {
            m = m + img.at<uchar>(i, j);
        }

    }
    cout << m;
    return m;
}

my java.h code : 
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class OpencvCpp */

#ifndef _Included_OpencvCpp
#define _Included_OpencvCpp
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     OpencvCpp
 * Method:    myFirsttest
 * Signature: ()F
 */
JNIEXPORT jfloat JNICALL Java_OpencvCpp_myFirsttest
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

my java code :
public class Main {

    static {
        System.load("/home/mohsen/IdeaProjects/DllTest/src/main.so");
    }

    public static void main(String[] args) {

        System.out.println(new OpencvCpp().myFirsttest());

    }
}

please help me!

edit retag flag offensive close merge delete

Comments

opencv version ?

does the error show up while linking your program ? did you forget -lopencv_imgcodecs there ?

(also note, that you can't use anything from the c++ highgui module from java, there's a "mock" replacement module (pure java) for this in the opencv wrappers)

berak gravatar imageberak ( 2019-04-17 01:44:59 -0600 )edit

version of opencv is 4.1

link_directories(-I/usr/local/include) link_libraries(-lopencv_imgcodecs) i add this text to cmakeList

Mohsen Shafaghi gravatar imageMohsen Shafaghi ( 2019-04-17 02:35:58 -0600 )edit

i add this text to cmakeList

aaand ?

berak gravatar imageberak ( 2019-04-17 03:34:00 -0600 )edit