从 C++ 创建一个 android.graphics.Bitmap

时间:2023-03-17
本文介绍了从 C++ 创建一个 android.graphics.Bitmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些基于 NDK 的 C++ 代码需要构建一个 android 位图对象.我确信有一种方法可以直接从 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 ;)

所以我要调用的方法是

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

因此,要从本机代码执行此操作,我需要执行以下步骤.

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 步非常迷茫.我的代码现在看起来像这样:

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++ 代码创建枚举?

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!

提前致谢.

推荐答案

我的代码中有这个,所以我可以给你答案.

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

1) 获取createBitmap(int width, int height, Bitmap.Config config)的静态方法id:

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

这里我们从命名值创建 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);

这篇关于从 C++ 创建一个 android.graphics.Bitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:android ndk中的共享内存 下一篇:没有制定目标 NDK 的规则

相关文章

最新文章