2010-12-05 59 views
5

我通過轉換INT顏色爲int成分

int color = image.getRGB(x,y); 

然後我想單獨購買的紅,綠,藍成分獲得像素的顏色。怎麼做?也許使用一些位掩碼?

int green = color&0x00ff00; 

顯然不工作... :(

回答

3

你忘了字節右移:

int green = (color & 0x00ff00) >> 8; 
+0

並確保將兩個字節右移紅色! – ClosureCowboy 2010-12-05 19:10:58

+0

還有一件事。 0x00ff是否等於0x00ff00? – Kamil 2010-12-05 19:13:30

26

爲了讓你可以使用顏色分量:

import android.graphics.Color; 

     int r = Color.red(intColor); 
     int g = Color.green(intColor); 
     int b = Color.blue(intColor); 
     int a = Color.alpha(intColor); 
4
int value = image.getRGB(x,y); 
R = (byte)(value & 0x000000FF); 
G = (byte)((value & 0x0000FF00) >> 8); 
B = (byte)((value & 0x00FF0000) >> 16); 
A = (byte)((value & 0xFF000000) >> 24); 

可能需要翻轉過來,將R,A,或B。