从 C++ 创建 android.graphics.Bitmap
我有一些基于 NDK 的 C++ 代码,需要构建 android 位图对象。我确信有一种方法可以直接从 C++ 代码执行此操作,但这不是最简单的事情;)
所以我想调用的方法是
Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );
因此,要从本机代码执行此操作,我需要执行以下步骤。
- 找到类(android.graphics.Bitmap)。
- 获取“createBitmap”的静态方法id。
- 创建枚举。
- 调用静态方法。
(最终我需要创建一个 jintArray 并传入数据,但我稍后会担心这一点)。
不过我对第2步和第3步很迷茫。我的代码现在看起来像这样:
jclass jBitmapClass = gpEnv->FindClass( "android.graphics.Bitmap" );
jmethodID jBitmapCreater = gpEnv->GetStaticMethodID( jBitmapClass, "createBitmap", "(IILandroid/graphics/Bitmap/Config;)Landroid/graphics/Bitmap;" );
但后来我陷入困境。如何从本机 C/C++ 代码创建枚举?
此外,我在 GetStaticMethodID 中的最后一个参数是否正确?我不确定如何指定特定对象,但我认为上述方法有效。不过,枚举可能是错误的!
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的代码中有这个,所以我可以给你有效的答案。
1) 获取createBitmap(int width, int height, Bitmap.Config config)的静态方法id:
注意Bitmap.Config的签名,里面有$符号。
2) 使用给定值创建 Bitmap.Config 枚举:
这里我们根据命名值创建 Bitmap.Config 枚举。另一个可能的值字符串是“RGB_565”。
3)调用createBitmap:
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):
Note signature of Bitmap.Config, it has $ sign in it.
2) Creating enum for Bitmap.Config with given value:
Here we create Bitmap.Config enum from named value. Another possible value string is "RGB_565".
3) Calling createBitmap:
枚举在编译时会映射到 Java 类。
此示例可能对您有帮助:
http ://mike-java.blogspot.com/2008/05/java-enum-in-java-native-interface-jni.html
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