从 BItmap 中提取 byte[] 中的像素颜色值

Extracting pixcel color values in byte[] from BItmap

背景

我正在编写一个简单的应用程序,其中应用程序以 RGB_565 格式从位图中提取像素颜色,并通过 BLE

将其发送到蓝牙设备

我得到 argb 格式的 int[] 颜色,我想要 RGB_565 格式 所以我从 Color.red(-10267343) 中提取了红色、绿色、蓝色,其中 -10267343 是我从 getPixel(x,y)

中获得的像素的颜色

我得到了

red : 99
green : 85
blue : 99 //from the above color value -10267343

我的问题是如何在两个字节中添加这些红色、绿色、蓝色

我需要这种格式|R|R|R|R|R|G|G|G|G|G|G|B|B|B|B|B|

到目前为止我试过这个方法

byte[] colorToByte(int c){
  int r = (c >> 16) & 0xFF;
  int g = (c >> 8)  & 0xFF;
  int b =  c        & 0xFF;
  return new byte[]{(byte)((r&248)|g>>5),(byte)((g&28)<<3|b>>3)};
}

如本回答中所建议的那样 How to correctly convert from rgb565 to rgb888

我也试过这个答案,但没有成功 Java image conversion to RGB565

有什么办法可以解决这个问题吗?任何帮助表示赞赏

private static byte[] colorToByte(int c){
        int rgb = c;
        int blue = rgb & 0xFF;
        int green = (rgb >> 8) & 0xFF;
        int red = (rgb >> 16) & 0xFF;

        int r_565 = red >> 3;
        int g_565 = green >> 2;
        int b_565 = blue >> 3;
        int rgb_565 = (r_565 << 11) | (g_565 << 5) | b_565;

        return new byte[]{(byte) ((rgb_565 >> 8) & 0xFF), (byte) (rgb_565 & 0xFF)};

        }