![](/img/trans.png)
[英]How to convert BufferedImage(RGB) to BufferedImage(ARGB or RGBA)?
[英]How to convert ARGB to RGB array of bytes?
我有一個字節數組:
byte[] blue_color = {-1,0,112,-64};
如何轉換為RGB字節數組?
還有如何獲得顏色的真實RGB值?
假設它是A組件的第一個元素:
byte[] rgb = Arrays.copyOfRange(blue_color, 1, 4);
要獲得“真實的”顏色值,您需要撤消兩者的補碼表示形式:
int x = (int)b & 0xFF;
如何將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
做得更好。
如何轉換為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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.