gpt4 book ai didi

android - 将 createBitmap 从 Java 转换为 JNI

转载 作者:行者123 更新时间:2023-11-30 03:42:49 24 4
gpt4 key购买 nike

我想在 JNI 中使用这段代码,而不用返回到 Java

我已经将位图操作转换为 JNI(感谢其他 stackoverflow 海报),但这看起来更复杂,因为我不明白如何调用构造函数。

Bitmap bmp;
bmp = ((BitmapDrawable)imgview.getDrawable()).getBitmap();
if (bmp == null || !bmp.isMutable())
Bitmap bmp = Bitmap.createBitmap(w, h, Config.ARGB_8888);

// bitmap manipulations goes here
jclass java_bitmap_class = (env)->GetObjectClass(java_bitmap);
class SkBitmap;
SkBitmap *sk_bitmap = (SkBitmap*)(env)->CallIntMethod(
java_bitmap, (env)->GetMethodID(java_bitmap_class, "ni", "()I"));
// there is more c++ code to manipulate bmp, but it is not relevant to a question

imgview.setImageBitmap(bmp);

最佳答案

好了,掌握了java->jni的翻译其实就很简单了。基本上,您可以在 JNI 方面做您在 Java 中可以做的任何事情。是的,看起来很乱。我决定不在 JNI 中创建位图,而是访问现有位图。

JNIEnv* Env = 0; jobject Obj; 
jclass cls = 0, ClassImageView = 0, class_drawable = 0, java_bitmap_class = 0;
jmethodID jcontrol_ui = 0, jfindViewById = 0, jgetBitmap = 0, jgetDrawable = 0;

int *getViewBitmapBuffer(int ID) {
jobject image_view = (jobject) (Env)->CallObjectMethod(Obj, jfindViewById, ID);
// some values can be cached, hence the checks for "(something == 0)"
if (ClassImageView == 0) ClassImageView = (Env)->GetObjectClass(image_view);
if (jgetDrawable == 0) jgetDrawable = (Env)->GetMethodID(ClassImageView, "getDrawable", sig_drawable);
jobject drawable = (jobject) (Env)->CallObjectMethod(image_view, jgetDrawable);
if (class_drawable == 0) class_drawable = (Env)->GetObjectClass(drawable);
if (jgetBitmap == 0) jgetBitmap = (Env)->GetMethodID(class_drawable, "getBitmap", sig_bitmap);
jobject java_bitmap = (jobject) (Env)->CallObjectMethod(drawable, jgetBitmap);
if (java_bitmap_class == 0) java_bitmap_class = (Env)->GetObjectClass(java_bitmap);
class SkBitmap;
SkBitmap *sk_bitmap = (SkBitmap*)(Env)->CallIntMethod(java_bitmap, (Env)->GetMethodID(java_bitmap_class, "ni", "()I"));
SkPixelRef *sk_pix_ref;
sk_pix_ref = (SkPixelRef*)((int*)sk_bitmap)[1];
int *B = (int*) sk_pix_ref->GetPixels();
return B;
}

关于android - 将 createBitmap 从 Java 转换为 JNI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15470975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com