我不明白如何使用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文件,我怎么能修改吗?
没有人能帮助我吗?请... –