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