[Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888

 

[Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888 

标签: androidbitmapjni

一段简单的JNI例子,输入是Bitmap(需要是Mutable),结果是把Bitmap变成灰度图。

为了看起来有点价值,所以同时支持了RGB565和ARGB8888(囧rz)

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <stdint.h>  
  5. #include <jni.h>  
  6. #include <android/bitmap.h>  
  7. #include <android/log.h>  
  8.   
  9. #ifndef eprintf  
  10. #define eprintf(...) __android_log_print(ANDROID_LOG_ERROR,"@",__VA_ARGS__)  
  11. #endif  
  12.   
  13. #define RGB565_R(p) ((((p) & 0xF800) >> 11) << 3)  
  14. #define RGB565_G(p) ((((p) & 0x7E0 ) >> 5)  << 2)  
  15. #define RGB565_B(p) ( ((p) & 0x1F  )        << 3)  
  16. #define MAKE_RGB565(r,g,b) ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))  
  17.   
  18. #define RGBA_A(p) (((p) & 0xFF000000) >> 24)  
  19. #define RGBA_R(p) (((p) & 0x00FF0000) >> 16)  
  20. #define RGBA_G(p) (((p) & 0x0000FF00) >>  8)  
  21. #define RGBA_B(p)  ((p) & 0x000000FF)  
  22. #define MAKE_RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))  
  23.   
  24. JNIEXPORT void JNICALL Java_com_yxcorp_hello_Effect_update  
  25.   (JNIEnv *env, jclass clazz, jobject zBitmap) {  
  26.     JNIEnv J = *env;  
  27.   
  28.     if (zBitmap == NULL) {  
  29.         eprintf("bitmap is null ");  
  30.         return;  
  31.     }  
  32.   
  33.     // Get bitmap info  
  34.     AndroidBitmapInfo info;  
  35.     memset(&info, 0, sizeof(info));  
  36.     AndroidBitmap_getInfo(env, zBitmap, &info);  
  37.     // Check format, only RGB565 & RGBA are supported  
  38.     if (info.width <= 0 || info.height <= 0 ||  
  39.         (info.format != ANDROID_BITMAP_FORMAT_RGB_565 && info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)) {  
  40.         eprintf("invalid bitmap ");  
  41.         J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "invalid bitmap");  
  42.         return;  
  43.     }  
  44.   
  45.     // Lock the bitmap to get the buffer  
  46.     void * pixels = NULL;  
  47.     int res = AndroidBitmap_lockPixels(env, zBitmap, &pixels);  
  48.     if (pixels == NULL) {  
  49.         eprintf("fail to lock bitmap: %d ", res);  
  50.         J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "fail to open bitmap");  
  51.         return;  
  52.     }  
  53.   
  54.     eprintf("Effect: %dx%d, %d ", info.width, info.height, info.format);  
  55.   
  56.     int x = 0, y = 0;  
  57.     // From top to bottom  
  58.     for (y = 0; y < info.height; ++y) {  
  59.         // From left to right  
  60.         for (x = 0; x < info.width; ++x) {  
  61.             int a = 0, r = 0, g = 0, b = 0;  
  62.             void *pixel = NULL;  
  63.             // Get each pixel by format  
  64.             if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {  
  65.                 pixel = ((uint16_t *)pixels) + y * info.width + x;  
  66.                 uint16_t v = *(uint16_t *)pixel;  
  67.                 r = RGB565_R(v);  
  68.                 g = RGB565_G(v);  
  69.                 b = RGB565_B(v);  
  70.             } else {// RGBA  
  71.                 pixel = ((uint32_t *)pixels) + y * info.width + x;  
  72.                 uint32_t v = *(uint32_t *)pixel;  
  73.                 a = RGBA_A(v);  
  74.                 r = RGBA_R(v);  
  75.                 g = RGBA_G(v);  
  76.                 b = RGBA_B(v);  
  77.             }  
  78.   
  79.             // Grayscale  
  80.             int gray = (r * 38 + g * 75 + b * 15) >> 7;  
  81.   
  82.             // Write the pixel back  
  83.             if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {  
  84.                 *((uint16_t *)pixel) = MAKE_RGB565(gray, gray, gray);  
  85.             } else {// RGBA  
  86.                 *((uint32_t *)pixel) = MAKE_RGBA(gray, gray, gray, a);  
  87.             }  
  88.         }  
  89.     }  
  90.   
  91.     AndroidBitmap_unlockPixels(env, zBitmap);  
  92. }  
原文地址:https://www.cnblogs.com/qingchen1984/p/5007896.html