我试图在我的应用程序中实现本机库。但无论我尝试什么,我都会遇到错误。所以我要求一点帮助!
lib的函数以java方式存储在这个类中:
public class CamLib {
static{
System.loadLibrary("leifdev_com_WaveNet_CamLib");
}
public static native void getSobel(byte[] frame, int width, int height, IntBuffer diff);
}
正在加载中:
CamLib.getSobel(mFrame, mFrameSize.width, mFrameSize.height, mFrameDiff);
头文件由javah自动生成,如下所示:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include <string.h>
#include <android/log.h>
/* Header for class leifdev_com_WaveNet_CamLib */
#ifndef _Included_leifdev_com_WaveNet_CamLib
#define _Included_leifdev_com_WaveNet_CamLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: leifdev_com_WaveNet_CamLib
* Method: getSobel
* Signature: (J)J
*/
JNIEXPORT void JNICALL Java_leifdev_com_WaveNet_CamLib_getSobel
(JNIEnv *, jclass,
jbyteArray , jint , jint , jobject );
#ifdef __cplusplus
}
#endif
#endif
函数本身来自一个教程,所以我认为它的工作原理如下:
#include "leifdev_com_WaveNet_CamLib.h"
#define LOGTAG "Sobel_Native"
JNIEXPORT void JNICALL Java_leifdev_com_WaveNet_CamLib_getSobel(
JNIEnv *env, jclass c,
jbyteArray frame, jint width, jint height, jobject diff)
{
jboolean framecopy;
jint *dbuf = (jint*)((*env)->GetDirectBufferAddress(env, diff));
jbyte *fbuf = (*env)->GetByteArrayElements(env, frame, &framecopy);
int x, y, maxx=width-1, maxy=height-1, p=width+1, px, py, ps;
for(y=1; y<maxy; y++, p+=2)
{
for(x=1; x<maxx; x++, p++)
{
px = fbuf[p+width+1]-fbuf[p+width-1]+fbuf[p+1]+fbuf[p+1]-fbuf[p-1]-fbuf[p-1]+fbuf[p-width+1]-fbuf[p-width-1];
py = fbuf[p-width-1]+fbuf[p-width]+fbuf[p-width]+fbuf[p-width+1]-fbuf[p+width-1]-fbuf[p+width]-fbuf[p+width]-fbuf[p+width+1];
if(px<0) px=-px; if(py<0) py=-py;
ps=px+py; if(ps>95) ps=255; if(ps<=95) ps=0;
dbuf[p] = (ps<<24)|(ps<<16)|(ps<<8)|ps;
}
}
}
我假设这些是最重要的logcat消息:
08-02 02:07:17.204: D/dalvikvm(15767): No JNI_OnLoad found in /data/data/leifdev.com/lib/libleifdev_com_WaveNet_CamLib.so 0x41a02998, skipping init
08-02 02:07:17.204: W/dalvikvm(15767): No implementation found for native Lleifdev/com/CamLib;.getSobel ([BIILjava/nio/IntBuffer;)V
08-02 02:07:17.204: D/AndroidRuntime(15767): Shutting down VM
08-02 02:07:17.204: W/dalvikvm(15767): threadid=1: thread exiting with uncaught exception (group=0x40c631f8)
08-02 02:07:17.219: V/Camera-JNI(15767): dataCallback(16, 0x7f5778)
08-02 02:07:17.224: V/Camera-JNI(15767): copyAndPost: off=0, size=1036800
08-02 02:07:17.224: V/Camera-JNI(15767): Allocating callback buffer
08-02 02:07:17.334: E/AndroidRuntime(15767): FATAL EXCEPTION: main
08-02 02:07:17.334: E/AndroidRuntime(15767): java.lang.UnsatisfiedLinkError: getSobel
08-02 02:07:17.334: E/AndroidRuntime(15767): at leifdev.com.CamLib.getSobel(Native Method)
(抱歉拼写错误,它变得非常晚。)
我已经在这个阶段停留了几天,我认为这与在本机标题中使用jclass而不是jobject有关,但我真的找不到任何关于它的文档。 所以,如果你们ndk的黑人巫师能帮助我,我真的很开心!
答案 0 :(得分:4)
LogCat说你的类CamLib应该在leifdev.com
包中,但是你的JNI函数希望它在leifdev.com.WaveNet
将JNI函数重命名为Java_leifdev_com_CamLib_getSobel