开发者

Android Pass Bitmap to Native in 2.1 and lower

It's extremely easy to get the Bitmap data in the NDK when working with Android 2.2, but with 2.1 and lower, the AndroidBitmap_lockPixels function is not available. I've been searching for开发者_开发知识库 the past few hours, but nothing has worked.

How can I access the pixel data of a bitmap without using that function?


Create empty bitmap with dimensions of original image and ARGB_8888 format:

int width =  src.getWidth();
int height = src.getHeight();
Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Copy pixels from source bitmap to the int array:

int[] pixels = new int[width * height];
src.getPixels(pixels, 0, width, 0, 0, width, height);

And set these pixels to destination bitmap:

dest.setPixels(pixels, 0, width, 0, 0, width, height);


Create an IntBuffer in your Java code and pass the array down to your native library:

// this is called from native code
buffer = IntBuffer.allocate(width*height);
return buffer.array();

Use GetIntArrayElements to get an jint* and write to the array:

jint * arr = env->GetIntArrayElements((jintArray)bufferArray, NULL);

Write to the array and when finished, release:

env->ReleaseIntArrayElements((jintArray)bufferArray, arr, 0);

Notify the Java code that the array has been updated and use Canvas.drawBitmap() to draw the IntBuffer:

canvas.drawBitmap(buffer.array(), ....);

To draw to a Bitmap, initialize the canvas with the bitmap

... new Canvas(bitmap)


Someone else just asked the same question - I'll just link to it to avoid duplicating my answer:

Android rendering to live wallpapers

In any event, you probably don't want to copy the bitmap data every time you need to exchange it between Java and JNI code, so if your code is performance sensitive, this may be your only option on Android 2.1 and lower.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜