How to get pixel's RGB from ByteBuffer with RGB_565 on Android

How to get pixel's RGB from ByteBuffer with RGB_565 on Android



I have ByteBuffer which contains pixels in format RGB_565. ByteBuffer is size 320x200*2. Each pixel is stored in two bytes.



I am getting first byte location for each pixel with (y * 320) + x.



Please how can I obtain RGB from these two bytes and convert it to android color?



I tried convert this post, but without luck



Here is how looks my code, but it gives wrong colors.



EDIT : Thank you friends, I found it. I did two mistakes. First was fixed by Paul, thanks to him I get correct index position. Then I indexed buffer.array() instead buffer.get(index). First way gives me pure byte with some extra bytes at beginning. This made incorrect indexing.



Here is correct function, maybe someone in the future can find it usefull


public int getPixel(int x, int y)
int index = ((y * width) + x) * 2;

//wrong (or add arrayOffset to index)
//byte bytes = buffer.array();
//int rgb = bytes[index + 1] * 256 + bytes[index];

//correct
int rgb = buffer.get(index + 1) * 256 + buffer.get(index);

int r = rgb;
r &= 0xF800; // 1111 1000 0000 0000
r >>= 11; // 0001 1111
r *= (255/31.); // Convert from 31 max to 255 max

int g = rgb;
g &= 0x7E0; // 0000 0111 1110 0000
g >>= 5; // 0011 1111
g *= (255/63.); // Convert from 63 max to 255 max

int b = rgb;
b &= 0x1F; // 0000 0000 0001 1111
//g >>= 0; // 0001 1111
b *= (255/31.); // Convert from 31 max to 255 max

return Color.rgb(r, g, b);



many thanks





where does that ByteBuffer come from?
– pskink
Sep 3 at 9:30


ByteBuffer





I am porting old application to android, ByteBuffer is filled in native code and later converted to opengl texture
– user1063364
Sep 3 at 9:39






i am assuming that those 2 bytes dont contain 5 lowest bits for B, 6 middle bits G and the 5 higher bits foe R? thats why i asked if you are sure that the buffer has valid data
– pskink
Sep 3 at 9:47






I think yes, because following code creates bitmap with correct image : bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); buffer.position(0); bmp.copyPixelsFromBuffer(buffer);
– user1063364
Sep 3 at 9:49





no, seems still gives wrong color.
– user1063364
Sep 3 at 9:57






Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế