Java – How do I modify the Android bitmap in the NDK so that I can use it on the Java side?

How do I modify the Android bitmap in the NDK so that I can use it on the Java side?… here is a solution to the problem.

How do I modify the Android bitmap in the NDK so that I can use it on the Java side?

I call a C++ function via JNI and pass an RGBA_8888 bitmap, lock it, change the value, unlock it, return, and then use this C++ code to display it in Java:

AndroidBitmap_getInfo(env, map, &info) < 0);
AndroidBitmap_lockPixels(env, map, (void**)&pixel);

for(i=info.width*info.height-1; i>=0; i--)
{   pixel[i] = 0xf1f1f1f1;
}

AndroidBitmap_unlockPixels(env, map);

The problem I’m having is that the bitmaps don’t look as I expected, and when I check them in Java, the pixel values (verified with getPixel) are different from the values I set in C++. When I set the bitmap value to 0xffffffff, I get the correct value in Java, but for many others I don’t. For example, 0xf1f1f1f1 becomes 0xF1FFFFFF.

What do I need to do to make it work?

PS: I’m using Android 2.3.4

Solution

It seems that the problem is because of the premultiplication alpha.

Further testing shows that r, g, b

values are scaled by alpha, so if I pass bitmap to jni, I need to multiply the r, g, b value by 255 and then divide by alpha to get the true value I can alpha. When I pass them back, I have to do the opposite.

This is unnecessary only for copying and manipulation that do not require alpha, as alpha will be 255 for maximum opacity, while *255/255 will negate itself.

Related Problems and Solutions