完全丢失,花了几天NDK错误,请帮忙
我正在尝试从我的java类中调用用C编写的函数,以便在android应用程序中使用它。但是当我运行应用程序时,我得到以下logcat
D / mytag(27455):before_lib
D / dalvikvm(27455):试图加载 lib /data/data/c.google.facedetect/lib/libface-detect.so 0x40517490
D / dalvikvm(27455):添加了共享库 /data/data/c.google.facedetect/lib/libface-detect.so 0x40517490
I / native_tag(27455):Jni_OnLoad_Called
W / dalvikvm(27455): JNI_OnLoad返回了错误的版本(-1) /data/data/c.google.facedetect/lib/libface-detect.so 0x40517490
W / dalvikvm(27455):异常Ljava / lang / NoClassDefFoundError; 初始化Lc / google / facedetect / FaceDetect时抛出;
W / dalvikvm(27455):类init在newInstance调用中失败 (Lc / google / facedetect / FaceDetect;)
W / dalvikvm(27455):threadid = 1: 线程退出与未捕获的异常(组= 0x40018578)
E / AndroidRuntime(27455):致命异常:主要
E / AndroidRuntime(27455):java.lang.ExceptionInInitializerError
E / AndroidRuntime(27455):at java.lang.Class.newInstanceImpl(Native方法)
E / AndroidRuntime(27455):at java.lang.Class.newInstance(Class.java:1409)
E / AndroidRuntime(27455):at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E / AndroidRuntime(27455):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
E / AndroidRuntime(27455):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
E / AndroidRuntime(27455):at android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
E / AndroidRuntime(27455):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)
E / AndroidRuntime(27455):at android.os.Handler.dispatchMessage(Handler.java:99)
E / AndroidRuntime(27455):at android.os.Looper.loop(Looper.java:130)
E / AndroidRuntime(27455): 在android.app.ActivityThread.main(ActivityThread.java:3687)
E / AndroidRuntime(27455):at java.lang.reflect.Method.invokeNative(Native Method)
E / AndroidRuntime(27455):at java.lang.reflect.Method.invoke(Method.java:507)
E / AndroidRuntime(27455):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)
E / AndroidRuntime(27455):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
E / AndroidRuntime(27455):at dalvik.system.NativeStart.main(本机方法)
E / AndroidRuntime(27455):引起: java.lang.NoClassDefFoundError:C
e / AndroidRuntime(27455):at java.lang.Runtime.nativeLoad(Native方法)
E / AndroidRuntime(27455):at java.lang.Runtime.loadLibrary(Runtime.java:432)
E / AndroidRuntime(27455):at java.lang.System.loadLibrary(System.java:554)
E / AndroidRuntime(27455):at c.google.facedetect.FaceDetect。(FaceDetect.java:62)
E / AndroidRuntime(27455):... 15更多
E / AndroidRuntime(27455):引起: java.lang.ClassNotFoundException:C在loader中 dalvik.system.PathClassLoader [/data/app/c.google.facedetect-1.apk]
E / AndroidRuntime(27455):at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
E / AndroidRuntime(27455):at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
E / AndroidRuntime(27455):at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E / AndroidRuntime(27455):... 19更多
pacakage名称是c.google.facedetect
我的java类是FaceDetect.java,如下所示:
package c.google.facedetect;
import android.app.Activity;
.
.
public class FaceDetect extends Activity implements SurfaceHolder.Callback, Camera.PreviewCallback
{
.
.
int[] argb8888 = new int[CAMERA_WIDTH * CAMERA_HEIGHT];
static
{
Log.d("mytag", "before_lib");
System.loadLibrary("face-detect");
Log.d("mytag", "after_lib");
}
.
.
public void onCreate(Bundle savedInstanceState)
{
.
.
decodeYUV(argb8888, data, CAMERA_WIDTH, CAMERA_HEIGHT);
.
.
}
public static native void decodeYUV(int[] out, byte[] fg, int width, int height);
}
我的jni生成的标头是c_google_facedetect_FaceDetect.h并放在jni文件夹中,如下所示:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class c_google_facedetect_FaceDetect */
#ifndef _Included_c_google_facedetect_FaceDetect
#define _Included_c_google_facedetect_FaceDetect
#ifdef __cplusplus
extern "C" {
#endif
#undef c_google_facedetect_FaceDetect_CAMERA_WIDTH
#define c_google_facedetect_FaceDetect_CAMERA_WIDTH 480L
#undef c_google_facedetect_FaceDetect_CAMERA_HEIGHT
#define c_google_facedetect_FaceDetect_CAMERA_HEIGHT 320L
/*
* Class: c_google_facedetect_FaceDetect
* Method: decodeYUV
* Signature: ([I[BII)V
*/
JNIEXPORT void JNICALL Java_c_google_facedetect_FaceDetect_decodeYUV(JNIEnv *, jclass, jintArray, jbyteArray, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
我的C文件名为face-detect.c,位于jni文件夹中,如下所示:
#include <jni.h>
#include <stdio.h>
#include <android/log.h>
int* rgbData;
int rgbDataSize = 0;
JavaVM *cached_jvm;
jclass Class_C;
jmethodID MID_C_g;
JNIEXPORT void JNICALL Java_c_google_facedetect_FaceDetect_decodeYUV(
JNIEnv * env, jobject obj, jintArray rgb, jbyteArray yuv420sp,
jint width, jint height)
{
int sz;
int i;
int j;
int Y;
int Cr = 0;
int Cb = 0;
int pixPtr = 0;
int jDiv2 = 0;
int R = 0;
int G = 0;
int B = 0;
int cOff;
int w = width;
int h = height;
sz = w * h;
jbyte* yuv = yuv420sp;
if (rgbDataSize < sz)
{
int tmp[sz];
rgbData = &tmp[0];
rgbDataSize = sz;
//__android_log_write(ANDROID_LOG_INFO, "JNI", "alloc");
}
for (j = 0; j < h; j++)
{
pixPtr = j * w;
jDiv2 = j >> 1;
for (i = 0; i < w; i++)
{
Y = yuv[pixPtr];
if (Y < 0)
Y += 255;
if ((i & 0x1) != 1)
{
cOff = sz + jDiv2 * w + (i >> 1) * 2;
Cb = yuv[cOff];
if (Cb < 0)
Cb += 127;
else
Cb -= 128;
Cr = yuv[cOff + 1];
if (Cr < 0)
Cr += 127;
else
Cr -= 128;
}
R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
if (R < 0)
R = 0;
else
if (R > 255)
R = 255;
G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 3)
+ (Cr >> 4) + (Cr >> 5);
if (G < 0)
G = 0;
else
if (G > 255)
G = 255;
B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
if (B < 0)
B = 0;
else
if (B > 255)
B = 255;
rgbData[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
}
}
(*env)->SetIntArrayRegion(env, rgb, 0, sz, (jint *) &rgbData[0]);
}
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
JNIEnv *env;
jclass cls;
cached_jvm = jvm; /* cache the JavaVM pointer */
__android_log_write(ANDROID_LOG_INFO, "native_tag", "Jni_OnLoad_Called");
if ((*jvm)->GetEnv(jvm, (void **) &env, JNI_VERSION_1_2))
{
return JNI_ERR; /* JNI version not supported */
}
cls = (*env)->FindClass(env, "C");
if (cls == NULL)
{
return JNI_ERR;
}
/* Use weak global ref to allow C class to be unloaded */
Class_C = (*env)->NewWeakGlobalRef(env, cls);
if (Class_C == NULL)
{
return JNI_ERR;
}
/* Compute and cache the method ID */
MID_C_g = (*env)->GetMethodID(env, cls, "g", "()V");
if (MID_C_g == NULL)
{
return JNI_ERR;
}
return JNI_VERSION_1_2;
}
我的Android.mk文件放在jni文件夹中,如下所示:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := face-detect
LOCAL_SRC_FILES := face-detect.c
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
我项目中的libs / armeabi文件夹包含libface-detect.so,您可以在此处找到http://uploading.com/files/get/28f3c9bd/libface-detect.so
我仍然不知道出了什么问题,请帮忙
答案 0 :(得分:1)
答案 1 :(得分:0)
看起来它找不到类'C'
或方法'g'
,你的Java程序中确实有这样的类吗?
通常他们有FindClass("myorg/mypackage/myclass");
答案 2 :(得分:0)
我重建了ndk并重建了java jni头文件并且程序运行了,不知道如何