简体   繁体   English

从C ++创建android.graphics.Bitmap

[英]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. 所以要从本机代码执行此操作,我需要执行以下步骤。

  • Find the class (android.graphics.Bitmap). 找到类(android.graphics.Bitmap)。
  • Get the static method id of "createBitmap". 获取“createBitmap”的静态方法ID。
  • 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). (最终我需要创建一个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.

相关问题 在没有Android的情况下从Java使用android.graphics.Bitmap - Use android.graphics.Bitmap from Java without Android Jackson ObjectMapper冲突的setter定义(Android.Graphics.Bitmap) - Jackson ObjectMapper Conflicting setter definitions (Android.Graphics.Bitmap) Jackson没有为android.graphics.Bitmap找到合适的构造函数 - Jackson no suitable constructor found for android.graphics.Bitmap Java中的android.graphics.Bitmap序列化和反序列化 - Serializing and De-Serializing android.graphics.Bitmap in Java org.mapsforge.core.graphics.Bitmap和android.graphics.Bitmap之间的任何关系方法 - Any relation method between org.mapsforge.core.graphics.Bitmap and android.graphics.Bitmap 方法'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap $ CompressFormat,int,java.io.OutputStream) - method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream) kotlin.TypeCastException:null无法强制转换为非null类型android.graphics.Bitmap - kotlin.TypeCastException: null cannot be cast to non-null type android.graphics.Bitmap 尝试在空对象引用上调用虚拟方法'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' - Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference 使用jni将位图从android相机传递到C ++ - Passing a bitmap from android camera to C++ with jni 从Bitmap android的边缘创建Path - Creating Path from edges of the Bitmap android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM