Reputation: 13
I wanted to manipulate the image by playing with the pixel bits. So, I wanted to covert the pixels I grabbed from PixelGrabber. The argb value were in bytes. Now I want to convert array of bytes into bits and manipulate it. And then convert back to bytes array.
For Example: -1057365 into 11101111 11011101 10101011 11111111 and 11101111 11011101 10101011 11111111 into -1057365
Anyone know there's any efficient way to converting between them? Or java has method implemented for it and I don't know.
Thx for helping.
Upvotes: 1
Views: 2525
Reputation: 44808
You might want to take a look at BitSet
.
byte[] argb = ...
BitSet bits = BitSet.valueOf(argb);
bits.set(0); // sets the 0th bit to true
bits.clear(0); // sets the 0th bit to false
byte[] newArgb = bits.toByteArray();
/edit
To convert a byte[]
to an int
:
int i = 0;
for(byte b : newArgb) { // you could also omit this loop
i <<= 8; // and do this all on one line
i |= (b & 0xFF); // but it can get kind of messy.
}
or
ByteBuffer bb = ByteBuffer.allocate(4);
bb.put(newArgb);
int i = bb.getInt();
Upvotes: 0
Reputation: 199
I Assume that the value that you have is a raw 4-byte int representation of the ARGB code. Each of the channels is 1 byte wide ranging from 0 to 254, together they make up the whole range of 0-255^4 (minus 1).
The best way you can acquire the different channel values is by combination of masking and shifting the argb value into different fields.
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
Upvotes: 4