I'm trying to find the best/fastest way to pass a bitmap from java to JNI. Next is how I do this operation at the moment. Can this be improved, or is there any other faster way?
void JNIBitmap::setBuffer(JNIEnv* env, const jobject bitmap)
{
// Allocate native pixels buffer
AndroidBitmapInfo bitmapInfo;
AndroidBitmap_getInfo(env, bitmap, &bitmapInfo)
this->bufferWidth = bitmapInfo.width;
this->bufferHeight = bitmapInfo.height;
this->bufferSize = this->bufferWidth * this->bufferHeight * 4;
this->buffer = new uint8_t[this->bufferSize];
// Copy pixels to native buffer
void* bitmapPixels;
AndroidBitmap_lockPixels(env, bitmap, &bitmapPixels)
memcpy((void*)this->buffer, bitmapPixels, this->bufferSize);
AndroidBitmap_unlockPixels(env, bitmap);
}
解决方案
The easiest way is to just call AndroidBitmap_lockPixels.
So in the constructor of JNIBitmap you would do this:
void* bitmapPixels;
AndroidBitmap_lockPixels(env, bitmap, &bitmapPixels)
Then in the destructor you would do this:
AndroidBitmap_unlockPixels( env, bitmap);