我想将位图传递给native,在bitmap.h的帮助下提取像素,而不是将像素存储在3D数组(x和y和颜色通道)中,通过调用其他c函数对数组进行一些处理,完成后,我将数组写回位图的像素。问题是:我可以提取像素,但我不能将它们存储在数组中。如果你能告诉我如何将数组写回位图的像素。
我的代码是:
#include <jni.h>
#include <time.h>
#include <android/log.h>
#include <android/bitmap.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define LOG_TAG "libimageprocessing"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
static void process(AndroidBitmapInfo* info, void* pixels){
int xx, yy, red, green, blue;
int w = info->width;
int h = info->height;
uint32_t* line;
int rgb[h][w][3];
for(yy = 0; yy < info->height; yy++){
line = (uint32_t*)pixels;
for(xx =0; xx < info->width; xx++){
red = (int) ((line[xx] & 0x00FF0000) >> 16);
green = (int)((line[xx] & 0x0000FF00) >> 8);
blue = (int) (line[xx] & 0x00000FF );
red = 255 - red;
green = 255 - green;
blue = 255 - blue ;
rgb[yy][xx][0] = red; ////THIS LINE CAUSES THE CRASH, If removed the app returns the inverted image. I WANT TO IMPLEMENT SUCH THING, THEN I DON'T KNOW HOW TO WRITE IT BACK TO BITMAP PIXELS.
line[xx] =
((red << 16) & 0x00FF0000) |
((green << 8) & 0x0000FF00) |
(blue & 0x000000FF);
}
pixels = (char*)pixels + info->stride;
}
}
JNIEXPORT void JNICALL Java_com_example_ibm_MainActivity_process(JNIEnv* env, jobject obj, jobject bitmap)
{
AndroidBitmapInfo info;
int ret;
void* pixels;
if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return;
}
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
LOGE("Bitmap format is not RGBA_8888 !");
return;
}
if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
}
process(&info,pixels);
AndroidBitmap_unlockPixels(env, bitmap);
}