使用Android 2.2时,获取NDK中的位图数据非常简单,但使用2.1或更低版本时,AndroidBitmap_lockPixels函数不可用。我一直在寻找过去的几个小时,但没有任何工作。Android Pass Bitmap to Native in 2.1 and lower
如何在不使用该函数的情况下访问位图的像素数据?
使用Android 2.2时,获取NDK中的位图数据非常简单,但使用2.1或更低版本时,AndroidBitmap_lockPixels函数不可用。我一直在寻找过去的几个小时,但没有任何工作。Android Pass Bitmap to Native in 2.1 and lower
如何在不使用该函数的情况下访问位图的像素数据?
创建原始图像的尺寸和ARGB_8888格式空位图:
int width = src.getWidth();
int height = src.getHeight();
Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
复制像素从源位图中的int数组:
int[] pixels = new int[width * height];
src.getPixels(pixels, 0, width, 0, 0, width, height);
并设置这些像素到目标位图:
dest.setPixels(pixels, 0, width, 0, 0, width, height);
对不起,我忘记提及我正在使用NDK。像素在Java中工作正常。 – GuyNoir 2011-01-14 19:10:36
在您的Java代码中创建一个IntBuffer并将该数组传递给t Ø您的本机库:
// this is called from native code
buffer = IntBuffer.allocate(width*height);
return buffer.array();
使用GetIntArrayElements得到一个jint *和写入阵列:
写入阵列,完成后,释放:
env->ReleaseIntArrayElements((jintArray)bufferArray, arr, 0);
通知该数组已更新的Java代码并使用Canvas.drawBitmap()绘制IntBuffer:
canvas.drawBitmap(buffer.array(), ....);
提请位图,与位图
... new Canvas(bitmap)
这看起来越来越近了。我会测试一下。谢谢。 – GuyNoir 2011-01-19 02:37:25
刚才有人问同样的问题初始化画布 - 我只是链接到它,以避免重复我的回答:
Android rendering to live wallpapers
无论如何,您可能不希望在每次需要在Java和JNI代码之间交换位图数据时复制位图数据,因此如果您的代码对性能非常敏感,这可能是您在Android 2.1及更低版本上的唯一选项。
GuyNoir您是否检查了android-opencv http://code.google.com/p/android-opencv/它们支持1.5及以上版本 – 100rabh 2011-01-14 08:09:34
我真的不想为这样一个简单的函数实现openCV。 – GuyNoir 2011-01-15 23:25:03