简体   繁体   中英

convert createBitmap from Java to JNI

I want to use this code in JNI , without going back to Java .

I already converted bitmap manipulation to JNI (thanks to other stackoverflow posters), but this seems more complicated because I do not understand how to call constructors.

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);

Ok, it is actually very simple once you master java->jni translation. Basically you can do anything in JNI side what you can do in Java. Yes, it looks messy. I decided not to create the bitmap in JNI but to access the existing one though.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM