The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 <ui/Surface.h> |
| 27 | #include <ui/Camera.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame^] | 28 | #include <binder/IMemory.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace android; |
| 31 | |
| 32 | enum CallbackMessageID { |
| 33 | kShutterCallback = 0, |
| 34 | kRawCallback = 1, |
| 35 | kJpegCallback = 2, |
| 36 | kPreviewCallback = 3, |
| 37 | kAutoFocusCallback = 4, |
| 38 | kErrorCallback = 5 |
| 39 | }; |
| 40 | |
| 41 | enum CameraError { |
| 42 | kCameraErrorUnknown = 1, |
| 43 | kCameraErrorMediaServer = 100 |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | struct fields_t { |
| 48 | jfieldID context; |
| 49 | jfieldID surface; |
| 50 | jmethodID post_event; |
| 51 | }; |
| 52 | |
| 53 | static fields_t fields; |
| 54 | static Mutex sLock; |
| 55 | |
| 56 | struct camera_context_t { |
| 57 | jobject mCameraJObjectWeak; // weak reference to java object |
| 58 | jclass mCameraJClass; // strong reference to java class |
| 59 | sp<Camera> mCamera; // strong reference to native object |
| 60 | }; |
| 61 | |
| 62 | sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, camera_context_t** pContext) |
| 63 | { |
| 64 | sp<Camera> camera; |
| 65 | Mutex::Autolock _l(sLock); |
| 66 | camera_context_t* context = reinterpret_cast<camera_context_t*>(env->GetIntField(thiz, fields.context)); |
| 67 | if (context != NULL) { |
| 68 | camera = context->mCamera; |
| 69 | } |
| 70 | LOGV("get_native_camera: context=%p, camera=%p", context, camera.get()); |
| 71 | if (camera == 0) { |
| 72 | jniThrowException(env, "java/lang/RuntimeException", "Method called after release()"); |
| 73 | } |
| 74 | |
| 75 | if (pContext != NULL) *pContext = context; |
| 76 | return camera; |
| 77 | } |
| 78 | |
| 79 | static void err_callback(status_t err, void *cookie) |
| 80 | { |
| 81 | camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie); |
| 82 | if ((context == NULL) || (context->mCamera == 0)) return; |
| 83 | |
| 84 | LOGV("err_callback: context=%p, camera=%p", context, context->mCamera.get()); |
| 85 | |
| 86 | int error; |
| 87 | switch (err) { |
| 88 | case DEAD_OBJECT: |
| 89 | error = kCameraErrorMediaServer; |
| 90 | break; |
| 91 | default: |
| 92 | error = kCameraErrorUnknown; |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 97 | if (env == NULL) { |
| 98 | LOGE("err_callback on dead VM"); |
| 99 | return; |
| 100 | } |
| 101 | env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event, |
| 102 | context->mCameraJObjectWeak, kErrorCallback, error, 0, NULL); |
| 103 | } |
| 104 | |
| 105 | // connect to camera service |
| 106 | static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this) |
| 107 | { |
| 108 | sp<Camera> camera = Camera::connect(); |
| 109 | |
| 110 | if (camera == NULL) { |
Wu-cheng Li | fa3e556 | 2009-05-04 19:38:43 +0800 | [diff] [blame] | 111 | jniThrowException(env, "java/lang/RuntimeException", |
| 112 | "Fail to connect to camera service"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
| 116 | // make sure camera hardware is alive |
| 117 | if (camera->getStatus() != NO_ERROR) { |
| 118 | jniThrowException(env, "java/io/IOException", "Camera initialization failed"); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | jclass clazz = env->GetObjectClass(thiz); |
| 123 | if (clazz == NULL) { |
| 124 | LOGE("Can't find android/hardware/Camera"); |
| 125 | // XXX no idea what to throw here, can this even happen? |
| 126 | jniThrowException(env, "java/lang/Exception", NULL); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | // We use a weak reference so the Camera object can be garbage collected. |
| 131 | // The reference is only used as a proxy for callbacks. |
| 132 | camera_context_t* context = new camera_context_t; |
| 133 | context->mCameraJObjectWeak = env->NewGlobalRef(weak_this); |
| 134 | context->mCameraJClass = (jclass)env->NewGlobalRef(clazz); |
| 135 | context->mCamera = camera; |
| 136 | |
| 137 | // save context in opaque field |
| 138 | env->SetIntField(thiz, fields.context, (int)context); |
| 139 | |
| 140 | LOGV("native_setup: mCameraJObjectWeak=%x, camera_obj=%x, context=%p", |
| 141 | (int)context->mCameraJObjectWeak, (int)thiz, context); |
| 142 | |
| 143 | // set error callback |
| 144 | camera->setErrorCallback(err_callback, context); |
| 145 | } |
| 146 | |
| 147 | // disconnect from camera service |
| 148 | // It's okay to call this when the native camera context is already null. |
| 149 | // This handles the case where the user has called release() and the |
| 150 | // finalizer is invoked later. |
| 151 | static void android_hardware_Camera_release(JNIEnv *env, jobject thiz) |
| 152 | { |
| 153 | camera_context_t* context = NULL; |
| 154 | sp<Camera> camera; |
| 155 | { |
| 156 | Mutex::Autolock _l(sLock); |
| 157 | context = reinterpret_cast<camera_context_t*>(env->GetIntField(thiz, fields.context)); |
| 158 | |
| 159 | // Make sure we do not attempt to callback on a deleted Java object. |
| 160 | env->SetIntField(thiz, fields.context, 0); |
| 161 | } |
| 162 | |
| 163 | // clean up if release has not been called before |
| 164 | if (context != NULL) { |
| 165 | camera = context->mCamera; |
| 166 | context->mCamera.clear(); |
| 167 | LOGV("native_release: context=%p camera=%p", context, camera.get()); |
| 168 | |
| 169 | // clear callbacks |
| 170 | if (camera != NULL) { |
| 171 | camera->setPreviewCallback(NULL, NULL, FRAME_CALLBACK_FLAG_NOOP); |
| 172 | camera->setErrorCallback(NULL, NULL); |
| 173 | camera->disconnect(); |
| 174 | env->DeleteGlobalRef(context->mCameraJObjectWeak); |
| 175 | env->DeleteGlobalRef(context->mCameraJClass); |
| 176 | } |
| 177 | |
| 178 | // remove context to prevent further Java access |
| 179 | delete context; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface) |
| 184 | { |
| 185 | LOGV("setPreviewDisplay"); |
| 186 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 187 | if (camera == 0) return; |
| 188 | |
| 189 | sp<Surface> surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface)); |
| 190 | if (camera->setPreviewDisplay(surface) != NO_ERROR) { |
| 191 | jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed"); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static void preview_callback(const sp<IMemory>& mem, void *cookie) |
| 196 | { |
| 197 | LOGV("preview_callback"); |
| 198 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 199 | if (env == NULL) { |
| 200 | LOGE("preview_callback on dead VM"); |
| 201 | return; |
| 202 | } |
| 203 | camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie); |
| 204 | if ((context == NULL) || (context->mCamera == 0)) { |
| 205 | LOGW("context or camera is NULL in preview_callback"); |
| 206 | return; |
| 207 | } |
| 208 | LOGV("native_release: context=%p camera=%p", context, context->mCamera.get()); |
| 209 | |
| 210 | int arg1 = 0, arg2 = 0; |
| 211 | jobject obj = NULL; |
| 212 | |
| 213 | ssize_t offset; |
| 214 | size_t size; |
| 215 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 216 | |
| 217 | uint8_t *data = ((uint8_t *)heap->base()) + offset; |
| 218 | |
| 219 | jbyteArray array = env->NewByteArray(size); |
| 220 | if (array == NULL) { |
| 221 | LOGE("Couldn't allocate byte array for YUV data"); |
| 222 | env->ExceptionClear(); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | jbyte *bytes = env->GetByteArrayElements(array, NULL); |
| 227 | memcpy(bytes, data, size); |
| 228 | env->ReleaseByteArrayElements(array, bytes, 0); |
| 229 | |
| 230 | obj = array; |
| 231 | |
| 232 | env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event, |
| 233 | context->mCameraJObjectWeak, kPreviewCallback, arg1, arg2, obj); |
| 234 | env->DeleteLocalRef(array); |
| 235 | } |
| 236 | |
| 237 | static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz) |
| 238 | { |
| 239 | LOGV("startPreview"); |
| 240 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 241 | if (camera == 0) return; |
| 242 | |
| 243 | if (camera->startPreview() != NO_ERROR) { |
| 244 | jniThrowException(env, "java/io/IOException", "startPreview failed"); |
| 245 | return; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz) |
| 250 | { |
| 251 | LOGV("stopPreview"); |
| 252 | sp<Camera> c = get_native_camera(env, thiz, NULL); |
| 253 | if (c == 0) return; |
| 254 | |
| 255 | c->stopPreview(); |
| 256 | } |
| 257 | |
| 258 | static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz) |
| 259 | { |
| 260 | LOGV("previewEnabled"); |
| 261 | sp<Camera> c = get_native_camera(env, thiz, NULL); |
| 262 | if (c == 0) return false; |
| 263 | |
| 264 | return c->previewEnabled(); |
| 265 | } |
| 266 | |
| 267 | static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean oneshot) |
| 268 | { |
| 269 | // Important: Only install preview_callback if the Java code has called |
| 270 | // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy |
| 271 | // each preview frame for nothing. |
| 272 | camera_context_t* context; |
| 273 | sp<Camera> camera = get_native_camera(env, thiz, &context); |
| 274 | if (camera == 0) return; |
| 275 | |
| 276 | int callback_flag; |
| 277 | if (installed) { |
| 278 | callback_flag = oneshot ? FRAME_CALLBACK_FLAG_BARCODE_SCANNER : FRAME_CALLBACK_FLAG_CAMERA; |
| 279 | } else { |
| 280 | callback_flag = FRAME_CALLBACK_FLAG_NOOP; |
| 281 | } |
| 282 | camera->setPreviewCallback(installed ? preview_callback : NULL, context, callback_flag); |
| 283 | } |
| 284 | |
| 285 | static void autofocus_callback_impl(bool success, void *cookie) |
| 286 | { |
| 287 | LOGV("autoFocusCallback"); |
| 288 | camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie); |
| 289 | |
| 290 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 291 | if (env == NULL) { |
| 292 | LOGE("autofocus_callback on dead VM"); |
| 293 | return; |
| 294 | } |
| 295 | env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event, |
| 296 | context->mCameraJObjectWeak, kAutoFocusCallback, success, 0, NULL); |
| 297 | } |
| 298 | |
| 299 | static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz) |
| 300 | { |
| 301 | LOGV("autoFocus"); |
| 302 | camera_context_t* context; |
| 303 | sp<Camera> c = get_native_camera(env, thiz, &context); |
| 304 | if (c == 0) return; |
| 305 | |
| 306 | c->setAutoFocusCallback(autofocus_callback_impl, context); |
| 307 | if (c->autoFocus() != NO_ERROR) { |
| 308 | jniThrowException(env, "java/io/IOException", "autoFocus failed"); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | static void jpeg_callback(const sp<IMemory>& mem, void *cookie) |
| 313 | { |
| 314 | LOGV("jpegCallback"); |
| 315 | camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie); |
| 316 | |
| 317 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 318 | if (env == NULL) { |
| 319 | LOGE("jpeg`_callback on dead VM"); |
| 320 | return; |
| 321 | } |
| 322 | int arg1 = 0, arg2 = 0; |
| 323 | jobject obj = NULL; |
| 324 | |
| 325 | if (mem == NULL) { |
| 326 | env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event, |
| 327 | context->mCameraJObjectWeak, kJpegCallback, arg1, arg2, NULL); |
| 328 | return; |
| 329 | } |
| 330 | ssize_t offset; |
| 331 | size_t size; |
| 332 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 333 | LOGV("jpeg_callback: mem off=%d, size=%d", offset, size); |
| 334 | |
| 335 | uint8_t *heap_base = (uint8_t *)heap->base(); |
| 336 | if (heap_base == NULL) { |
| 337 | LOGE("YUV heap is NULL"); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | uint8_t *data = heap_base + offset; |
| 342 | |
| 343 | jbyteArray array = env->NewByteArray(size); |
| 344 | if (array == NULL) { |
| 345 | LOGE("Couldn't allocate byte array for JPEG data"); |
| 346 | env->ExceptionClear(); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | jbyte *bytes = env->GetByteArrayElements(array, NULL); |
| 351 | memcpy(bytes, data, size); |
| 352 | env->ReleaseByteArrayElements(array, bytes, 0); |
| 353 | |
| 354 | obj = array; |
| 355 | |
| 356 | env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event, |
| 357 | context->mCameraJObjectWeak, kJpegCallback, arg1, arg2, obj); |
| 358 | env->DeleteLocalRef(array); |
| 359 | } |
| 360 | |
| 361 | static void shutter_callback_impl(void *cookie) |
| 362 | { |
| 363 | LOGV("shutterCallback"); |
| 364 | camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie); |
| 365 | |
| 366 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 367 | if (env == NULL) { |
| 368 | LOGE("shutter_callback on dead VM"); |
| 369 | return; |
| 370 | } |
| 371 | env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event, |
| 372 | context->mCameraJObjectWeak, kShutterCallback, 0, 0, NULL); |
| 373 | } |
| 374 | |
| 375 | static void raw_callback(const sp<IMemory>& mem __attribute__((unused)), |
| 376 | void *cookie) |
| 377 | { |
| 378 | LOGV("rawCallback"); |
| 379 | camera_context_t* context = reinterpret_cast<camera_context_t*>(cookie); |
| 380 | |
| 381 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 382 | if (env == NULL) { |
| 383 | LOGE("raw_callback on dead VM"); |
| 384 | return; |
| 385 | } |
| 386 | env->CallStaticVoidMethod(context->mCameraJClass, fields.post_event, |
| 387 | context->mCameraJObjectWeak, kRawCallback, 0, 0, NULL); |
| 388 | } |
| 389 | |
| 390 | static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz) |
| 391 | { |
| 392 | LOGV("takePicture"); |
| 393 | camera_context_t* context; |
| 394 | sp<Camera> camera = get_native_camera(env, thiz, &context); |
| 395 | if (camera == 0) return; |
| 396 | |
| 397 | camera->setShutterCallback(shutter_callback_impl, context); |
| 398 | camera->setRawCallback(raw_callback, context); |
| 399 | camera->setJpegCallback(jpeg_callback, context); |
| 400 | if (camera->takePicture() != NO_ERROR) { |
| 401 | jniThrowException(env, "java/io/IOException", "takePicture failed"); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params) |
| 409 | { |
| 410 | LOGV("setParameters"); |
| 411 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 412 | if (camera == 0) return; |
| 413 | |
| 414 | const jchar* str = env->GetStringCritical(params, 0); |
| 415 | String8 params8; |
| 416 | if (params) { |
| 417 | params8 = String8(str, env->GetStringLength(params)); |
| 418 | env->ReleaseStringCritical(params, str); |
| 419 | } |
| 420 | if (camera->setParameters(params8) != NO_ERROR) { |
| 421 | jniThrowException(env, "java/lang/IllegalArgumentException", "setParameters failed"); |
| 422 | return; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz) |
| 427 | { |
| 428 | LOGV("getParameters"); |
| 429 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 430 | if (camera == 0) return 0; |
| 431 | |
| 432 | return env->NewStringUTF(camera->getParameters().string()); |
| 433 | } |
| 434 | |
| 435 | static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz) |
| 436 | { |
| 437 | LOGV("reconnect"); |
| 438 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 439 | if (camera == 0) return; |
| 440 | |
| 441 | if (camera->reconnect() != NO_ERROR) { |
| 442 | jniThrowException(env, "java/io/IOException", "reconnect failed"); |
| 443 | return; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | static jint android_hardware_Camera_lock(JNIEnv *env, jobject thiz) |
| 448 | { |
| 449 | LOGV("lock"); |
| 450 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 451 | if (camera == 0) return INVALID_OPERATION; |
| 452 | return (jint) camera->lock(); |
| 453 | } |
| 454 | |
| 455 | static jint android_hardware_Camera_unlock(JNIEnv *env, jobject thiz) |
| 456 | { |
| 457 | LOGV("unlock"); |
| 458 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 459 | if (camera == 0) return INVALID_OPERATION; |
| 460 | return (jint) camera->unlock(); |
| 461 | } |
| 462 | |
| 463 | //------------------------------------------------- |
| 464 | |
| 465 | static JNINativeMethod camMethods[] = { |
| 466 | { "native_setup", |
| 467 | "(Ljava/lang/Object;)V", |
| 468 | (void*)android_hardware_Camera_native_setup }, |
| 469 | { "native_release", |
| 470 | "()V", |
| 471 | (void*)android_hardware_Camera_release }, |
| 472 | { "setPreviewDisplay", |
| 473 | "(Landroid/view/Surface;)V", |
| 474 | (void *)android_hardware_Camera_setPreviewDisplay }, |
| 475 | { "startPreview", |
| 476 | "()V", |
| 477 | (void *)android_hardware_Camera_startPreview }, |
| 478 | { "stopPreview", |
| 479 | "()V", |
| 480 | (void *)android_hardware_Camera_stopPreview }, |
| 481 | { "previewEnabled", |
| 482 | "()Z", |
| 483 | (void *)android_hardware_Camera_previewEnabled }, |
| 484 | { "setHasPreviewCallback", |
| 485 | "(ZZ)V", |
| 486 | (void *)android_hardware_Camera_setHasPreviewCallback }, |
| 487 | { "native_autoFocus", |
| 488 | "()V", |
| 489 | (void *)android_hardware_Camera_autoFocus }, |
| 490 | { "native_takePicture", |
| 491 | "()V", |
| 492 | (void *)android_hardware_Camera_takePicture }, |
| 493 | { "native_setParameters", |
| 494 | "(Ljava/lang/String;)V", |
| 495 | (void *)android_hardware_Camera_setParameters }, |
| 496 | { "native_getParameters", |
| 497 | "()Ljava/lang/String;", |
| 498 | (void *)android_hardware_Camera_getParameters }, |
| 499 | { "reconnect", |
| 500 | "()V", |
| 501 | (void*)android_hardware_Camera_reconnect }, |
| 502 | { "lock", |
| 503 | "()I", |
| 504 | (void*)android_hardware_Camera_lock }, |
| 505 | { "unlock", |
| 506 | "()I", |
| 507 | (void*)android_hardware_Camera_unlock }, |
| 508 | }; |
| 509 | |
| 510 | struct field { |
| 511 | const char *class_name; |
| 512 | const char *field_name; |
| 513 | const char *field_type; |
| 514 | jfieldID *jfield; |
| 515 | }; |
| 516 | |
| 517 | static int find_fields(JNIEnv *env, field *fields, int count) |
| 518 | { |
| 519 | for (int i = 0; i < count; i++) { |
| 520 | field *f = &fields[i]; |
| 521 | jclass clazz = env->FindClass(f->class_name); |
| 522 | if (clazz == NULL) { |
| 523 | LOGE("Can't find %s", f->class_name); |
| 524 | return -1; |
| 525 | } |
| 526 | |
| 527 | jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type); |
| 528 | if (field == NULL) { |
| 529 | LOGE("Can't find %s.%s", f->class_name, f->field_name); |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | *(f->jfield) = field; |
| 534 | } |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | // Get all the required offsets in java class and register native functions |
| 540 | int register_android_hardware_Camera(JNIEnv *env) |
| 541 | { |
| 542 | field fields_to_find[] = { |
| 543 | { "android/hardware/Camera", "mNativeContext", "I", &fields.context }, |
| 544 | { "android/view/Surface", "mSurface", "I", &fields.surface } |
| 545 | }; |
| 546 | |
| 547 | if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0) |
| 548 | return -1; |
| 549 | |
| 550 | jclass clazz = env->FindClass("android/hardware/Camera"); |
| 551 | fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", |
| 552 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 553 | if (fields.post_event == NULL) { |
| 554 | LOGE("Can't find android/hardware/Camera.postEventFromNative"); |
| 555 | return -1; |
| 556 | } |
| 557 | |
| 558 | |
| 559 | // Register native functions |
| 560 | return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera", |
| 561 | camMethods, NELEM(camMethods)); |
| 562 | } |
| 563 | |