From fd6f39e147dc077139a41b9c2a4039fbaa20224e Mon Sep 17 00:00:00 2001 From: Jamie Gennis Date: Mon, 20 Dec 2010 12:15:00 -0800 Subject: [PATCH] Enable camera preview to a SurfaceTexture. This change adds a public Java API to use a SurfaceTexture as the destination of camera preview frames. Change-Id: If537fed2df12c5c181e2af5f817985c1bda853fb --- api/current.xml | 13 +++++++++++ core/java/android/hardware/Camera.java | 30 ++++++++++++++++++++++++-- core/jni/android_hardware_Camera.cpp | 26 +++++++++++++++++++++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/api/current.xml b/api/current.xml index 15d16d477f675..5307cd9f3f9e4 100644 --- a/api/current.xml +++ b/api/current.xml @@ -90535,6 +90535,19 @@ + + + + The {@link SurfaceHolder} must already contain a surface when this * method is called. If you are using {@link android.view.SurfaceView}, @@ -356,6 +359,29 @@ public class Camera { private native final void setPreviewDisplay(Surface surface); + /** + * Sets the {@link SurfaceTexture} to be used for live preview. + * Either a surface or surface texture is necessary for preview, and + * preview is necessary to take pictures. The same surface texture can be + * re-set without harm. Setting a preview surface texture will un-set any + * preview surface that was set via {@link #setPreviewDisplay}. + * + *

This method must be called before {@link #startPreview()}. The + * one exception is that if the preview surface texture is not set (or set + * to null) before startPreview() is called, then this method may be called + * once with a non-null parameter to set the preview surface. (This allows + * camera setup and surface creation to happen in parallel, saving time.) + * The preview surface texture may not otherwise change while preview is + * running. + * + * @param surfaceTexture the {@link SurfaceTexture} to which the preview + * images are to be sent or null to remove the current preview surface + * texture + * @throws IOException if the method fails (for example, if the surface + * texture is unavailable or unsuitable). + */ + public native final void setPreviewTexture(SurfaceTexture surfaceTexture); + /** * Callback interface used to deliver copies of preview frames as * they are displayed. diff --git a/core/jni/android_hardware_Camera.cpp b/core/jni/android_hardware_Camera.cpp index 10fe58357d0d0..9f70509f43c01 100644 --- a/core/jni/android_hardware_Camera.cpp +++ b/core/jni/android_hardware_Camera.cpp @@ -25,6 +25,7 @@ #include +#include #include #include #include @@ -34,6 +35,7 @@ using namespace android; struct fields_t { jfieldID context; jfieldID surface; + jfieldID surfaceTexture; jfieldID facing; jfieldID orientation; jmethodID post_event; @@ -393,6 +395,24 @@ static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, } } +static void android_hardware_Camera_setPreviewTexture(JNIEnv *env, + jobject thiz, jobject jSurfaceTexture) +{ + LOGV("setPreviewTexture"); + sp camera = get_native_camera(env, thiz, NULL); + if (camera == 0) return; + + sp surfaceTexture = NULL; + if (jSurfaceTexture != NULL) { + surfaceTexture = reinterpret_cast(env->GetIntField( + jSurfaceTexture, fields.surfaceTexture)); + } + if (camera->setPreviewTexture(surfaceTexture) != NO_ERROR) { + jniThrowException(env, "java/io/IOException", + "setPreviewTexture failed"); + } +} + static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz) { LOGV("startPreview"); @@ -603,6 +623,9 @@ static JNINativeMethod camMethods[] = { { "setPreviewDisplay", "(Landroid/view/Surface;)V", (void *)android_hardware_Camera_setPreviewDisplay }, + { "setPreviewTexture", + "(Landroid/graphics/SurfaceTexture;)V", + (void *)android_hardware_Camera_setPreviewTexture }, { "startPreview", "()V", (void *)android_hardware_Camera_startPreview }, @@ -688,6 +711,8 @@ int register_android_hardware_Camera(JNIEnv *env) field fields_to_find[] = { { "android/hardware/Camera", "mNativeContext", "I", &fields.context }, { "android/view/Surface", ANDROID_VIEW_SURFACE_JNI_ID, "I", &fields.surface }, + { "android/graphics/SurfaceTexture", + ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I", &fields.surfaceTexture }, { "android/hardware/Camera$CameraInfo", "facing", "I", &fields.facing }, { "android/hardware/Camera$CameraInfo", "orientation", "I", &fields.orientation }, }; @@ -708,4 +733,3 @@ int register_android_hardware_Camera(JNIEnv *env) return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera", camMethods, NELEM(camMethods)); } -