我想将位图像素复制到3D数组,并从这个3d数组中为特定的图像处理算法分配新的数组。我的代码将涉及最多使用MD数组。我想要一种有效的方法,快速,没有内存开销/泄漏......
这是我目前的Cpp代码:
#include <jni.h>
#include <android/log.h>
#include <android/bitmap.h>
#include "com_example_ndk_MainActivity.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
} argb;
JNIEXPORT void JNICALL Java_com_example_ndk_MainActivity_invert(JNIEnv *env, jobject obj, jobject bitmapcolor,jobject bitmapgray)
{
AndroidBitmapInfo infocolor;
void* pixelscolor;
AndroidBitmapInfo infogray;
void* pixelsgray;
int ret;
int y;
int x;
int i,j;
if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) {
return;
}
if ((ret = AndroidBitmap_getInfo(env, bitmapgray, &infogray)) < 0) {
return;
}
if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) {
}
if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) {
}
// copy pixels to array
int* arr;
arr = new int[infocolor.height * infocolor.width * 3* sizeof(int)];
for (y=0;y<infocolor.height;y++) {
argb * line = (argb *) pixelscolor;
argb * grayline = (argb *) pixelsgray;
for (x=0;x<infocolor.width;x++) {
arr[x * (infocolor.height * 3) + y * 3 + 1] = line[x].red;
arr[x * (infocolor.height * 3) + y * 3 + 2] = line[x].green;
arr[x * (infocolor.height * 3) + y * 3 + 3] = line[x].blue;
}
pixelscolor = (char *)pixelscolor + infocolor.stride;
}
//do the algorithm on the array of pixels here--------
//the store the new pixels in the returned bitmap
AndroidBitmap_unlockPixels(env, bitmapcolor);
for (y=0;y<infocolor.height;y++) {
argb * grayline = (argb *) pixelsgray;
for (x=0;x<infocolor.width;x++) {
grayline[x].alpha = 255;
grayline[x].red = arr[x * (infocolor.height * 3) + y * 3 + 1];
grayline[x].green = arr[x * (infocolor.height * 3) + y * 3 + 2];
grayline[x].blue = arr[x * (infocolor.height * 3) + y * 3 + 3];
}
pixelsgray = (char *)pixelsgray + infocolor.stride;
}
AndroidBitmap_unlockPixels(env, bitmapgray);
}