在Android上本机访问相机时Camera :: open失败

时间:2014-02-11 13:01:08

标签: android android-ndk android-camera

我正在尝试使用基于AOSP的类(如Camera.h)来NDK访问摄像头,如所描述http://guoh.org/lifelog/2013/05/android-camera-architecture/

static int android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this, jint cameraId)
{
    sp<Camera> camera = Camera::connect(cameraId);
    if (camera == NULL) {
       // jniThrowRuntimeException(env, "Fail to connect to camera service");
        return 1;  // always comes here
    }
...}

logcat是:

01-02 08:19:11.870    1351-1649/? I/CameraClient﹕ Opening camera 0
01-02 08:19:13.110    1351-1649/? D/HALCameraFactory﹕ Calling process is: com.my.camndkapp
01-02 08:19:13.110    3741-3741/com.my.camndkapp W/dalvikvm﹕ JNI WARNING: instance fieldID 0x0 not valid for class Ljava/lang/Class;
01-02 08:19:13.110    3741-3741/com.my.camndkapp W/dalvikvm﹕ in Lcom/my/camndkapp/CameraGate;.initCamera:(Ljava/lang/Object;I)I (SetIntField)
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ "main" prio=5 tid=1 RUNNABLE
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ | group="main" sCount=0 dsCount=0 obj=0x40b129a0 self=0x56a5c010
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ | sysTid=3741 nice=0 sched=0/0 cgrp=apps handle=1074443228
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ | state=R schedstat=( 0 0 0 ) utm=25 stm=10 core=1
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ at com.my.camndkapp.CameraGate.initCamera(Native Method)
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ at com.my.camndkapp.CameraGate.init(CameraGate.java:34)
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ at com.my.camndkapp.FullscreenActivity.onCreate(FullscreenActivity.java:73)
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ at android.app.Activity.performCreate(Activity.java:5104)
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-02 08:19:13.110    3741-3741/com.my.camndkapp I/dalvikvm﹕ at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-02 08:19:13.120    3741-3741/com.my.camndkapp I/dalvikvm﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-02 08:19:13.120    3741-3741/com.my.camndkapp I/dalvikvm﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 08:19:13.120    3741-3741/com.my.camndkapp I/dalvikvm﹕ at android.os.Looper.loop(Looper.java:137)
01-02 08:19:13.120    3741-3741/com.my.camndkapp I/dalvikvm﹕ at android.app.ActivityThread.main(ActivityThread.java:5041)
01-02 08:19:13.120    3741-3741/com.my.camndkapp I/dalvikvm﹕ at java.lang.reflect.Method.invokeNative(Native Method)
01-02 08:19:13.120    3741-3741/com.my.camndkapp I/dalvikvm﹕ at java.lang.reflect.Method.invoke(Method.java:511)
01-02 08:19:13.120    3741-3741/com.my.camndkapp I/dalvikvm﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
01-02 08:19:13.120    3741-3741/com.my.camndkapp I/dalvikvm﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-02 08:19:13.120    3741-3741/com.my.camndkapp I/dalvikvm﹕ at dalvik.system.NativeStart.main(Native Method)
01-02 08:19:13.120    3741-3741/com.my.camndkapp I/dalvikvm﹕ [ 01-02 08:19:13.120  3741: 3741 E/dalvikvm ]
    VM aborting
01-02 08:19:13.120    3741-3741/com.my.camndkapp A/libc﹕ Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 3741 (my.camndkapp)

注1:如果我将 -lcamera.fiber.so 添加到Android.mk中的库中 - 应用程序在启动时崩溃

注2:所有权限都存在,Java的Camera.open()工作正常

注3:我想知道Camera :: connect(..)原则上如何返回NULL:

sp<Camera> Camera::connect(int cameraId)
{
    ALOGV("connect");
    sp<Camera> c = new Camera();
    const sp<ICameraService>& cs = getCameraService();
    if (cs != 0) {
        c->mCamera = cs->connect(c, cameraId);
    }
    if (c->mCamera != 0) {
        c->mCamera->asBinder()->linkToDeath(c);
        c->mStatus = NO_ERROR;
    } else {
        c.clear();
    }
    return c;
}

如果 c = new Camera()返回NULL,它将在下一行访问任何 c-&gt; var

时崩溃

注4: Android级API-17

提前谢谢你!

编辑1:同时对getNumberOfCameras()的本机调用正常工作:

static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
{
    return Camera::getNumberOfCameras();
}

0 个答案:

没有答案