Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an android.graphics.Bitmap from C++

I have some NDK based C++ code that needs to build an android bitmap object. I'm sure there is a way to do this directly from the C++ code but its not the easiest of things to do ;)

So the method I want to call is

Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );

So to do this from native code I need to do the following steps.

  • Find the class (android.graphics.Bitmap).
  • Get the static method id of "createBitmap".
  • Create the enum.
  • Call the static method.

(Eventually I will need to create a jintArray and pass the data in but I'll worry about that later).

I'm very lost on steps 2 and 3 though. My code looks like this at the moment:

jclass      jBitmapClass        = gpEnv->FindClass( "android.graphics.Bitmap" );
jmethodID   jBitmapCreater      = gpEnv->GetStaticMethodID( jBitmapClass, "createBitmap", "(IILandroid/graphics/Bitmap/Config;)Landroid/graphics/Bitmap;" );

but then I'm stuck. How do I create an enum from native C/C++ code?

Furthermore is my last parameter into GetStaticMethodID correct? I wasn't sure how to specify the specific objects but I think the above works. May be wrong on the enum, though!

Thanks in advance.

like image 607
Goz Avatar asked Oct 06 '11 15:10

Goz


2 Answers

I have this in my code, so I can give you answer that works.

1) Get the static method id of createBitmap(int width, int height, Bitmap.Config config):

jclass java_bitmap_class = (jclass)env.FindClass("android/graphics/Bitmap");
jmethodID mid = env.GetStaticMethodID(java_bitmap_class, "createBitmap", "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");

Note signature of Bitmap.Config, it has $ sign in it.

2) Creating enum for Bitmap.Config with given value:

const wchar_t config_name[] = L"ARGB_8888";
jstring j_config_name = env.NewString((const jchar*)config_name, wcslen(config_name));
jclass bcfg_class = env.FindClass("android/graphics/Bitmap$Config");
jobject java_bitmap_config = env.CallStaticObjectMethod(bcfg_class, env.GetStaticMethodID(bcfg_class, "valueOf", "(Ljava/lang/String;)Landroid/graphics/Bitmap$Config;"), j_config_name);

Here we create Bitmap.Config enum from named value. Another possible value string is "RGB_565".

3) Calling createBitmap:

java_bitmap = env.CallStaticObjectMethod(java_bitmap_class, mid, w, h, java_bitmap_config);
like image 163
Pointer Null Avatar answered Nov 09 '22 19:11

Pointer Null


Enums are mapped to Java classes when compiled.

This example might help you:

http://mike-java.blogspot.com/2008/05/java-enum-in-java-native-interface-jni.html

like image 1
Paulo Pinto Avatar answered Nov 09 '22 21:11

Paulo Pinto