我正在尝试从OpenGL ES中捕获帧缓冲数据,并从中创建图像,基本上是为了捕获屏幕截图。
我正试图通过JNI使用BitmapFactory类进行转换。这是我的当前代码:
基本上,代码的目的是:
相同的代码用于将帧缓冲区捕获到像素数组中(即glReadPixels调用),在iOS和MacOSX上没有问题,因此我不认为问题出在那里。
所以,任何关于我可能遗漏的内容的帮助?
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方法
相同的代码用于将帧缓冲区捕获到像素数组中(即glReadPixels调用),在iOS和MacOSX上没有问题,因此我不认为问题出在那里。
所以,任何关于我可能遗漏的内容的帮助?