3 ** Copyright 2008, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "Camera-JNI"
20 #include <utils/Log.h>
24 #include "android_runtime/AndroidRuntime.h"
25 #include <android_runtime/android_graphics_SurfaceTexture.h>
26 #include <android_runtime/android_view_Surface.h>
28 #include <cutils/properties.h>
29 #include <utils/Vector.h>
31 #include <gui/GLConsumer.h>
32 #include <gui/Surface.h>
33 #include <camera/Camera.h>
34 #include <binder/IMemory.h>
36 using namespace android;
42 jfieldID canDisableShutterSound;
50 jmethodID rect_constructor;
51 jmethodID face_constructor;
54 static fields_t fields;
57 // provides persistent context for calls from native code to Java
58 class JNICameraContext: public CameraListener
61 JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera);
62 ~JNICameraContext() { release(); }
63 virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2);
64 virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr,
65 camera_frame_metadata_t *metadata);
66 virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
67 void postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata);
68 void addCallbackBuffer(JNIEnv *env, jbyteArray cbb, int msgType);
69 void setCallbackMode(JNIEnv *env, bool installed, bool manualMode);
70 sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; }
71 bool isRawImageCallbackBufferAvailable() const;
75 void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType);
76 void clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers);
77 void clearCallbackBuffers_l(JNIEnv *env);
78 jbyteArray getCallbackBuffer(JNIEnv *env, Vector<jbyteArray> *buffers, size_t bufferSize);
80 jobject mCameraJObjectWeak; // weak reference to java object
81 jclass mCameraJClass; // strong reference to java class
82 sp<Camera> mCamera; // strong reference to native object
83 jclass mFaceClass; // strong reference to Face class
84 jclass mRectClass; // strong reference to Rect class
88 * Global reference application-managed raw image buffer queue.
90 * Manual-only mode is supported for raw image callbacks, which is
91 * set whenever method addCallbackBuffer() with msgType =
92 * CAMERA_MSG_RAW_IMAGE is called; otherwise, null is returned
93 * with raw image callbacks.
95 Vector<jbyteArray> mRawImageCallbackBuffers;
98 * Application-managed preview buffer queue and the flags
99 * associated with the usage of the preview buffer callback.
101 Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[]
102 bool mManualBufferMode; // Whether to use application managed buffers.
103 bool mManualCameraCallbackSet; // Whether the callback has been set, used to
104 // reduce unnecessary calls to set the callback.
107 bool JNICameraContext::isRawImageCallbackBufferAvailable() const
109 return !mRawImageCallbackBuffers.isEmpty();
112 sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext)
115 Mutex::Autolock _l(sLock);
116 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
117 if (context != NULL) {
118 camera = context->getCamera();
120 ALOGV("get_native_camera: context=%p, camera=%p", context, camera.get());
122 jniThrowRuntimeException(env, "Method called after release()");
125 if (pContext != NULL) *pContext = context;
129 JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera)
131 mCameraJObjectWeak = env->NewGlobalRef(weak_this);
132 mCameraJClass = (jclass)env->NewGlobalRef(clazz);
135 jclass faceClazz = env->FindClass("android/hardware/Camera$Face");
136 mFaceClass = (jclass) env->NewGlobalRef(faceClazz);
138 jclass rectClazz = env->FindClass("android/graphics/Rect");
139 mRectClass = (jclass) env->NewGlobalRef(rectClazz);
141 mManualBufferMode = false;
142 mManualCameraCallbackSet = false;
145 void JNICameraContext::release()
148 Mutex::Autolock _l(mLock);
149 JNIEnv *env = AndroidRuntime::getJNIEnv();
151 if (mCameraJObjectWeak != NULL) {
152 env->DeleteGlobalRef(mCameraJObjectWeak);
153 mCameraJObjectWeak = NULL;
155 if (mCameraJClass != NULL) {
156 env->DeleteGlobalRef(mCameraJClass);
157 mCameraJClass = NULL;
159 if (mFaceClass != NULL) {
160 env->DeleteGlobalRef(mFaceClass);
163 if (mRectClass != NULL) {
164 env->DeleteGlobalRef(mRectClass);
167 clearCallbackBuffers_l(env);
171 void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2)
175 // VM pointer will be NULL if object is released
176 Mutex::Autolock _l(mLock);
177 if (mCameraJObjectWeak == NULL) {
178 ALOGW("callback on dead camera object");
181 JNIEnv *env = AndroidRuntime::getJNIEnv();
184 * If the notification or msgType is CAMERA_MSG_RAW_IMAGE_NOTIFY, change it
185 * to CAMERA_MSG_RAW_IMAGE since CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed
188 if (msgType == CAMERA_MSG_RAW_IMAGE_NOTIFY) {
189 msgType = CAMERA_MSG_RAW_IMAGE;
192 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
193 mCameraJObjectWeak, msgType, ext1, ext2, NULL);
196 jbyteArray JNICameraContext::getCallbackBuffer(
197 JNIEnv* env, Vector<jbyteArray>* buffers, size_t bufferSize)
199 jbyteArray obj = NULL;
201 // Vector access should be protected by lock in postData()
202 if (!buffers->isEmpty()) {
203 ALOGV("Using callback buffer from queue of length %d", buffers->size());
204 jbyteArray globalBuffer = buffers->itemAt(0);
205 buffers->removeAt(0);
207 obj = (jbyteArray)env->NewLocalRef(globalBuffer);
208 env->DeleteGlobalRef(globalBuffer);
211 jsize bufferLength = env->GetArrayLength(obj);
212 if ((int)bufferLength < (int)bufferSize) {
213 ALOGE("Callback buffer was too small! Expected %d bytes, but got %d bytes!",
214 bufferSize, bufferLength);
215 env->DeleteLocalRef(obj);
224 void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
226 jbyteArray obj = NULL;
228 // allocate Java byte array and copy data
229 if (dataPtr != NULL) {
232 sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
233 ALOGV("copyAndPost: off=%ld, size=%d", offset, size);
234 uint8_t *heapBase = (uint8_t*)heap->base();
236 if (heapBase != NULL) {
237 const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
239 if (msgType == CAMERA_MSG_RAW_IMAGE) {
240 obj = getCallbackBuffer(env, &mRawImageCallbackBuffers, size);
241 } else if (msgType == CAMERA_MSG_PREVIEW_FRAME && mManualBufferMode) {
242 obj = getCallbackBuffer(env, &mCallbackBuffers, size);
244 if (mCallbackBuffers.isEmpty()) {
245 ALOGV("Out of buffers, clearing callback!");
246 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
247 mManualCameraCallbackSet = false;
254 ALOGV("Allocating callback buffer");
255 obj = env->NewByteArray(size);
259 ALOGE("Couldn't allocate byte array for JPEG data");
260 env->ExceptionClear();
262 env->SetByteArrayRegion(obj, 0, size, data);
265 ALOGE("image heap is NULL");
269 // post image data to Java
270 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
271 mCameraJObjectWeak, msgType, 0, 0, obj);
273 env->DeleteLocalRef(obj);
277 void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr,
278 camera_frame_metadata_t *metadata)
280 // VM pointer will be NULL if object is released
281 Mutex::Autolock _l(mLock);
282 JNIEnv *env = AndroidRuntime::getJNIEnv();
283 if (mCameraJObjectWeak == NULL) {
284 ALOGW("callback on dead camera object");
288 int32_t dataMsgType = msgType & ~CAMERA_MSG_PREVIEW_METADATA;
290 // return data based on callback type
291 switch (dataMsgType) {
292 case CAMERA_MSG_VIDEO_FRAME:
293 // should never happen
296 // For backward-compatibility purpose, if there is no callback
297 // buffer for raw image, the callback returns null.
298 case CAMERA_MSG_RAW_IMAGE:
299 ALOGV("rawCallback");
300 if (mRawImageCallbackBuffers.isEmpty()) {
301 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
302 mCameraJObjectWeak, dataMsgType, 0, 0, NULL);
304 copyAndPost(env, dataPtr, dataMsgType);
313 ALOGV("dataCallback(%d, %p)", dataMsgType, dataPtr.get());
314 copyAndPost(env, dataPtr, dataMsgType);
318 // post frame metadata to Java
319 if (metadata && (msgType & CAMERA_MSG_PREVIEW_METADATA)) {
320 postMetadata(env, CAMERA_MSG_PREVIEW_METADATA, metadata);
324 void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr)
326 // TODO: plumb up to Java. For now, just drop the timestamp
327 postData(msgType, dataPtr, NULL);
330 void JNICameraContext::postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata)
332 jobjectArray obj = NULL;
333 obj = (jobjectArray) env->NewObjectArray(metadata->number_of_faces,
336 ALOGE("Couldn't allocate face metadata array");
340 for (int i = 0; i < metadata->number_of_faces; i++) {
341 jobject face = env->NewObject(mFaceClass, fields.face_constructor);
342 env->SetObjectArrayElement(obj, i, face);
344 jobject rect = env->NewObject(mRectClass, fields.rect_constructor);
345 env->SetIntField(rect, fields.rect_left, metadata->faces[i].rect[0]);
346 env->SetIntField(rect, fields.rect_top, metadata->faces[i].rect[1]);
347 env->SetIntField(rect, fields.rect_right, metadata->faces[i].rect[2]);
348 env->SetIntField(rect, fields.rect_bottom, metadata->faces[i].rect[3]);
350 env->SetObjectField(face, fields.face_rect, rect);
351 env->SetIntField(face, fields.face_score, metadata->faces[i].score);
353 env->DeleteLocalRef(face);
354 env->DeleteLocalRef(rect);
356 env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
357 mCameraJObjectWeak, msgType, 0, 0, obj);
358 env->DeleteLocalRef(obj);
361 void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode)
363 Mutex::Autolock _l(mLock);
364 mManualBufferMode = manualMode;
365 mManualCameraCallbackSet = false;
367 // In order to limit the over usage of binder threads, all non-manual buffer
368 // callbacks use CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now.
370 // Continuous callbacks will have the callback re-registered from handleMessage.
371 // Manual buffer mode will operate as fast as possible, relying on the finite supply
372 // of buffers for throttling.
375 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
376 clearCallbackBuffers_l(env, &mCallbackBuffers);
377 } else if (mManualBufferMode) {
378 if (!mCallbackBuffers.isEmpty()) {
379 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
380 mManualCameraCallbackSet = true;
383 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_BARCODE_SCANNER);
384 clearCallbackBuffers_l(env, &mCallbackBuffers);
388 void JNICameraContext::addCallbackBuffer(
389 JNIEnv *env, jbyteArray cbb, int msgType)
391 ALOGV("addCallbackBuffer: 0x%x", msgType);
393 Mutex::Autolock _l(mLock);
395 case CAMERA_MSG_PREVIEW_FRAME: {
396 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
397 mCallbackBuffers.push(callbackBuffer);
399 ALOGV("Adding callback buffer to queue, %d total",
400 mCallbackBuffers.size());
402 // We want to make sure the camera knows we're ready for the
403 // next frame. This may have come unset had we not had a
404 // callbackbuffer ready for it last time.
405 if (mManualBufferMode && !mManualCameraCallbackSet) {
406 mCamera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_CAMERA);
407 mManualCameraCallbackSet = true;
411 case CAMERA_MSG_RAW_IMAGE: {
412 jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb);
413 mRawImageCallbackBuffers.push(callbackBuffer);
417 jniThrowException(env,
418 "java/lang/IllegalArgumentException",
419 "Unsupported message type");
424 ALOGE("Null byte array!");
428 void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env)
430 clearCallbackBuffers_l(env, &mCallbackBuffers);
431 clearCallbackBuffers_l(env, &mRawImageCallbackBuffers);
434 void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env, Vector<jbyteArray> *buffers) {
435 ALOGV("Clearing callback buffers, %d remained", buffers->size());
436 while (!buffers->isEmpty()) {
437 env->DeleteGlobalRef(buffers->top());
442 static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz)
444 return Camera::getNumberOfCameras();
447 static void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz,
448 jint cameraId, jobject info_obj)
450 CameraInfo cameraInfo;
451 status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo);
452 if (rc != NO_ERROR) {
453 jniThrowRuntimeException(env, "Fail to get camera info");
456 env->SetIntField(info_obj, fields.facing, cameraInfo.facing);
457 env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation);
459 char value[PROPERTY_VALUE_MAX];
460 property_get("ro.camera.sound.forced", value, "0");
461 jboolean canDisableShutterSound = (strncmp(value, "0", 2) == 0);
462 env->SetBooleanField(info_obj, fields.canDisableShutterSound,
463 canDisableShutterSound);
466 // connect to camera service
467 static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz,
468 jobject weak_this, jint cameraId, jstring clientPackageName)
470 // Convert jstring to String16
471 const char16_t *rawClientName = env->GetStringChars(clientPackageName, NULL);
472 jsize rawClientNameLen = env->GetStringLength(clientPackageName);
473 String16 clientName(rawClientName, rawClientNameLen);
474 env->ReleaseStringChars(clientPackageName, rawClientName);
476 sp<Camera> camera = Camera::connect(cameraId, clientName,
477 Camera::USE_CALLING_UID);
479 if (camera == NULL) {
480 jniThrowRuntimeException(env, "Fail to connect to camera service");
484 // make sure camera hardware is alive
485 if (camera->getStatus() != NO_ERROR) {
486 jniThrowRuntimeException(env, "Camera initialization failed");
490 jclass clazz = env->GetObjectClass(thiz);
492 jniThrowRuntimeException(env, "Can't find android/hardware/Camera");
496 // We use a weak reference so the Camera object can be garbage collected.
497 // The reference is only used as a proxy for callbacks.
498 sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera);
499 context->incStrong((void*)android_hardware_Camera_native_setup);
500 camera->setListener(context);
502 // save context in opaque field
503 env->SetIntField(thiz, fields.context, (int)context.get());
506 // disconnect from camera service
507 // It's okay to call this when the native camera context is already null.
508 // This handles the case where the user has called release() and the
509 // finalizer is invoked later.
510 static void android_hardware_Camera_release(JNIEnv *env, jobject thiz)
512 // TODO: Change to ALOGV
513 ALOGV("release camera");
514 JNICameraContext* context = NULL;
517 Mutex::Autolock _l(sLock);
518 context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
520 // Make sure we do not attempt to callback on a deleted Java object.
521 env->SetIntField(thiz, fields.context, 0);
524 // clean up if release has not been called before
525 if (context != NULL) {
526 camera = context->getCamera();
528 ALOGV("native_release: context=%p camera=%p", context, camera.get());
531 if (camera != NULL) {
532 camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
533 camera->disconnect();
536 // remove context to prevent further Java access
537 context->decStrong((void*)android_hardware_Camera_native_setup);
541 static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface)
543 ALOGV("setPreviewDisplay");
544 sp<Camera> camera = get_native_camera(env, thiz, NULL);
545 if (camera == 0) return;
547 sp<IGraphicBufferProducer> gbp;
550 surface = android_view_Surface_getSurface(env, jSurface);
551 if (surface != NULL) {
552 gbp = surface->getIGraphicBufferProducer();
556 if (camera->setPreviewTarget(gbp) != NO_ERROR) {
557 jniThrowException(env, "java/io/IOException", "setPreviewTexture failed");
561 static void android_hardware_Camera_setPreviewTexture(JNIEnv *env,
562 jobject thiz, jobject jSurfaceTexture)
564 ALOGV("setPreviewTexture");
565 sp<Camera> camera = get_native_camera(env, thiz, NULL);
566 if (camera == 0) return;
568 sp<IGraphicBufferProducer> producer = NULL;
569 if (jSurfaceTexture != NULL) {
570 producer = SurfaceTexture_getProducer(env, jSurfaceTexture);
571 if (producer == NULL) {
572 jniThrowException(env, "java/lang/IllegalArgumentException",
573 "SurfaceTexture already released in setPreviewTexture");
579 if (camera->setPreviewTarget(producer) != NO_ERROR) {
580 jniThrowException(env, "java/io/IOException",
581 "setPreviewTexture failed");
585 static void android_hardware_Camera_setPreviewCallbackSurface(JNIEnv *env,
586 jobject thiz, jobject jSurface)
588 ALOGV("setPreviewCallbackSurface");
589 JNICameraContext* context;
590 sp<Camera> camera = get_native_camera(env, thiz, &context);
591 if (camera == 0) return;
593 sp<IGraphicBufferProducer> gbp;
596 surface = android_view_Surface_getSurface(env, jSurface);
597 if (surface != NULL) {
598 gbp = surface->getIGraphicBufferProducer();
601 // Clear out normal preview callbacks
602 context->setCallbackMode(env, false, false);
603 // Then set up callback surface
604 if (camera->setPreviewCallbackTarget(gbp) != NO_ERROR) {
605 jniThrowException(env, "java/io/IOException", "setPreviewCallbackTarget failed");
609 static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz)
611 ALOGV("startPreview");
612 sp<Camera> camera = get_native_camera(env, thiz, NULL);
613 if (camera == 0) return;
615 if (camera->startPreview() != NO_ERROR) {
616 jniThrowRuntimeException(env, "startPreview failed");
621 static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz)
623 ALOGV("stopPreview");
624 sp<Camera> c = get_native_camera(env, thiz, NULL);
630 static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz)
632 ALOGV("previewEnabled");
633 sp<Camera> c = get_native_camera(env, thiz, NULL);
634 if (c == 0) return false;
636 return c->previewEnabled();
639 static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer)
641 ALOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer);
642 // Important: Only install preview_callback if the Java code has called
643 // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy
644 // each preview frame for nothing.
645 JNICameraContext* context;
646 sp<Camera> camera = get_native_camera(env, thiz, &context);
647 if (camera == 0) return;
649 // setCallbackMode will take care of setting the context flags and calling
650 // camera->setPreviewCallbackFlags within a mutex for us.
651 context->setCallbackMode(env, installed, manualBuffer);
654 static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes, int msgType) {
655 ALOGV("addCallbackBuffer: 0x%x", msgType);
657 JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context));
659 if (context != NULL) {
660 context->addCallbackBuffer(env, bytes, msgType);
664 static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz)
667 JNICameraContext* context;
668 sp<Camera> c = get_native_camera(env, thiz, &context);
671 if (c->autoFocus() != NO_ERROR) {
672 jniThrowRuntimeException(env, "autoFocus failed");
676 static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz)
678 ALOGV("cancelAutoFocus");
679 JNICameraContext* context;
680 sp<Camera> c = get_native_camera(env, thiz, &context);
683 if (c->cancelAutoFocus() != NO_ERROR) {
684 jniThrowRuntimeException(env, "cancelAutoFocus failed");
688 static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz, int msgType)
690 ALOGV("takePicture");
691 JNICameraContext* context;
692 sp<Camera> camera = get_native_camera(env, thiz, &context);
693 if (camera == 0) return;
696 * When CAMERA_MSG_RAW_IMAGE is requested, if the raw image callback
697 * buffer is available, CAMERA_MSG_RAW_IMAGE is enabled to get the
698 * notification _and_ the data; otherwise, CAMERA_MSG_RAW_IMAGE_NOTIFY
699 * is enabled to receive the callback notification but no data.
701 * Note that CAMERA_MSG_RAW_IMAGE_NOTIFY is not exposed to the
704 if (msgType & CAMERA_MSG_RAW_IMAGE) {
705 ALOGV("Enable raw image callback buffer");
706 if (!context->isRawImageCallbackBufferAvailable()) {
707 ALOGV("Enable raw image notification, since no callback buffer exists");
708 msgType &= ~CAMERA_MSG_RAW_IMAGE;
709 msgType |= CAMERA_MSG_RAW_IMAGE_NOTIFY;
713 if (camera->takePicture(msgType) != NO_ERROR) {
714 jniThrowRuntimeException(env, "takePicture failed");
719 static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params)
721 ALOGV("setParameters");
722 sp<Camera> camera = get_native_camera(env, thiz, NULL);
723 if (camera == 0) return;
725 const jchar* str = env->GetStringCritical(params, 0);
728 params8 = String8(str, env->GetStringLength(params));
729 env->ReleaseStringCritical(params, str);
731 if (camera->setParameters(params8) != NO_ERROR) {
732 jniThrowRuntimeException(env, "setParameters failed");
737 static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz)
739 ALOGV("getParameters");
740 sp<Camera> camera = get_native_camera(env, thiz, NULL);
741 if (camera == 0) return 0;
743 String8 params8 = camera->getParameters();
744 if (params8.isEmpty()) {
745 jniThrowRuntimeException(env, "getParameters failed (empty parameters)");
748 return env->NewStringUTF(params8.string());
751 static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz)
754 sp<Camera> camera = get_native_camera(env, thiz, NULL);
755 if (camera == 0) return;
757 if (camera->reconnect() != NO_ERROR) {
758 jniThrowException(env, "java/io/IOException", "reconnect failed");
763 static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz)
766 sp<Camera> camera = get_native_camera(env, thiz, NULL);
767 if (camera == 0) return;
769 if (camera->lock() != NO_ERROR) {
770 jniThrowRuntimeException(env, "lock failed");
774 static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz)
777 sp<Camera> camera = get_native_camera(env, thiz, NULL);
778 if (camera == 0) return;
780 if (camera->unlock() != NO_ERROR) {
781 jniThrowRuntimeException(env, "unlock failed");
785 static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value)
787 ALOGV("startSmoothZoom");
788 sp<Camera> camera = get_native_camera(env, thiz, NULL);
789 if (camera == 0) return;
791 status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0);
792 if (rc == BAD_VALUE) {
794 sprintf(msg, "invalid zoom value=%d", value);
795 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
796 } else if (rc != NO_ERROR) {
797 jniThrowRuntimeException(env, "start smooth zoom failed");
801 static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz)
803 ALOGV("stopSmoothZoom");
804 sp<Camera> camera = get_native_camera(env, thiz, NULL);
805 if (camera == 0) return;
807 if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) {
808 jniThrowRuntimeException(env, "stop smooth zoom failed");
812 static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz,
815 ALOGV("setDisplayOrientation");
816 sp<Camera> camera = get_native_camera(env, thiz, NULL);
817 if (camera == 0) return;
819 if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) {
820 jniThrowRuntimeException(env, "set display orientation failed");
824 static jboolean android_hardware_Camera_enableShutterSound(JNIEnv *env, jobject thiz,
827 ALOGV("enableShutterSound");
828 sp<Camera> camera = get_native_camera(env, thiz, NULL);
829 if (camera == 0) return JNI_FALSE;
831 int32_t value = (enabled == JNI_TRUE) ? 1 : 0;
832 status_t rc = camera->sendCommand(CAMERA_CMD_ENABLE_SHUTTER_SOUND, value, 0);
833 if (rc == NO_ERROR) {
835 } else if (rc == PERMISSION_DENIED) {
838 jniThrowRuntimeException(env, "enable shutter sound failed");
843 static void android_hardware_Camera_startFaceDetection(JNIEnv *env, jobject thiz,
846 ALOGV("startFaceDetection");
847 JNICameraContext* context;
848 sp<Camera> camera = get_native_camera(env, thiz, &context);
849 if (camera == 0) return;
851 status_t rc = camera->sendCommand(CAMERA_CMD_START_FACE_DETECTION, type, 0);
852 if (rc == BAD_VALUE) {
854 snprintf(msg, sizeof(msg), "invalid face detection type=%d", type);
855 jniThrowException(env, "java/lang/IllegalArgumentException", msg);
856 } else if (rc != NO_ERROR) {
857 jniThrowRuntimeException(env, "start face detection failed");
861 static void android_hardware_Camera_stopFaceDetection(JNIEnv *env, jobject thiz)
863 ALOGV("stopFaceDetection");
864 sp<Camera> camera = get_native_camera(env, thiz, NULL);
865 if (camera == 0) return;
867 if (camera->sendCommand(CAMERA_CMD_STOP_FACE_DETECTION, 0, 0) != NO_ERROR) {
868 jniThrowRuntimeException(env, "stop face detection failed");
872 static void android_hardware_Camera_enableFocusMoveCallback(JNIEnv *env, jobject thiz, jint enable)
874 ALOGV("enableFocusMoveCallback");
875 sp<Camera> camera = get_native_camera(env, thiz, NULL);
876 if (camera == 0) return;
878 if (camera->sendCommand(CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG, enable, 0) != NO_ERROR) {
879 jniThrowRuntimeException(env, "enable focus move callback failed");
883 //-------------------------------------------------
885 static JNINativeMethod camMethods[] = {
886 { "getNumberOfCameras",
888 (void *)android_hardware_Camera_getNumberOfCameras },
890 "(ILandroid/hardware/Camera$CameraInfo;)V",
891 (void*)android_hardware_Camera_getCameraInfo },
893 "(Ljava/lang/Object;ILjava/lang/String;)V",
894 (void*)android_hardware_Camera_native_setup },
897 (void*)android_hardware_Camera_release },
898 { "setPreviewDisplay",
899 "(Landroid/view/Surface;)V",
900 (void *)android_hardware_Camera_setPreviewDisplay },
901 { "setPreviewTexture",
902 "(Landroid/graphics/SurfaceTexture;)V",
903 (void *)android_hardware_Camera_setPreviewTexture },
904 { "setPreviewCallbackSurface",
905 "(Landroid/view/Surface;)V",
906 (void *)android_hardware_Camera_setPreviewCallbackSurface },
909 (void *)android_hardware_Camera_startPreview },
912 (void *)android_hardware_Camera_stopPreview },
915 (void *)android_hardware_Camera_previewEnabled },
916 { "setHasPreviewCallback",
918 (void *)android_hardware_Camera_setHasPreviewCallback },
919 { "_addCallbackBuffer",
921 (void *)android_hardware_Camera_addCallbackBuffer },
922 { "native_autoFocus",
924 (void *)android_hardware_Camera_autoFocus },
925 { "native_cancelAutoFocus",
927 (void *)android_hardware_Camera_cancelAutoFocus },
928 { "native_takePicture",
930 (void *)android_hardware_Camera_takePicture },
931 { "native_setParameters",
932 "(Ljava/lang/String;)V",
933 (void *)android_hardware_Camera_setParameters },
934 { "native_getParameters",
935 "()Ljava/lang/String;",
936 (void *)android_hardware_Camera_getParameters },
939 (void*)android_hardware_Camera_reconnect },
942 (void*)android_hardware_Camera_lock },
945 (void*)android_hardware_Camera_unlock },
948 (void *)android_hardware_Camera_startSmoothZoom },
951 (void *)android_hardware_Camera_stopSmoothZoom },
952 { "setDisplayOrientation",
954 (void *)android_hardware_Camera_setDisplayOrientation },
955 { "_enableShutterSound",
957 (void *)android_hardware_Camera_enableShutterSound },
958 { "_startFaceDetection",
960 (void *)android_hardware_Camera_startFaceDetection },
961 { "_stopFaceDetection",
963 (void *)android_hardware_Camera_stopFaceDetection},
964 { "enableFocusMoveCallback",
966 (void *)android_hardware_Camera_enableFocusMoveCallback},
970 const char *class_name;
971 const char *field_name;
972 const char *field_type;
976 static int find_fields(JNIEnv *env, field *fields, int count)
978 for (int i = 0; i < count; i++) {
979 field *f = &fields[i];
980 jclass clazz = env->FindClass(f->class_name);
982 ALOGE("Can't find %s", f->class_name);
986 jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type);
988 ALOGE("Can't find %s.%s", f->class_name, f->field_name);
992 *(f->jfield) = field;
998 // Get all the required offsets in java class and register native functions
999 int register_android_hardware_Camera(JNIEnv *env)
1001 field fields_to_find[] = {
1002 { "android/hardware/Camera", "mNativeContext", "I", &fields.context },
1003 { "android/hardware/Camera$CameraInfo", "facing", "I", &fields.facing },
1004 { "android/hardware/Camera$CameraInfo", "orientation", "I", &fields.orientation },
1005 { "android/hardware/Camera$CameraInfo", "canDisableShutterSound", "Z",
1006 &fields.canDisableShutterSound },
1007 { "android/hardware/Camera$Face", "rect", "Landroid/graphics/Rect;", &fields.face_rect },
1008 { "android/hardware/Camera$Face", "score", "I", &fields.face_score },
1009 { "android/graphics/Rect", "left", "I", &fields.rect_left },
1010 { "android/graphics/Rect", "top", "I", &fields.rect_top },
1011 { "android/graphics/Rect", "right", "I", &fields.rect_right },
1012 { "android/graphics/Rect", "bottom", "I", &fields.rect_bottom },
1015 if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0)
1018 jclass clazz = env->FindClass("android/hardware/Camera");
1019 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
1020 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
1021 if (fields.post_event == NULL) {
1022 ALOGE("Can't find android/hardware/Camera.postEventFromNative");
1026 clazz = env->FindClass("android/graphics/Rect");
1027 fields.rect_constructor = env->GetMethodID(clazz, "<init>", "()V");
1028 if (fields.rect_constructor == NULL) {
1029 ALOGE("Can't find android/graphics/Rect.Rect()");
1033 clazz = env->FindClass("android/hardware/Camera$Face");
1034 fields.face_constructor = env->GetMethodID(clazz, "<init>", "()V");
1035 if (fields.face_constructor == NULL) {
1036 ALOGE("Can't find android/hardware/Camera$Face.Face()");
1040 // Register native functions
1041 return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",
1042 camMethods, NELEM(camMethods));