【问题标题】:How to convert ARGB to RGB array of bytes?如何将 ARGB 转换为 RGB 字节数组?
【发布时间】:2012-05-16 15:35:53
【问题描述】:

我有一个字节数组:

byte[] blue_color = {-1,0,112,-64};

如何转换成RGB的字节数组?

还有如何获得颜色的真实 RGB 值?

【问题讨论】:

  • 你应该知道什么是ARGB,什么是RGB...
  • 不,你问的是完全不同的东西。
  • 对不起,我的意思是将 argb 颜色表示为字节数组,并将其转换为真正的 rgb 值。
  • 什么是真正的 rgb 值?

标签: java rgb argb


【解决方案1】:

假设它是 A 组件的第一个元素:

byte[] rgb = Arrays.copyOfRange(blue_color, 1, 4);

要获得“真实”的颜色值,您需要撤消二进制补码表示:

int x = (int)b & 0xFF;

【讨论】:

  • 没有copyOfRange方法(我用java 1.5)
  • @kenny 然后使用System.arraycopy()
  • 想跳过一个元素时如何使用System.arraycopy
  • 但是这种颜色的 rgb 实际值是多少?
  • @Sulthan System.arraycopy(blue_color, 1, rgb, 0, 3);
【解决方案2】:

如何将 ARGB 数组转换为 RGB?


byte[] argb = ...;
byte[] rgb = new byte[(argb.length / 4) * 3];

int index = rgb.length - 1;

for (int i = argb - 1; i >= 0; i -= 4) {
  rgb[index--] = argb[i];
  rgb[index--] = argb[i - 1];
  rgb[index--] = argb[i - 2];
}

如何打印整数值:



byte[] oneColor = {..., ..., ..., ...};

int alpha = oneColor[0] & 0xFF;
int red = oneColor[1] & 0xFF;
int green = oneColor[2] & 0xFF;
int blue = oneColor[3] & 0xFF;

System.out.println("Color: " + alpha + ", " + red + ", " + green ", " + blue);

System.out.println("Hexa color: 0x" + Integer.toHexString(alpha) + " " + Integer.toHexString(red) + " " + Integer.toHexString(green) + " " + Integer.toHexString(blue));

可以用printf 做得更漂亮。

【讨论】:

    【解决方案3】:

    如何转换成RGB的字节数组?

    byte[] rgb = new byte[3];
    System.arraycopy(blue_color, 1, rgb, 0, 3);
    

    还有如何获得颜色的真实 RGB 值?

    int red = rgb[0] >= 0 ? rgb[0] : rgb[0] + 256;
    int green = rgb[1] >= 0 ? rgb[1] : rgb[1] + 256;
    int blue = rgb[2] >= 0 ? rgb[2] : rgb[2] + 256;
    

    【讨论】:

    • 他们不是。 256 必须添加到负值。
    • @OliCharlesworth 哎呀,我的意思是+ 256 ;)
    猜你喜欢
    • 2011-01-04
    • 2022-10-21
    • 2011-07-27
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2012-07-20
    相关资源
    最近更新 更多