我有一个类像素,它具有方法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