3

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?

Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • Did you ever find a solution to your problem as I was also wondering the same thing? – TomP89 Jul 10 '12 at 21:02
  • 1
    @TomP89 Yes, it is much easier than .NET LockBits, just use 2 methods I mentioned in the question, it will copy your bitmap color data to an array and vice versa. – Luke Vo Jul 11 '12 at 12:18

1 Answers1

4

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);