版权声明:本文为博主原创文章,未经博主允许不得转载。
一段简单的JNI例子,输入是Bitmap(需要是Mutable),结果是把Bitmap变成灰度图。
为了看起来有点价值,所以同时支持了RGB565和ARGB8888(囧rz)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <jni.h>
- #include <android/bitmap.h>
- #include <android/log.h>
- #ifndef eprintf
- #define eprintf(...) __android_log_print(ANDROID_LOG_ERROR,"@",__VA_ARGS__)
- #endif
- #define RGB565_R(p) ((((p) & 0xF800) >> 11) << 3)
- #define RGB565_G(p) ((((p) & 0x7E0 ) >> 5) << 2)
- #define RGB565_B(p) ( ((p) & 0x1F ) << 3)
- #define MAKE_RGB565(r,g,b) ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
- #define RGBA_A(p) (((p) & 0xFF000000) >> 24)
- #define RGBA_R(p) (((p) & 0x00FF0000) >> 16)
- #define RGBA_G(p) (((p) & 0x0000FF00) >> 8)
- #define RGBA_B(p) ((p) & 0x000000FF)
- #define MAKE_RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
- JNIEXPORT void JNICALL Java_com_yxcorp_hello_Effect_update
- (JNIEnv *env, jclass clazz, jobject zBitmap) {
- JNIEnv J = *env;
- if (zBitmap == NULL) {
- eprintf("bitmap is null ");
- return;
- }
- // Get bitmap info
- AndroidBitmapInfo info;
- memset(&info, 0, sizeof(info));
- AndroidBitmap_getInfo(env, zBitmap, &info);
- // Check format, only RGB565 & RGBA are supported
- if (info.width <= 0 || info.height <= 0 ||
- (info.format != ANDROID_BITMAP_FORMAT_RGB_565 && info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)) {
- eprintf("invalid bitmap ");
- J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "invalid bitmap");
- return;
- }
- // Lock the bitmap to get the buffer
- void * pixels = NULL;
- int res = AndroidBitmap_lockPixels(env, zBitmap, &pixels);
- if (pixels == NULL) {
- eprintf("fail to lock bitmap: %d ", res);
- J->ThrowNew(env, J->FindClass(env, "java/io/IOException"), "fail to open bitmap");
- return;
- }
- eprintf("Effect: %dx%d, %d ", info.width, info.height, info.format);
- int x = 0, y = 0;
- // From top to bottom
- for (y = 0; y < info.height; ++y) {
- // From left to right
- for (x = 0; x < info.width; ++x) {
- int a = 0, r = 0, g = 0, b = 0;
- void *pixel = NULL;
- // Get each pixel by format
- if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {
- pixel = ((uint16_t *)pixels) + y * info.width + x;
- uint16_t v = *(uint16_t *)pixel;
- r = RGB565_R(v);
- g = RGB565_G(v);
- b = RGB565_B(v);
- } else {// RGBA
- pixel = ((uint32_t *)pixels) + y * info.width + x;
- uint32_t v = *(uint32_t *)pixel;
- a = RGBA_A(v);
- r = RGBA_R(v);
- g = RGBA_G(v);
- b = RGBA_B(v);
- }
- // Grayscale
- int gray = (r * 38 + g * 75 + b * 15) >> 7;
- // Write the pixel back
- if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {
- *((uint16_t *)pixel) = MAKE_RGB565(gray, gray, gray);
- } else {// RGBA
- *((uint32_t *)pixel) = MAKE_RGBA(gray, gray, gray, a);
- }
- }
- }
- AndroidBitmap_unlockPixels(env, zBitmap);
- }