1

我试图在我的应用程序中实现一个本地库。但无论我尝试什么,我都会不断出错。所以我寻求一点帮助!

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 的黑巫师可以帮助我,我会非常高兴!

4

1 回答 1

4

LogCat 说你的 CamLib 类应该在leifdev.com包中,但你的 JNI 函数希望它在leifdev.com.WaveNet

所以将你的 JNI 函数重命名为Java_leifdev_com_CamLib_getSobel

于 2012-08-02T01:42:03.467 回答