0

I have a bitmap and I am trying to get the the data to be an rgba byte array. I can do bmp.getPixels but this is not in rgba bytes. My end destination was to feed this to an HTML5 canvas which requires an 4 elements (r, g, b, a) per pixel. Is this possible?

This is the code I am using to get my bmp:

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
// can do bmp.getPixels here but it doesnt give RGBA :(

I tried this:

bmp.copyPixelsToBuffer(ByteBuffer.wrap(buffer));

but the contents of buffer is extremely weird, it doesnt look like rgba to me, it looks like this:

[ 54, 59, 64, -1, 54, 59, 64, -1,

Those -1s especially look weird.

Noitidart
  • 35,443
  • 37
  • 154
  • 323

3 Answers3

2

Each element of the pixel array is an int, containing argb values. You can get values like that:

int alpha = Color.alphpa(pixel);
int red = Color.red(pixel);
int green = Color.green(pixel);
int blue = Color.blue(pixel);
Simon Marquis
  • 7,248
  • 1
  • 28
  • 43
1

Question was already asked here, I believe it's a hint to yours.

how to get RGB values of bitmap in android

Community
  • 1
  • 1
scarto
  • 94
  • 6
0

So the RGBA bitwise manipulation out of pixel worked.

But copyPixelsToBuffer also worked, the only issue was I had to change the alpha from -1 to 255 and it matched the RGBA bitwise manip method too.

Thanks all for the help!

Noitidart
  • 35,443
  • 37
  • 154
  • 323