[英]Creating an android.graphics.Bitmap from C++
I have some NDK based C++ code that needs to build an android bitmap object. 我有一些基于NDK的C ++代码需要构建一个android位图对象。 I'm sure there is a way to do this directly from the C++ code but its not the easiest of things to do ;)
我确信有一种方法可以直接从C ++代码执行此操作,但这不是最简单的事情;)
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. 所以要从本机代码执行此操作,我需要执行以下步骤。
(Eventually I will need to create a jintArray and pass the data in but I'll worry about that later). (最终我需要创建一个jintArray并传入数据但我稍后会担心)。
I'm very lost on steps 2 and 3 though. 我在第2步和第3步很失落。 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?
如何从本机C / C ++代码创建枚举?
Furthermore is my last parameter into GetStaticMethodID correct? 我的最后一个参数是GetStaticMethodID正确吗? 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): 1)获取createBitmap的静态方法id(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. 注意Bitmap.Config的签名,它有$符号。
2) Creating enum for Bitmap.Config with given value: 2)使用给定值为Bitmap.Config创建枚举:
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. 这里我们从命名值创建Bitmap.Config枚举。 Another possible value string is "RGB_565".
另一个可能的值字符串是“RGB_565”。
3) Calling createBitmap: 3)调用createBitmap:
java_bitmap = env.CallStaticObjectMethod(java_bitmap_class, mid, w, h, java_bitmap_config);
Enums are mapped to Java classes when compiled. 枚举在编译时映射到Java类。
This example might help you: 此示例可能对您有所帮助:
http://mike-java.blogspot.com/2008/05/java-enum-in-java-native-interface-jni.html http://mike-java.blogspot.com/2008/05/java-enum-in-java-native-interface-jni.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.