我有一個類像素,它具有方法getRed,getBlue,getGreen,setRed,setBlue,setGreen(r,g,b) 並且到目前爲止所有東西都單獨工作,但是當我把它放在一起時setGreen和setBlue不工作。我是否在掩蔽方面做錯了什麼?在java中編輯像素值
public class Pixel {
int pixel;
public Pixel (int pixel) {
this.pixel = pixel;
}
public int getRed() {
int red = pixel >> 16;
red = red & (0xFF);
return red;
}
public int getGreen() {
int green = pixel >> 8;
green = green & (0xFF);
return green;
}
public int getBlue() {
int blue = pixel;
blue = blue & (0xFF);
return blue;
}
public void setRed(int red) {
pixel = (pixel & ~(0xFFFF0000)) << 16;
pixel= ((red << 16))|pixel ;
}
public void setGreen(int value) {
pixel = (pixel & ~(0xFF00FF00)) << 8;
pixel= (value << 8) |pixel;
}
public void setBlue(int value) {
pixel = (pixel & ~(0xFFFFFF00));
pixel= (value) |pixel;
}
public static void main(String[] args) {
Pixel p3 = new Pixel(0xFF000000);
System.out.printf("rgb = (%d, %d, %d)\n", p3.getRed(), p3.getGreen(), p3.getBlue());
p3.setRed(42);
p3.setGreen(18);
p3.setBlue(225);
System.out.printf("rgb = (%d, %d, %d)\n", p3.getRed(), p3.getGreen(), p3.getBlue());
p3.setRed(-1);
p3.setGreen(500);
p3.setBlue(1000);
System.out.printf("rgb = (%d, %d, %d)\n", p3.getRed(), p3.getGreen(), p3.getBlue());
}
}
我應該得到這個作爲我的結果:
rgb = (0, 0, 0)
rgb = (42, 18, 225)
rgb = (255, 244, 232)
,但是這是即時得到
rgb = (0, 0, 0)
rgb = (0, 0, 225)
rgb = (0, 3, 232)
你怎麼想在你的'int'位佈局?它應該是標準的'RRGGBB'(其中一個字母表示四位)? – 5gon12eder 2014-10-06 01:53:41
一個像素是一個32位的int值,可以解釋爲四個8位值的序列。第一個8位值是alpha通道。我們會忽略這一點。接下來的三個8位值分別是像素的紅色,綠色和藍色分量。 – Panthy 2014-10-06 01:54:14
他們是8位而不是4我相信 – Panthy 2014-10-06 01:56:39