The jnigraphics library can be used to access bitmap buffers in C / C ++ from the android.bitmap.Graphics class (in Java, of course). It is described in more detail in the documentation supplied with the NDK:
android-ndk-r5b/docs/STABLE-APIS.html
It can be used to download images, for example. OpenGL ES is in C / C ++, but you need to do some work to pass the jobject to this library so that it can give you direct access to the buffer. You can pass this buffer to OpenGL via glTexImage2D() .
First, you need a Java Bitmap object that you can acquire and pass into your own method as follows:
import android.graphics.Bitmap; import android.graphics.BitmapFactory; ... BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = false; Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.myimage, options); MyJniMethod(bitmap); // Should be static in this example
This native method might look something like this:
#include <android/bitmap.h> void MyJniMethod(JNIEnv *env, jobject obj, jobject bitmap) { AndroidBitmapInfo info; uint32_t *pixels; int ret; AndroidBitmap_getInfo(env, bitmap, &info); if(info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { LOGE("Bitmap format is not RGBA_8888!"); return false; } AndroidBitmap_lockPixels(env, bitmap, reinterpret_cast<void **>(&pixels)); // Now you can use the pixel array 'pixels', which is in RGBA format }
Keep in mind that you should call AndroidBitmap_unlockPixels() when you are done with the pixel buffer, and that this example does not check for errors at all.
Update for the Sid Datta question: you can make sure the output image format is what you expect by adding this to the above options:
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
There is one case where the output image will still have an unknown format in JNI. This seems to only happen with GIFs. After calling BitmapFactory.decodeResource() you can optionally convert the image to the desired format:
if (bitmap.getConfig() != Bitmap.Config.ARGB_8888) { Bitmap reformatted_bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false); bitmap.recycle(); /* reduce memory load in app w/o waiting for GC */ bitmap = reformatted_bitmap; }