This is one of the ways you can decode Pixel colors out of the integers you get from Android Pixels.
ARGB_8888, stands for Alpha, Reg, Green, Blue. The 8’s stand for the number of bits per channel.
In Android, signed
int‘s are used to represent pixel’s alpha/color information.
Since Android’s language of choice is java, these ints are 32-bit integers, each
int takes 4 bytes.
4 bytes = 32 bits = 8bits + 8bits + 8bits + 8bits.
If you had an
0xFFAABBCC, each pair of letters on that hexadecimal would mean the following from left to right
If you’ve done web programming and played with rgb colors and you didn’t know about this, now it all should click on how your web browser represents colors, except in HTML you don’t deal with the alpha value in the front. On Android’s XML you do.
In bits (binary), the
0xFFAABBCC value would look like this:
alpha red green blue
0xFF 0xAA 0xBB 0xCC
255 170 187 204
If you wanted to look at the entire number in binary/bits, it’d be something like this (leaving spaces for visual help):
0b11111111 10101010 10111011 11001100 == 0xFFAABBCC == 4289379276
So if you wanted to get the red channel (
0xAA = 170 = 0b10101010) , you’d have to move the whole thing towards the right 16 places, and then compare it with only the rightmost 8 bits.
So, we shift 16 places to the right, we’d get rid of the 2 bytes on the right side and end up only with the left half
Since we only care about those 8 bits on the right, we do an “&” (bitwise “and”) against
0xFF=255=0b111111111 (all 8 rightmost bits set to 1)
0b11111111 10101010 &
So with simple right bit shifting and “& 0xff” of the shifted value we can extract the values per channel.
This class also features a “multiplyByFloat()” function, which I was using to multiply to each channel as I was operating with convolution kernels while playing with image filters.