我想捕獲從OpenGL ES的幀緩衝數據和從它創建一個圖像,基本上,以此來捕捉屏幕截圖從OpenGL ES的幀緩衝區通過JNI在Android NDK創建位圖圖像
我試圖通過JNI使用BitmapFactory類來完成轉換。這裏是我當前的代碼:
size_t size = w * h;
uint8_t *pixels = new uint8_t[size];
glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
jobject jbitmap = 0;
if(pixels != 0) {
JNIEnv * env = GetEnv(state_->activity->vm);
//create the jbyte array and fill in with the pixel data
int byte_array_length = w * h;
jbyteArray byte_array = env->NewByteArray(byte_array_length);
env->SetByteArrayRegion(byte_array, 0, byte_array_length, (jbyte *)pixels);
//get the BitmapFactory class
jclass bitmap_factory_class = loadExternalClass("android/graphics/BitmapFactory");
jmethodID decode_byte_array_method = env->GetStaticMethodID(bitmap_factory_class,
"decodeByteArray", "([BII)Landroid/graphics/Bitmap;");
//get the bitmap itself
jbitmap = env->CallStaticObjectMethod(bitmap_factory_class, decode_byte_array_method,
byte_array, 0, byte_array_length);
env->DeleteLocalRef(byte_array);
}
if(jbitmap == 0) {
Log("Could not create image from framebuffer to share");
}
基本上,代碼試圖做的是:
- 捕獲的OpenGL幀緩衝區爲像素陣列
- 尺寸W * H的創建jbyteArray
- 填充jbytearray
- 獲取BitmapFactory類
- 用jbytearray調用decodeByteArray方法
然而,jbitmap對象被連續地設置爲0,這使我認爲BitmapFactory呼叫失敗,我不知道爲什麼用於捕獲幀緩衝器到該像素陣列(
相同的代碼即:glReadPixels調用)使用沒有問題在iOS & MacOSX,所以我不認爲問題在於那裏
因此,任何幫助我可能會失蹤?
最初發布到[android-ndk google group](https://groups.google.com/forum/#!topic/android-ndk/oA6v40n49S8) – codemonkey
@ AndonM.Coleman我已經記錄了所有三個(w, h&像素)。 W&H確實符合我的屏幕尺寸,並且像素不爲零,也不全爲零。對於我來說,確定它是否確實是有效的數據有點困難,但我猜測它應該是,因爲它只是一個直線glReadPixels調用 – codemonkey