JNI with opencv_java248

asked 2014-05-02 03:28:15 -0500

Leena gravatar image

Hello, I would like to write a JNI on Java with opencv_java248 but I'am having some difficulties. I know I could write it in Java thanks to opencv248.jar and opencv_java248.dll but my superior want me to do a JNI. So I read tutorials,docs etc.. and here what I wrote.

Filter.java :

import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;

public class Filter {

static
{
    System.loadLibrary("opencv_java248");
}

private static native void doNativeHistogramEqualisation(long src);

public static Mat doHistogramEqualisation(Mat src)
{
    doNativeHistogramEqualisation(src.getNativeObjAddr());
    return src;
}

public static void main(String[] args) 
{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);


    for (int i = 1;i<13;i++)
    {
        String path = i+".jpg";
        Mat image = Highgui.imread("images/"+path);
        Mat dst = new Mat();
        dst = doHistogramEqualisation(image);
        //doHoughLineTransform(image, 30, 50, 20, 14);
        Highgui.imwrite("output/"+path,dst);
    }   
}

opencv_Filter.h: (Generated by javah)

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class opencv_Filter */

#ifndef _Included_opencv_Filter
#define _Included_opencv_Filter
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     opencv_Filter
 * Method:    doNativeHistogramEqualisation
 * Signature: (J)J
*/
JNIEXPORT void JNICALL Java_opencv_Filter_doNativeHistogramEqualisation
(JNIEnv *, jclass, jlong);

#ifdef __cplusplus
}
#endif
#endif

opencv_Filter.cpp:

#include "opencv_Filter.h"

#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

JNIEXPORT void JNICALL Java_opencv_Filter_doNativeHistogramEqualisation
(JNIEnv *jenv, jclass,    jlong Jsrc)
{
Mat* src = (Mat*) Jsrc;
src->create(rows, cols, type);
std::memcpy(src->data, data, src->step * src->rows);

if(src.channels() != 1)
    cvtColor( src, src, CV_BGR2GRAY );
equalizeHist( src, src );
return;
}

I make sure the opencv_java248.dll is linked but I keep getting the errors :

Exception in thread "main" java.lang.UnsatisfiedLinkError:        opencv.Filter.doNativeHistogramEqualisation(J)V
    at opencv.Filter.doNativeHistogramEqualisation(Native Method)
    at opencv.Filter.doHistogramEqualisation(Filter.java:18)
    at opencv.Filter.main(Filter.java:32)

I check the type of error and It say come from the native method but I can't figure what is the problem. I think it's because I am not compiling the .cpp but I'm not sure about that.

Thank you, very much if you can help me. Regards

edit retag flag offensive close merge delete

Comments

look here:

 System.loadLibrary("opencv_java248");

you need a second line with the name of your dll there:

 System.loadLibrary("opencv_Filter"); // i guessed here

(if you're using eclipse, put the dll toplevel into your project dir)

berak gravatar imageberak ( 2014-05-02 03:42:29 -0500 )edit

Thank you for your answer, so I must compile opencv_Filter.dll into a .dll and use this instead of opencv_java248.dll ? I don't know how I can do that on Windows on a easy way and I must include opencv in the compilation, right ?

Leena gravatar imageLeena ( 2014-05-02 03:51:54 -0500 )edit

not instead of opencv_java248.dll, your code needs to load both dlls.

berak gravatar imageberak ( 2014-05-02 03:55:20 -0500 )edit

and yes, when you compile that dll, it needs to link against the opencv libs

berak gravatar imageberak ( 2014-05-02 03:58:43 -0500 )edit

Ok, thank you for your answer. I succed to compile in .dll opencv_Filter and the program run.

Leena gravatar imageLeena ( 2014-05-02 04:08:17 -0500 )edit