我已经给了一个基于人脸识别的项目,用JavaCV编写来改进。其想法是要么使用C++重写程序,要么编写JNI绑定以仍然使用Java。我做了一些研究,根据官方网站OpenCV的说法,2.4.4版已经为Java和Python绑定了。所以,既然官方网站这么说,我决定跟它一起去。请注意,我以前没有用C++编程,过去也没有写过任何JNI包装器。如果官方网站没有声明它包含Java绑定,我将使用Qt使用C++编写它。
通过这种或另一种方式,我完成了我想要做的与Java项目有关的所有事情,并留下了面部识别部分。不幸的是(相关问题1,相关问题2),我发现FaceRecognizer有一个bug,用于人脸识别的JNI包装器必须手工编写。
我找到了一个很好的jni c++ java教程,我试着在上面链接的相关问题#1中提到的代码中使用它。下面的截图显示了我现在所得到的。

代码如下: LBPHFaceRecognizer.java
import org.opencv.contrib.FaceRecognizer;
public class LBPHFaceRecognizer extends FaceRecognizer
{
static{
System.loadLibrary("opencv_java248");
System.loadLibrary("facerec");
}
private static native long n_createLBPHFaceRecognizer();
public LBPHFaceRecognizer()
{
super(n_createLBPHFaceRecognizer());
}
FaceRecognizer facerec = new LBPHFaceRecognizer();
} LBPHFaceRecognizer.c
// facerec.dll
#include "jni.h"
#include "opencv2/contrib/contrib.hpp"
extern "C" {
JNIEXPORT jlong JNICALL Java_org_matxx_n_createLBPHFaceRecognizer(JNIEnv* env, jclass, jint);
JNIEXPORT jlong JNICALL Java_org_matxx_n_createLBPHFaceRecognizer(JNIEnv* env, jclass, jint)
{
try {
cv::Ptr<cv::FaceRecognizer> ptr = cv::createLBPHFaceRecognizer();
cv::FaceRecognizer * pf = ptr.get();
ptr.addref(); //don't let it self-destroy here..
return (jlong) pf;
} catch (...) {
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "sorry, dave..");
}
return 0;
}
} // extern "C"makefile
# Define a variable for classpath
CLASS_PATH = ../bin
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : facerec.dll
# $@ matches the target, $< matches the first dependancy
facerec.dll : LBPHFaceRecognizer.o
gcc -m64 -Wl,--add-stdcall-alias -shared -o $@ $<
# $@ matches the target, $< matches the first dependancy
LBPHFaceRecognizer.o : LBPHFaceRecognizer.c LBPHFaceRecognizer.h
gcc -m64 -I"C:\Program Files\Java\jdk1.7.0_51\include" -I"C:\Program Files\Java\jdk1.7.0_51\include\win32" -I"C:\Users\User\Desktop\OPENCVINSTALLATION\opencv" -I"C:\Users\User\Desktop\OPENCVINSTALLATION\opencv\build\java\x64" -c $< -o $@
# $* matches the target filename without the extension
LBPHFaceRecognizer.h : LBPHFaceRecognizer.class
javah -classpath $(CLASS_PATH) $*
clean :
rm LBPHFaceRecognizer.h LBPHFaceRecognizer.o facerec.dll总之,我成功地创建了facerec.dll,但是存在一些问题。首先,当我创建facerec.dll时,LBPHFacerecognizer.java没有将导入放在顶部,因为它在抱怨它。之后,我添加了它,只是为了不看到错误。其次,.c代码抱怨所有的事情,如下面的截图所示。

有趣的是,它不会抱怨jni.h导入,而下面的屏幕截图所示的头文件就是这样。

就这样,有人能看一下然后告诉我我做得对吗?有什么需要我改变的吗?考虑到dll已经创建,这些错误可以吗?或者有人曾经这样做过,并且愿意共享dll文件。我还没有准备好用JavaCV代码的java等价物来测试它。
我使用OpenCV 2.4.8,Win 7 64位,明w 64位,Java 64位7__51发行版,Eclipse64位。
提前谢谢。
https://stackoverflow.com/questions/22948553
复制相似问题