[英]convert createBitmap from Java to JNI
我想在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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.