Home | History | Annotate | Download | only in jni
      1 /*
      2 **
      3 ** Copyright 2008, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 //#define LOG_NDEBUG 0
     19 #define LOG_TAG "Camera-JNI"
     20 #include <utils/Log.h>
     21 
     22 #include "jni.h"
     23 #include "JNIHelp.h"
     24 #include "android_runtime/AndroidRuntime.h"
     25 
     26 #include <utils/Vector.h>
     27 
     28 #include <surfaceflinger/Surface.h>
     29 #include <camera/Camera.h>
     30 #include <binder/IMemory.h>
     31 
     32 using namespace android;
     33 
     34 struct fields_t {
     35     jfieldID    context;
     36     jfieldID    surface;
     37     jmethodID   post_event;
     38 };
     39 
     40 static fields_t fields;
     41 static Mutex sLock;
     42 
     43 // provides persistent context for calls from native code to Java
     44 class JNICameraContext: public CameraListener
     45 {
     46 public:
     47     JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
     48     ~JNICameraContext() { release(); }
     49     virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
     50     virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr);
     51     virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
     52     void addCallbackBuffer(JNIEnv *env, jbyteArray cbb);
     53     void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
     54     sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
     55     void release();
     56 
     57 private:
     58     void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
     59     void clearCallbackBuffers_l(JNIEnv *env);
     60 
     61     jobject     mCameraJObjectWeak;     // weak reference to java object
     62     jclass      mCameraJClass;          // strong reference to java class
     63     sp<Camera>  mCamera;                // strong reference to native object
     64     Mutex       mLock;
     65 
     66     Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
     67     bool mManualBufferMode;              // Whether to use application managed buffers.
     68     bool mManualCameraCallbackSet;       // Whether the callback has been set, used to reduce unnecessary calls to set the callback.
     69 };
     70 
     71 sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
     72 {
     73     sp<Camera> camera;
     74     Mutex::Autolock _l(sLock);
     75     JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
     76     if (context != NULL) {
     77         camera = context->getCamera();
     78     }
     79     LOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
     80     if (camera == 0) {
     81         jniThrowException(env, "java/lang/RuntimeException", "Method called after release()");
     82     }
     83 
     84     if (pContext != NULL) *pContext = context;
     85     return camera;
     86 }
     87 
     88 JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
     89 {
     90     mCameraJObjectWeak = env->NewGlobalRef(weak_this);
     91     mCameraJClass = (jclass)env->NewGlobalRef(clazz);
     92     mCamera = camera;
     93 
     94     mManualBufferMode = false;
     95     mManualCameraCallbackSet = false;
     96 }
     97 
     98 void JNICameraContext::release()
     99 {
    100     LOGV("release");
    101     Mutex::Autolock _l(mLock);
    102     JNIEnv *env = AndroidRuntime::getJNIEnv();
    103 
    104     if (mCameraJObjectWeak != NULL) {
    105         env->DeleteGlobalRef(mCameraJObjectWeak);
    106         mCameraJObjectWeak = NULL;
    107     }
    108     if (mCameraJClass != NULL) {
    109         env->DeleteGlobalRef(mCameraJClass);
    110         mCameraJClass = NULL;
    111     }
    112     clearCallbackBuffers_l(env);
    113     mCamera.clear();
    114 }
    115 
    116 void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
    117 {
    118     LOGV("notify");
    119 
    120     // VM pointer will be NULL if object is released
    121     Mutex::Autolock _l(mLock);
    122     if (mCameraJObjectWeak == NULL) {
    123         LOGW("callback on dead camera object");
    124         return;
    125     }
    126     JNIEnv *env = AndroidRuntime::getJNIEnv();
    127     env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
    128             mCameraJObjectWeak, msgType, ext1, ext2, NULL);
    129 }
    130 
    131 void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
    132 {
    133     jbyteArray obj = NULL;
    134 
    135     // allocate Java byte array and copy data
    136     if (dataPtr != NULL) {
    137         ssize_t offset;
    138         size_t size;
    139         sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
    140         LOGV("postData: off=%d, size=%d", offset, size);
    141         uint8_t *heapBase = (uint8_t*)heap->base();
    142 
    143         if (heapBase != NULL) {
    144             const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
    145 
    146             if (!mManualBufferMode) {
    147                 LOGV("Allocating callback buffer");
    148                 obj = env->NewByteArray(size);
    149             } else {
    150                 // Vector access should be protected by lock in postData()
    151                 if(!mCallbackBuffers.isEmpty()) {
    152                     LOGV("Using callback buffer from queue of length %d", mCallbackBuffers.size());
    153                     jbyteArray globalBuffer = mCallbackBuffers.itemAt(0);
    154                     mCallbackBuffers.removeAt(0);
    155 
    156                     obj = (jbyteArray)env->NewLocalRef(globalBuffer);
    157                     env->DeleteGlobalRef(globalBuffer);
    158 
    159                     if (obj != NULL) {
    160                         jsize bufferLength = env->GetArrayLength(obj);
    161                         if ((int)bufferLength < (int)size) {
    162                             LOGE("Manually set buffer was too small! Expected %d bytes, but got %d!",
    163                                  size, bufferLength);
    164                             env->DeleteLocalRef(obj);
    165                             return;
    166                         }
    167                     }
    168                 }
    169 
    170                 if(mCallbackBuffers.isEmpty()) {
    171                     LOGV("Out of buffers, clearing callback!");
    172                     mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
    173                     mManualCameraCallbackSet = false;
    174 
    175                     if (obj == NULL) {
    176                         return;
    177                     }
    178                 }
    179             }
    180 
    181             if (obj == NULL) {
    182                 LOGE("Couldn't allocate byte array for JPEG data");
    183                 env->ExceptionClear();
    184             } else {
    185                 env->SetByteArrayRegion(obj, 0, size, data);
    186             }
    187         } else {
    188             LOGE("image heap is NULL");
    189         }
    190     }
    191 
    192     // post image data to Java
    193     env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
    194             mCameraJObjectWeak, msgType, 0, 0, obj);
    195     if (obj) {
    196         env->DeleteLocalRef(obj);
    197     }
    198 }
    199 
    200 void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr)
    201 {
    202     // VM pointer will be NULL if object is released
    203     Mutex::Autolock _l(mLock);
    204     JNIEnv *env = AndroidRuntime::getJNIEnv();
    205     if (mCameraJObjectWeak == NULL) {
    206         LOGW("callback on dead camera object");
    207         return;
    208     }
    209 
    210     // return data based on callback type
    211     switch(msgType) {
    212     case CAMERA_MSG_VIDEO_FRAME:
    213         // should never happen
    214         break;
    215     // don't return raw data to Java
    216     case CAMERA_MSG_RAW_IMAGE:
    217         LOGV("rawCallback");
    218         env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
    219                 mCameraJObjectWeak, msgType, 0, 0, NULL);
    220         break;
    221     default:
    222         // TODO: Change to LOGV
    223         LOGV("dataCallback(%d, %p)", msgType, dataPtr.get());
    224         copyAndPost(env, dataPtr, msgType);
    225         break;
    226     }
    227 }
    228 
    229 void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
    230 {
    231     // TODO: plumb up to Java. For now, just drop the timestamp
    232     postData(msgType, dataPtr);
    233 }
    234 
    235 void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
    236 {
    237     Mutex::Autolock _l(mLock);
    238     mManualBufferMode = manualMode;
    239     mManualCameraCallbackSet = false;
    240 
    241     // In order to limit the over usage of binder threads, all non-manual buffer
    242     // callbacks use FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
    243     //
    244     // Continuous callbacks will have the callback re-registered from handleMessage.
    245     // Manual buffer mode will operate as fast as possible, relying on the finite supply
    246     // of buffers for throttling.
    247 
    248     if (!installed) {
    249         mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
    250         clearCallbackBuffers_l(env);
    251     } else if (mManualBufferMode) {
    252         if (!mCallbackBuffers.isEmpty()) {
    253             mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_CAMERA);
    254             mManualCameraCallbackSet = true;
    255         }
    256     } else {
    257         mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
    258         clearCallbackBuffers_l(env);
    259     }
    260 }
    261 
    262 void JNICameraContext::addCallbackBuffer(JNIEnv *env, jbyteArray cbb)
    263 {
    264     if (cbb != NULL) {
    265         Mutex::Autolock _l(mLock);
    266         jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
    267         mCallbackBuffers.push(cbb);
    268 
    269         LOGV("Adding callback buffer to queue, %d total", mCallbackBuffers.size());
    270 
    271         // We want to make sure the camera knows we're ready for the next frame.
    272         // This may have come unset had we not had a callbackbuffer ready for it last time.
    273         if (mManualBufferMode && !mManualCameraCallbackSet) {
    274             mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_CAMERA);
    275             mManualCameraCallbackSet = true;
    276         }
    277     } else {
    278        LOGE("Null byte array!");
    279     }
    280 }
    281 
    282 void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
    283 {
    284     LOGV("Clearing callback buffers, %d remained", mCallbackBuffers.size());
    285     while(!mCallbackBuffers.isEmpty()) {
    286         env->DeleteGlobalRef(mCallbackBuffers.top());
    287         mCallbackBuffers.pop();
    288     }
    289 }
    290 
    291 // connect to camera service
    292 static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
    293 {
    294     sp<Camera> camera = Camera::connect();
    295 
    296     if (camera == NULL) {
    297         jniThrowException(env, "java/lang/RuntimeException",
    298                           "Fail to connect to camera service");
    299         return;
    300     }
    301 
    302     // make sure camera hardware is alive
    303     if (camera->getStatus() != NO_ERROR) {
    304         jniThrowException(env, "java/lang/RuntimeException", "Camera initialization failed");
    305         return;
    306     }
    307 
    308     jclass clazz = env->GetObjectClass(thiz);
    309     if (clazz == NULL) {
    310         jniThrowException(env, "java/lang/RuntimeException", "Can't find android/hardware/Camera");
    311         return;
    312     }
    313 
    314     // We use a weak reference so the Camera object can be garbage collected.
    315     // The reference is only used as a proxy for callbacks.
    316     sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
    317     context->incStrong(thiz);
    318     camera->setListener(context);
    319 
    320     // save context in opaque field
    321     env->SetIntField(thiz, fields.context, (int)context.get());
    322 }
    323 
    324 // disconnect from camera service
    325 // It's okay to call this when the native camera context is already null.
    326 // This handles the case where the user has called release() and the
    327 // finalizer is invoked later.
    328 static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
    329 {
    330     // TODO: Change to LOGV
    331     LOGV("release camera");
    332     JNICameraContext* context = NULL;
    333     sp<Camera> camera;
    334     {
    335         Mutex::Autolock _l(sLock);
    336         context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
    337 
    338         // Make sure we do not attempt to callback on a deleted Java object.
    339         env->SetIntField(thiz, fields.context, 0);
    340     }
    341 
    342     // clean up if release has not been called before
    343     if (context != NULL) {
    344         camera = context->getCamera();
    345         context->release();
    346         LOGV("native_release: context=%p camera=%p", context, camera.get());
    347 
    348         // clear callbacks
    349         if (camera != NULL) {
    350             camera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
    351             camera->disconnect();
    352         }
    353 
    354         // remove context to prevent further Java access
    355         context->decStrong(thiz);
    356     }
    357 }
    358 
    359 static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
    360 {
    361     LOGV("setPreviewDisplay");
    362     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    363     if (camera == 0) return;
    364 
    365     sp<Surface> surface = NULL;
    366     if (jSurface != NULL) {
    367         surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface));
    368     }
    369     if (camera->setPreviewDisplay(surface) != NO_ERROR) {
    370         jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed");
    371     }
    372 }
    373 
    374 static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
    375 {
    376     LOGV("startPreview");
    377     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    378     if (camera == 0) return;
    379 
    380     if (camera->startPreview() != NO_ERROR) {
    381         jniThrowException(env, "java/lang/RuntimeException", "startPreview failed");
    382         return;
    383     }
    384 }
    385 
    386 static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
    387 {
    388     LOGV("stopPreview");
    389     sp<Camera> c = get_native_camera(env, thiz, NULL);
    390     if (c == 0) return;
    391 
    392     c->stopPreview();
    393 }
    394 
    395 static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
    396 {
    397     LOGV("previewEnabled");
    398     sp<Camera> c = get_native_camera(env, thiz, NULL);
    399     if (c == 0) return false;
    400 
    401     return c->previewEnabled();
    402 }
    403 
    404 static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
    405 {
    406     LOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
    407     // Important: Only install preview_callback if the Java code has called
    408     // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
    409     // each preview frame for nothing.
    410     JNICameraContext* context;
    411     sp<Camera> camera = get_native_camera(env, thiz, &context);
    412     if (camera == 0) return;
    413 
    414     // setCallbackMode will take care of setting the context flags and calling
    415     // camera->setPreviewCallbackFlags within a mutex for us.
    416     context->setCallbackMode(env, installed, manualBuffer);
    417 }
    418 
    419 static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes) {
    420     LOGV("addCallbackBuffer");
    421 
    422     JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
    423 
    424     if (context != NULL) {
    425         context->addCallbackBuffer(env, bytes);
    426     }
    427 }
    428 
    429 static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
    430 {
    431     LOGV("autoFocus");
    432     JNICameraContext* context;
    433     sp<Camera> c = get_native_camera(env, thiz, &context);
    434     if (c == 0) return;
    435 
    436     if (c->autoFocus() != NO_ERROR) {
    437         jniThrowException(env, "java/lang/RuntimeException", "autoFocus failed");
    438     }
    439 }
    440 
    441 static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
    442 {
    443     LOGV("cancelAutoFocus");
    444     JNICameraContext* context;
    445     sp<Camera> c = get_native_camera(env, thiz, &context);
    446     if (c == 0) return;
    447 
    448     if (c->cancelAutoFocus() != NO_ERROR) {
    449         jniThrowException(env, "java/lang/RuntimeException", "cancelAutoFocus failed");
    450     }
    451 }
    452 
    453 static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz)
    454 {
    455     LOGV("takePicture");
    456     JNICameraContext* context;
    457     sp<Camera> camera = get_native_camera(env, thiz, &context);
    458     if (camera == 0) return;
    459 
    460     if (camera->takePicture() != NO_ERROR) {
    461         jniThrowException(env, "java/lang/RuntimeException", "takePicture failed");
    462         return;
    463     }
    464 }
    465 
    466 static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
    467 {
    468     LOGV("setParameters");
    469     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    470     if (camera == 0) return;
    471 
    472     const jchar* str = env->GetStringCritical(params, 0);
    473     String8 params8;
    474     if (params) {
    475         params8 = String8(str, env->GetStringLength(params));
    476         env->ReleaseStringCritical(params, str);
    477     }
    478     if (camera->setParameters(params8) != NO_ERROR) {
    479         jniThrowException(env, "java/lang/RuntimeException", "setParameters failed");
    480         return;
    481     }
    482 }
    483 
    484 static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
    485 {
    486     LOGV("getParameters");
    487     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    488     if (camera == 0) return 0;
    489 
    490     return env->NewStringUTF(camera->getParameters().string());
    491 }
    492 
    493 static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
    494 {
    495     LOGV("reconnect");
    496     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    497     if (camera == 0) return;
    498 
    499     if (camera->reconnect() != NO_ERROR) {
    500         jniThrowException(env, "java/io/IOException", "reconnect failed");
    501         return;
    502     }
    503 }
    504 
    505 static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
    506 {
    507     LOGV("lock");
    508     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    509     if (camera == 0) return;
    510 
    511     if (camera->lock() != NO_ERROR) {
    512         jniThrowException(env, "java/lang/RuntimeException", "lock failed");
    513     }
    514 }
    515 
    516 static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
    517 {
    518     LOGV("unlock");
    519     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    520     if (camera == 0) return;
    521 
    522     if (camera->unlock() != NO_ERROR) {
    523         jniThrowException(env, "java/lang/RuntimeException", "unlock failed");
    524     }
    525 }
    526 
    527 static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
    528 {
    529     LOGV("startSmoothZoom");
    530     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    531     if (camera == 0) return;
    532 
    533     status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
    534     if (rc == BAD_VALUE) {
    535         char msg[64];
    536         sprintf(msg, "invalid zoom value=%d", value);
    537         jniThrowException(env, "java/lang/IllegalArgumentException", msg);
    538     } else if (rc != NO_ERROR) {
    539         jniThrowException(env, "java/lang/RuntimeException", "start smooth zoom failed");
    540     }
    541 }
    542 
    543 static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
    544 {
    545     LOGV("stopSmoothZoom");
    546     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    547     if (camera == 0) return;
    548 
    549     if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
    550         jniThrowException(env, "java/lang/RuntimeException", "stop smooth zoom failed");
    551     }
    552 }
    553 
    554 static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
    555         jint value)
    556 {
    557     LOGV("setDisplayOrientation");
    558     sp<Camera> camera = get_native_camera(env, thiz, NULL);
    559     if (camera == 0) return;
    560 
    561     if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
    562         jniThrowException(env, "java/lang/RuntimeException", "set display orientation failed");
    563     }
    564 }
    565 
    566 //-------------------------------------------------
    567 
    568 static JNINativeMethod camMethods[] = {
    569   { "native_setup",
    570     "(Ljava/lang/Object;)V",
    571     (void*)android_hardware_Camera_native_setup },
    572   { "native_release",
    573     "()V",
    574     (void*)android_hardware_Camera_release },
    575   { "setPreviewDisplay",
    576     "(Landroid/view/Surface;)V",
    577     (void *)android_hardware_Camera_setPreviewDisplay },
    578   { "startPreview",
    579     "()V",
    580     (void *)android_hardware_Camera_startPreview },
    581   { "stopPreview",
    582     "()V",
    583     (void *)android_hardware_Camera_stopPreview },
    584   { "previewEnabled",
    585     "()Z",
    586     (void *)android_hardware_Camera_previewEnabled },
    587   { "setHasPreviewCallback",
    588     "(ZZ)V",
    589     (void *)android_hardware_Camera_setHasPreviewCallback },
    590   { "addCallbackBuffer",
    591     "([B)V",
    592     (void *)android_hardware_Camera_addCallbackBuffer },
    593   { "native_autoFocus",
    594     "()V",
    595     (void *)android_hardware_Camera_autoFocus },
    596   { "native_cancelAutoFocus",
    597     "()V",
    598     (void *)android_hardware_Camera_cancelAutoFocus },
    599   { "native_takePicture",
    600     "()V",
    601     (void *)android_hardware_Camera_takePicture },
    602   { "native_setParameters",
    603     "(Ljava/lang/String;)V",
    604     (void *)android_hardware_Camera_setParameters },
    605   { "native_getParameters",
    606     "()Ljava/lang/String;",
    607     (void *)android_hardware_Camera_getParameters },
    608   { "reconnect",
    609     "()V",
    610     (void*)android_hardware_Camera_reconnect },
    611   { "lock",
    612     "()V",
    613     (void*)android_hardware_Camera_lock },
    614   { "unlock",
    615     "()V",
    616     (void*)android_hardware_Camera_unlock },
    617   { "startSmoothZoom",
    618     "(I)V",
    619     (void *)android_hardware_Camera_startSmoothZoom },
    620   { "stopSmoothZoom",
    621     "()V",
    622     (void *)android_hardware_Camera_stopSmoothZoom },
    623   { "setDisplayOrientation",
    624     "(I)V",
    625     (void *)android_hardware_Camera_setDisplayOrientation },
    626 };
    627 
    628 struct field {
    629     const char *class_name;
    630     const char *field_name;
    631     const char *field_type;
    632     jfieldID   *jfield;
    633 };
    634 
    635 static int find_fields(JNIEnv *env, field *fields, int count)
    636 {
    637     for (int i = 0; i < count; i++) {
    638         field *f = &fields[i];
    639         jclass clazz = env->FindClass(f->class_name);
    640         if (clazz == NULL) {
    641             LOGE("Can't find %s", f->class_name);
    642             return -1;
    643         }
    644 
    645         jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
    646         if (field == NULL) {
    647             LOGE("Can't find %s.%s", f->class_name, f->field_name);
    648             return -1;
    649         }
    650 
    651         *(f->jfield) = field;
    652     }
    653 
    654     return 0;
    655 }
    656 
    657 // Get all the required offsets in java class and register native functions
    658 int register_android_hardware_Camera(JNIEnv *env)
    659 {
    660     field fields_to_find[] = {
    661         { "android/hardware/Camera", "mNativeContext",   "I", &fields.context },
    662         { "android/view/Surface",    "mSurface",         "I", &fields.surface }
    663     };
    664 
    665     if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
    666         return -1;
    667 
    668     jclass clazz = env->FindClass("android/hardware/Camera");
    669     fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
    670                                                "(Ljava/lang/Object;IIILjava/lang/Object;)V");
    671     if (fields.post_event == NULL) {
    672         LOGE("Can't find android/hardware/Camera.postEventFromNative");
    673         return -1;
    674     }
    675 
    676 
    677     // Register native functions
    678     return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
    679                                               camMethods, NELEM(camMethods));
    680 }
    681 
    682