Bitmap LockBits on Android?
-
12-04-2021 - |
Pregunta
My program on Android uses an algorithm that uses a lot of setPixel and getPixel, therefore, it's very slow. On .NET, I can use LockBits to make it faster. Is there LockBits or similar on Java or Android?
EDIT: After some searches, I found copyPixelToBuffer
and copyPixelFromBuffer
, wonder if it is what I need?
Solución
Yes, you should use the above two methods and make use of a ByteBuffer object where you will be first storing all the bitmap data. After doing so, copy all the buffer data into a byte array and then you can do all you argb manipulations within this array. After all done, wrap this byte array into a newly allocated ByteBuffer and then finally copy the pixels back from this buffer into the original bitmap. Here's some sample: "bmpData" is your Bitmap object holding image pixel data.
int size = bmpData.getRowBytes()*bmpData.getHeight()*4;
ByteBuffer buf = ByteBuffer.allocate(size);
bmpData.copyPixelsToBuffer(buf);
byte[] byt = buf.array();
for(int ctr=0;ctr<size;ctr+=4)
{
//access array in form of argb. for ex. byt[0] is 'r', byt[1] is 'g' and so on..
}
ByteBuffer retBuf = ByteBuffer.wrap(byt);
bmpData.copyPixelsFromBuffer(retBuf);