创建从C的android.graphics.Bitmap ++android、graphics、Bitmap

2023-09-05 09:01:36 作者:月亮是我啃弯的

我有一些NDK基于C ++ $ C $,需要建立一个Android的位图对象C。我敢肯定有一种方法可以直接从C ++ code做到这一点,但它不是最容易做的事情;)

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

所以我要调用的方法是

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

所以,从本地code做到这一点,我需要做下面的步骤。

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

找到类(android.graphics.Bitmap)。 在获取createBitmap静态方法ID。 创建枚举。 调用静态方法。

(最后,我需要创建一个jintArray和传递数据,但我会担心以后)。

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

我非常虽然失去了步骤2和3。我的code看起来像这样的时刻:

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

但我坚持。如何创建从本机C枚举/ C ++ code?

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

此外就是我的最后一个参数为GetStaticMethodID正确吗?我不知道如何指定特定的对象,但我认为上述作品。可能是错误的枚举,但!

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!

在此先感谢。

推荐答案

我已经在我的code,这样我就可以给你答案的作品。

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

1)获取createBitmap的静态方法ID(INT宽度,高度INT,Bitmap.Config配置):

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

请注意Bitmap.Config的签名,它有$符号在里面。

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

2)创建枚举的Bitmap.Config与给定值:

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

在这里,我们创建一个名为value Bitmap.Config枚举。另一种可能的值字符串为RGB_565。

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

3)调用createBitmap:

3) Calling createBitmap:

java_bitmap = env.CallStaticObjectMethod(java_bitmap_class, mid, w, h, java_bitmap_config);