开发者

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.


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);


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜