1

我有下面的代码在 JNI 中使用“ARGB_8888”配置创建一个位图(只是一个黑色/灰色图像)。但是,当我在 Java 代码中转储位图的内容时,我只能看到配置,而看不到位图中的像素数据。

JNI 代码

// Image Details
int imgWidth = 128;
int imgHeight = 128;
int numPix = imgWidth * imgHeight;

// Creaing Bitmap Config Class
jclass bmpCfgCls = env->FindClass("android/graphics/Bitmap$Config");
jmethodID bmpClsValueOfMid = env->GetStaticMethodID(bmpCfgCls, "valueOf", "(Ljava/lang/String;)Landroid/graphics/Bitmap$Config;");
jobject jBmpCfg = env->CallStaticObjectMethod(bmpCfgCls, bmpClsValueOfMid, env->NewStringUTF("ARGB_8888"));

// Creating a Bitmap Class
jclass bmpCls = env->FindClass("android/graphics/Bitmap");
jmethodID createBitmapMid = env->GetStaticMethodID(bmpCls, "createBitmap", "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
jBmpObj = env->CallStaticObjectMethod(bmpCls, createBitmapMid, imgWidth, imgHeight, jBmpCfg);

// Creating Pixel Data
int triplicateLen = numPix * 4;
char *tripPixData = (char*)malloc(triplicateLen);
for (int lc = 0; lc < triplicateLen; lc++)
{ 
    // Gray / Black Image
    if (0 == (lc%4))
        tripPixData[lc] = 0x7F; // Alpha
    else
        tripPixData[lc] = 0x00; // RGB
}

// Setting Pixels in Bitmap
jByteArr = env->NewByteArray(triplicateLen);
env->SetByteArrayRegion(jByteArr, 0, triplicateLen, (jbyte*)tripPixData);
jmethodID setPixelsMid = env->GetMethodID(bmpCls, "setPixels", "([IIIIIII)V");
env->CallVoidMethod(jBmpObj, setPixelsMid, (jintArray)jByteArr, 0, imgWidth, 0, 0, imgWidth, imgHeight);

free(tripPixData);

// Return BitMap Object
return jBmpObj;

在 JAVA 中(输出)

// Checking the Configuration / Image Details
jBmpObj.getWidth() - 128
jBmpObj.getHeight() - 128
jBmpObj.getRowBytes() - 512
jBmpObj.getConfig() - ARGB 8888

// Getting Pixel Data
imgPixs = new int[jBmpObj.getWidth() * jBmpObj.getHeight()];
jBmpObj.getPixels(imgPixs, 0, jBmpObj.getWidth(), 0, 0, jBmpObj.getWidth(), jBmpObj.getHeight());

// Running a Loop on the imgPixs
imgPixs[<0 - imgPixs.lenght>] - 0 (Every Pixel Data)

我使用相同的概念在 Java 代码中创建了位图,并且效果很好(即使我能够看到图像)。但我希望逻辑在 JNI 部分而不是在 Java 代码中。所以我尝试了上述逻辑,但未能设置像素数据。

解决此问题的任何输入都将非常有帮助,..

4

3 回答 3

9

完整的工作示例:

jclass bitmapConfig = jniEnv->FindClass("android/graphics/Bitmap$Config");
    jfieldID rgba8888FieldID = jniEnv->GetStaticFieldID(bitmapConfig, "ARGB_8888", "Landroid/graphics/Bitmap$Config;");
    jobject rgba8888Obj = jniEnv->GetStaticObjectField(bitmapConfig, rgba8888FieldID);

    jclass bitmapClass = jniEnv->FindClass("android/graphics/Bitmap");
    jmethodID createBitmapMethodID = jniEnv->GetStaticMethodID(bitmapClass,"createBitmap", "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
    jobject bitmapObj = jniEnv->CallStaticObjectMethod(bitmapClass, createBitmapMethodID, _width, _height, rgba8888Obj);

    jintArray pixels = jniEnv->NewIntArray(_width * _height);
    for (int i = 0; i < _width * _height; i++)
    {
        unsigned char red = bitmap[i*4];
        unsigned char green = bitmap[i*4 + 1];
        unsigned char blue = bitmap[i*4 + 2];
        unsigned char alpha = bitmap[i*4 + 3];
        int currentPixel = (alpha << 24) | (red << 16) | (green << 8) | (blue);
        jniEnv->SetIntArrayRegion(pixels, i, 1, &currentPixel);
    }

    jmethodID setPixelsMid = jniEnv->GetMethodID(bitmapClass, "setPixels", "([IIIIIII)V");
    jniEnv->CallVoidMethod(bitmapObj, setPixelsMid, pixels, 0, _width, 0, 0, _width, _height);

其中位图是无符号字符*。

于 2015-08-26T10:35:20.627 回答
0
IN Android each pixel represented as 0xFFFFFFFF ie ARGB.
0xFF referes most significamt 8 bits of given data.
From your snippet, where you are getting soure image data? But i have solved this issue
by using following code base.i hope this ll help you.

// 创建像素数据

unsigned char* rawData = //your raw data

**Note**: here you have get each r,g & b component as 8 bit data //If it is rgb image,if it             
is monochrome you can use raw data

int triplicateLen = imgheight * imgwidth;
int *tripPixData = (int*) malloc(triplicateLen * sizeof(int));

if(rgb){
       for (int lc = 0; lc < triplicateLen ; lc++){
            tripPixData [lc] = (0xFF << 24) | (r[lc] << 16) | (g[lc] << 8) | b[lc]; 
       }
 }else{
      for (int lc = 0; lc < triplicateLen ; lc++){
            tripPixData [lc] = (0xFF << 24) | (rawData [lc] << 16) | (rawData [lc] << 8) | rawData [lc];    
       }
 }
于 2014-09-03T05:16:01.573 回答
0

在 Java 中不能将 byte[] 转换为 int[],因此不能在 JNI 中转换它。但是您可以将 char* 转换为 int*,因此您可以简单地使用您的 tripPixData 来填充一个新的 jjintArray。

于 2012-12-24T02:42:25.930 回答