blob: e944654a2fb3128de60fa5eb2539f85f58f0b715 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Agopian07952722009-05-19 19:08:10 -070028#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30using namespace android;
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032struct fields_t {
33 jfieldID context;
34 jfieldID surface;
35 jmethodID post_event;
36};
37
38static fields_t fields;
39static Mutex sLock;
40
Dave Sparks5e271152009-06-23 17:30:11 -070041// provides persistent context for calls from native code to Java
42class JNICameraContext: public CameraListener
43{
44public:
45 JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
46 ~JNICameraContext() { release(); }
47 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
48 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr);
Dave Sparks59c1a932009-07-08 15:56:53 -070049 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
Dave Sparks5e271152009-06-23 17:30:11 -070050 sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
51 void release();
52
53private:
54 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
55
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 jobject mCameraJObjectWeak; // weak reference to java object
57 jclass mCameraJClass; // strong reference to java class
Wu-cheng Liffe1cf22009-09-10 16:49:17 +080058 sp<Camera> mCamera; // strong reference to native object
Dave Sparks5e271152009-06-23 17:30:11 -070059 Mutex mLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060};
61
Dave Sparks5e271152009-06-23 17:30:11 -070062sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063{
64 sp<Camera> camera;
65 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -070066 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -070068 camera = context->getCamera();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 }
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
Dave Sparks5e271152009-06-23 17:30:11 -070079JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080{
Dave Sparks5e271152009-06-23 17:30:11 -070081 mCameraJObjectWeak = env->NewGlobalRef(weak_this);
82 mCameraJClass = (jclass)env->NewGlobalRef(clazz);
83 mCamera = camera;
84}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
Dave Sparks5e271152009-06-23 17:30:11 -070086void JNICameraContext::release()
87{
88 LOGV("release");
89 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparks5e271152009-06-23 17:30:11 -070091
92 if (mCameraJObjectWeak != NULL) {
93 env->DeleteGlobalRef(mCameraJObjectWeak);
94 mCameraJObjectWeak = NULL;
95 }
96 if (mCameraJClass != NULL) {
97 env->DeleteGlobalRef(mCameraJClass);
98 mCameraJClass = NULL;
99 }
100 mCamera.clear();
101}
102
103void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
104{
105 LOGV("notify");
106
107 // VM pointer will be NULL if object is released
108 Mutex::Autolock _l(mLock);
109 if (mCameraJObjectWeak == NULL) {
110 LOGW("callback on dead camera object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 return;
112 }
Dave Sparks5e271152009-06-23 17:30:11 -0700113 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700114 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Chih-Chung Chang6157de02009-09-24 15:29:35 -0700115 mCameraJObjectWeak, msgType, ext1, ext2, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700116}
117
118void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
119{
120 jbyteArray obj = NULL;
121
122 // allocate Java byte array and copy data
123 if (dataPtr != NULL) {
124 ssize_t offset;
125 size_t size;
126 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
127 LOGV("postData: off=%d, size=%d", offset, size);
128 uint8_t *heapBase = (uint8_t*)heap->base();
129
130 if (heapBase != NULL) {
Dave Sparksc4ca4202009-07-13 09:38:20 -0700131 const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
Dave Sparks5e271152009-06-23 17:30:11 -0700132 obj = env->NewByteArray(size);
133 if (obj == NULL) {
134 LOGE("Couldn't allocate byte array for JPEG data");
135 env->ExceptionClear();
136 } else {
Dave Sparksa95f4952009-07-10 18:13:36 -0700137 env->SetByteArrayRegion(obj, 0, size, data);
Dave Sparks5e271152009-06-23 17:30:11 -0700138 }
139 } else {
140 LOGE("image heap is NULL");
141 }
142 }
143
144 // post image data to Java
145 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
146 mCameraJObjectWeak, msgType, 0, 0, obj);
147 if (obj) {
148 env->DeleteLocalRef(obj);
149 }
150}
151
152void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr)
153{
154 // VM pointer will be NULL if object is released
155 Mutex::Autolock _l(mLock);
156 JNIEnv *env = AndroidRuntime::getJNIEnv();
Dave Sparksd0cbb1a2009-06-29 19:03:33 -0700157 if (mCameraJObjectWeak == NULL) {
158 LOGW("callback on dead camera object");
159 return;
160 }
Dave Sparks5e271152009-06-23 17:30:11 -0700161
162 // return data based on callback type
163 switch(msgType) {
Dave Sparks5e271152009-06-23 17:30:11 -0700164 case CAMERA_MSG_VIDEO_FRAME:
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700165 // should never happen
Dave Sparks5e271152009-06-23 17:30:11 -0700166 break;
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700167 // don't return raw data to Java
Dave Sparks5e271152009-06-23 17:30:11 -0700168 case CAMERA_MSG_RAW_IMAGE:
169 LOGV("rawCallback");
170 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700171 mCameraJObjectWeak, msgType, 0, 0, NULL);
Dave Sparks5e271152009-06-23 17:30:11 -0700172 break;
173 default:
Dave Sparksda578562009-07-21 08:43:29 -0700174 // TODO: Change to LOGV
175 LOGD("dataCallback(%d, %p)", msgType, dataPtr.get());
Dave Sparksc62f9bd2009-06-26 13:33:32 -0700176 copyAndPost(env, dataPtr, msgType);
Dave Sparks5e271152009-06-23 17:30:11 -0700177 break;
178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179}
180
Dave Sparks59c1a932009-07-08 15:56:53 -0700181void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
182{
183 // TODO: plumb up to Java. For now, just drop the timestamp
184 postData(msgType, dataPtr);
185}
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187// connect to camera service
188static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
189{
190 sp<Camera> camera = Camera::connect();
191
192 if (camera == NULL) {
Wu-cheng Lifa3e5562009-05-04 19:38:43 +0800193 jniThrowException(env, "java/lang/RuntimeException",
194 "Fail to connect to camera service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 return;
196 }
197
198 // make sure camera hardware is alive
199 if (camera->getStatus() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800200 jniThrowException(env, "java/lang/RuntimeException", "Camera initialization failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 return;
202 }
203
204 jclass clazz = env->GetObjectClass(thiz);
205 if (clazz == NULL) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800206 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/hardware/Camera");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 return;
208 }
209
210 // We use a weak reference so the Camera object can be garbage collected.
211 // The reference is only used as a proxy for callbacks.
Dave Sparks5e271152009-06-23 17:30:11 -0700212 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
213 context->incStrong(thiz);
214 camera->setListener(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
216 // save context in opaque field
Dave Sparks5e271152009-06-23 17:30:11 -0700217 env->SetIntField(thiz, fields.context, (int)context.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218}
219
220// disconnect from camera service
221// It's okay to call this when the native camera context is already null.
222// This handles the case where the user has called release() and the
223// finalizer is invoked later.
224static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
225{
Dave Sparksda578562009-07-21 08:43:29 -0700226 // TODO: Change to LOGV
227 LOGD("release camera");
Dave Sparks5e271152009-06-23 17:30:11 -0700228 JNICameraContext* context = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 sp<Camera> camera;
230 {
231 Mutex::Autolock _l(sLock);
Dave Sparks5e271152009-06-23 17:30:11 -0700232 context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233
234 // Make sure we do not attempt to callback on a deleted Java object.
235 env->SetIntField(thiz, fields.context, 0);
236 }
237
238 // clean up if release has not been called before
239 if (context != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700240 camera = context->getCamera();
241 context->release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 LOGV("native_release: context=%p camera=%p", context, camera.get());
243
244 // clear callbacks
245 if (camera != NULL) {
Dave Sparks5e271152009-06-23 17:30:11 -0700246 camera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 camera->disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
249
250 // remove context to prevent further Java access
Dave Sparks5e271152009-06-23 17:30:11 -0700251 context->decStrong(thiz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 }
253}
254
255static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
256{
257 LOGV("setPreviewDisplay");
258 sp<Camera> camera = get_native_camera(env, thiz, NULL);
259 if (camera == 0) return;
260
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800261 sp<Surface> surface = NULL;
262 if (jSurface != NULL) {
263 surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface));
264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 if (camera->setPreviewDisplay(surface) != NO_ERROR) {
266 jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed");
267 }
268}
269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
271{
272 LOGV("startPreview");
273 sp<Camera> camera = get_native_camera(env, thiz, NULL);
274 if (camera == 0) return;
275
276 if (camera->startPreview() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800277 jniThrowException(env, "java/lang/RuntimeException", "startPreview failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 return;
279 }
280}
281
282static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
283{
284 LOGV("stopPreview");
285 sp<Camera> c = get_native_camera(env, thiz, NULL);
286 if (c == 0) return;
287
288 c->stopPreview();
289}
290
291static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
292{
293 LOGV("previewEnabled");
294 sp<Camera> c = get_native_camera(env, thiz, NULL);
295 if (c == 0) return false;
296
297 return c->previewEnabled();
298}
299
300static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean oneshot)
301{
302 // Important: Only install preview_callback if the Java code has called
303 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
304 // each preview frame for nothing.
Dave Sparks5e271152009-06-23 17:30:11 -0700305 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 sp<Camera> camera = get_native_camera(env, thiz, &context);
307 if (camera == 0) return;
308
309 int callback_flag;
310 if (installed) {
311 callback_flag = oneshot ? FRAME_CALLBACK_FLAG_BARCODE_SCANNER : FRAME_CALLBACK_FLAG_CAMERA;
312 } else {
313 callback_flag = FRAME_CALLBACK_FLAG_NOOP;
314 }
Dave Sparks5e271152009-06-23 17:30:11 -0700315 camera->setPreviewCallbackFlags(callback_flag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316}
317
318static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
319{
320 LOGV("autoFocus");
Dave Sparks5e271152009-06-23 17:30:11 -0700321 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 sp<Camera> c = get_native_camera(env, thiz, &context);
323 if (c == 0) return;
324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 if (c->autoFocus() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800326 jniThrowException(env, "java/lang/RuntimeException", "autoFocus failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 }
328}
329
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800330static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
331{
332 LOGV("cancelAutoFocus");
333 JNICameraContext* context;
334 sp<Camera> c = get_native_camera(env, thiz, &context);
335 if (c == 0) return;
336
337 if (c->cancelAutoFocus() != NO_ERROR) {
338 jniThrowException(env, "java/lang/RuntimeException", "cancelAutoFocus failed");
339 }
340}
341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz)
343{
344 LOGV("takePicture");
Dave Sparks5e271152009-06-23 17:30:11 -0700345 JNICameraContext* context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 sp<Camera> camera = get_native_camera(env, thiz, &context);
347 if (camera == 0) return;
348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 if (camera->takePicture() != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800350 jniThrowException(env, "java/lang/RuntimeException", "takePicture failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 return;
352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353}
354
355static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
356{
357 LOGV("setParameters");
358 sp<Camera> camera = get_native_camera(env, thiz, NULL);
359 if (camera == 0) return;
360
361 const jchar* str = env->GetStringCritical(params, 0);
362 String8 params8;
363 if (params) {
364 params8 = String8(str, env->GetStringLength(params));
365 env->ReleaseStringCritical(params, str);
366 }
367 if (camera->setParameters(params8) != NO_ERROR) {
Wu-cheng Liba55b362009-06-10 16:55:39 +0800368 jniThrowException(env, "java/lang/RuntimeException", "setParameters failed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 return;
370 }
371}
372
373static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
374{
375 LOGV("getParameters");
376 sp<Camera> camera = get_native_camera(env, thiz, NULL);
377 if (camera == 0) return 0;
378
379 return env->NewStringUTF(camera->getParameters().string());
380}
381
382static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
383{
384 LOGV("reconnect");
385 sp<Camera> camera = get_native_camera(env, thiz, NULL);
386 if (camera == 0) return;
387
388 if (camera->reconnect() != NO_ERROR) {
389 jniThrowException(env, "java/io/IOException", "reconnect failed");
390 return;
391 }
392}
393
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800394static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395{
396 LOGV("lock");
397 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800398 if (camera == 0) return;
399
400 if (camera->lock() != NO_ERROR) {
401 jniThrowException(env, "java/lang/RuntimeException", "lock failed");
402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403}
404
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800405static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406{
407 LOGV("unlock");
408 sp<Camera> camera = get_native_camera(env, thiz, NULL);
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800409 if (camera == 0) return;
410
411 if (camera->unlock() != NO_ERROR) {
412 jniThrowException(env, "java/lang/RuntimeException", "unlock failed");
413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414}
415
416//-------------------------------------------------
417
418static JNINativeMethod camMethods[] = {
419 { "native_setup",
420 "(Ljava/lang/Object;)V",
421 (void*)android_hardware_Camera_native_setup },
422 { "native_release",
423 "()V",
424 (void*)android_hardware_Camera_release },
425 { "setPreviewDisplay",
426 "(Landroid/view/Surface;)V",
427 (void *)android_hardware_Camera_setPreviewDisplay },
428 { "startPreview",
429 "()V",
430 (void *)android_hardware_Camera_startPreview },
431 { "stopPreview",
432 "()V",
433 (void *)android_hardware_Camera_stopPreview },
434 { "previewEnabled",
435 "()Z",
436 (void *)android_hardware_Camera_previewEnabled },
437 { "setHasPreviewCallback",
438 "(ZZ)V",
439 (void *)android_hardware_Camera_setHasPreviewCallback },
440 { "native_autoFocus",
441 "()V",
442 (void *)android_hardware_Camera_autoFocus },
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800443 { "native_cancelAutoFocus",
444 "()V",
445 (void *)android_hardware_Camera_cancelAutoFocus },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 { "native_takePicture",
447 "()V",
448 (void *)android_hardware_Camera_takePicture },
449 { "native_setParameters",
450 "(Ljava/lang/String;)V",
451 (void *)android_hardware_Camera_setParameters },
452 { "native_getParameters",
453 "()Ljava/lang/String;",
454 (void *)android_hardware_Camera_getParameters },
455 { "reconnect",
456 "()V",
457 (void*)android_hardware_Camera_reconnect },
458 { "lock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800459 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 (void*)android_hardware_Camera_lock },
461 { "unlock",
Wu-cheng Liffe1cf22009-09-10 16:49:17 +0800462 "()V",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 (void*)android_hardware_Camera_unlock },
464};
465
466struct field {
467 const char *class_name;
468 const char *field_name;
469 const char *field_type;
470 jfieldID *jfield;
471};
472
473static int find_fields(JNIEnv *env, field *fields, int count)
474{
475 for (int i = 0; i < count; i++) {
476 field *f = &fields[i];
477 jclass clazz = env->FindClass(f->class_name);
478 if (clazz == NULL) {
479 LOGE("Can't find %s", f->class_name);
480 return -1;
481 }
482
483 jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
484 if (field == NULL) {
485 LOGE("Can't find %s.%s", f->class_name, f->field_name);
486 return -1;
487 }
488
489 *(f->jfield) = field;
490 }
491
492 return 0;
493}
494
495// Get all the required offsets in java class and register native functions
496int register_android_hardware_Camera(JNIEnv *env)
497{
498 field fields_to_find[] = {
499 { "android/hardware/Camera", "mNativeContext", "I", &fields.context },
500 { "android/view/Surface", "mSurface", "I", &fields.surface }
501 };
502
503 if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
504 return -1;
505
506 jclass clazz = env->FindClass("android/hardware/Camera");
507 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
508 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
509 if (fields.post_event == NULL) {
510 LOGE("Can't find android/hardware/Camera.postEventFromNative");
511 return -1;
512 }
513
514
515 // Register native functions
516 return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
517 camMethods, NELEM(camMethods));
518}
519