2017-05-06 48 views
0

我不明白如何使用JNI在java中运行C++代码。 我认为在makefile中有一些错误,我认为有些lib缺失。将lib添加到jni应用程序的makefile中

我有这样的代码在java类:

private native void getCanny(long mat); 
getCanny(mat.getNativeObjAddr()); 

和Mat2Image.h产生:

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

#ifndef _Included_Mat2Image 
#define _Included_Mat2Image 
#ifdef __cplusplus 
extern "C" { 
#endif 
/* 
* Class:  Mat2Image 
* Method: getCanny 
* Signature: (J)V 
*/ 
JNIEXPORT void JNICALL Java_Mat2Image_getCanny 
    (JNIEnv *, jobject, jlong); 

#ifdef __cplusplus 
} 
#endif 
#endif 

,这是我做了的.cpp:

#include "Mat2Image.h" 
#include <iostream> 
#include <opencv2/core/core.hpp> 
#include <opencv2/imgproc.hpp> 


JNIEXPORT void JNICALL Java_Mat2Image_getCanny 
    (JNIEnv * env, jobject obj, jlong matr){ 


     cv::Mat* frame=(cv::Mat*)matr; 
      cv::cvtColor(*frame, *frame, CV_BGR2GRAY); 
      cv::GaussianBlur(*frame, *frame, cv::Size(7,7), 1.5, 1.5); 
      cv::Canny(*frame, *frame, 0, 30, 3); 


} 

这是我的生成文件:

# Define a variable for classpath 
CLASS_PATH = ../bin 

# Debug: -g3=compile with extra debugg infos. -ggdbg3=include things like macro defenitions. -O0=turn off optimizations. 
DEBUGFLAGS = -g3 -ggdb3 -O0 
CFLAGS = $(DEBUGFLAGS) 

# Define a virtual path for .class in the bin directory 
vpath %.class $(CLASS_PATH) 

all : libMat.so 

# [email protected] matches the target, $< matches the first dependancy 
libMat.so : libMat.o 
    g++ $(CFLAGS) -W -shared -o [email protected] $< 

# [email protected] matches the target, $< matches the first dependancy 
libMat.o : Mat2Image.cpp Mat2Image.h 
    g++ $(CFLAGS) -fPIC -I/usr/lib/jvm/jdk1.8.0_111/include -I/usr/lib/jvm/jdk1.8.0_111/include/linux -c $< -o [email protected] 

# $* matches the target filename without the extension 
# manually this would be: javah -classpath ../bin HelloJNI 
HelloJNI.h : Mat2Image.class 
    javah -classpath $(CLASS_PATH) $* 

clean : 
    rm -f Mat2Image.h libMat.o libMat.so 

但是当我尝试运行的方法我有这样的错误:

/usr/lib/jvm/jdk1.8.0_111/bin/java: symbol lookup error: /home/buzzo/Downloads/helloJni-master/jni/libMat.so: undefined symbol: _ZN2cv8cvtColorERKNS_11_InputArrayERKNS_12_OutputArrayEii 

我认为这个问题是makefile文件,我怎么能修改吗?

+0

没有人能帮助我吗?请... –

回答

0

它看起来像你的代码使用未链接的符号。

我建议将你的共享库与opencv链接?

如果你想看看那里的共享库从JNI使用示例代码,看看这里:

https://github.com/mkowsiak/jnicookbook/tree/master/recipeNo023

在你的情况下,它看起来像OpenCV库是不是LD_LIBRARY_PATH。

+0

你有一个很好的使用JNI指南!我会读它! –

+0

我通过修改makefile解决了 –

+0

太棒了!与JNI玩得开心! – mko

0

我通过修改Makefile解决:

我改变

libMat.so : libMat.o 
    g++ $(CFLAGS) -W -shared -o [email protected] $< 

libMat.so : libMat.o 
    g++ $(CFLAGS) -W -shared -o [email protected] $< -lopencv_imgproc 

这是最后生成文件:

# Define a variable for classpath 
CLASS_PATH = ../bin 
# Debug: -g3=compile with extra debugg infos. -ggdbg3=include things like macro defenitions. -O0=turn off optimizations. 
DEBUGFLAGS = -g3 -ggdb3 -O0 
CFLAGS = $(DEBUGFLAGS) 

# Define a virtual path for .class in the bin directory 
vpath %.class $(CLASS_PATH) 

all : libMat.so 

# [email protected] matches the target, $< matches the first dependancy 
libMat.so : libMat.o 
    g++ $(CFLAGS) -W -shared -o [email protected] $< -lopencv_imgproc 

# [email protected] matches the target, $< matches the first dependancy 
libMat.o : Mat2Image.cpp Mat2Image.h 
    g++ $(CFLAGS) -fPIC -I/usr/lib/jvm/jdk1.8.0_111/include -I/usr/lib/jvm/jdk1.8.0_111/include/linux -c $< -o [email protected] 

# $* matches the target filename without the extension 
# manually this would be: javah -classpath ../bin HelloJNI 
HelloJNI.h : Mat2Image.class 
    javah -classpath $(CLASS_PATH) $* 

clean : 
    rm -f libMat.o libMat.so