diff options
Diffstat (limited to 'core/jni/android_hardware_Camera.cpp')
-rw-r--r-- | core/jni/android_hardware_Camera.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/core/jni/android_hardware_Camera.cpp b/core/jni/android_hardware_Camera.cpp index 4cf317e..3bb6af3 100644 --- a/core/jni/android_hardware_Camera.cpp +++ b/core/jni/android_hardware_Camera.cpp @@ -79,6 +79,8 @@ struct fields_t { static fields_t fields; static Mutex sLock; +int bwDataSize = 0; + // provides persistent context for calls from native code to Java class JNICameraContext: public CameraListener { @@ -933,6 +935,44 @@ static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz) return env->NewStringUTF(params8.string()); } +static void android_hardware_Camera_rgbToBw(JNIEnv *env, jobject thiz, jbyteArray pixelBuffer, jint bufWidth, jint bufHeight) +{ + int width = bufWidth; + int height = bufHeight; + int size = width * height; + + static jbyte* pixeldata; + static jbyte* luminances; + + if(bwDataSize != size) { + pixeldata = (jbyte *)malloc(size*2); + luminances = (jbyte *)malloc(size); + bwDataSize = size; + ALOGV("Allocated buffer of size %d", size); + } + + env->GetByteArrayRegion(pixelBuffer, 0, size*2, pixeldata); + + // Convert the entire image to grayscale + for (int y = 0; y < height; y++) { + int offset = y * width *2; + for (int x = 0; x < width *2; x+=2) { + jbyte pixel = pixeldata[offset +x]; + jbyte pixel2 = pixeldata[offset +x + 1]; + // little endian + // GGGBBBBB | RRRRRGGG + jbyte b = (jbyte)(pixel & 0x1f); // 5 bits + jbyte g = (jbyte)(((pixel >> 5) & 0x07) | ((pixel2 & 0x07) << 3)); // 6 bits + jbyte r = (jbyte)((pixel2 >> 3) & 0x1f); // 5 bits + + // Calculate luminance cheaply, favoring green. + luminances[(offset + x)/2] = (jbyte)((r + g + b) << 1); + } + } + + env->SetByteArrayRegion(pixelBuffer, 0, size, luminances); +} + static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz) { ALOGV("reconnect"); @@ -1146,6 +1186,9 @@ static JNINativeMethod camMethods[] = { { "native_getParameters", "()Ljava/lang/String;", (void *)android_hardware_Camera_getParameters }, + { "native_rgbToBw", + "([BII)V", + (void *)android_hardware_Camera_rgbToBw }, { "reconnect", "()V", (void*)android_hardware_Camera_reconnect }, |