在Java中更改图片颜色的问题
我得到了一些从rgb值计算R、G和B值的代码。它们看起来像这样:在Java中更改图片颜色的问题,java,image,colors,Java,Image,Colors,我得到了一些从rgb值计算R、G和B值的代码。它们看起来像这样: public static int getR(int rgb) { return (rgb >> 16) & 0xff; } public static int getG(int rgb) { return (rgb >> 8) & 0xff; } public static int getB(int rgb) { return rgb &
public static int getR(int rgb) {
return (rgb >> 16) & 0xff;
}
public static int getG(int rgb) {
return (rgb >> 8) & 0xff;
}
public static int getB(int rgb) {
return rgb & 0xff;
}
现在我必须做以下练习。我正在更改图像的正确部分,但所有内容都是黑色的
/**
* Converts a picture by dividing it in three equal parts along the X axis.
* In the first (left) part, only the red component is drawn. In the second
* (middle) part, only the green component is drawn. In the third (right) part,
* only the blue component is drawn.
*
* @param pixels The input pixels.
* @return The output pixels.
*/
public static int[][] andyWarhol(int[][] pixels) {
int i, j;
//Convert red part:
for (i = 0; i < pixels.length / 3; i++) {
for (j = 0; j < pixels[0].length; j++) {
pixels[i][j] = Colors.getR(pixels[i][j]);
}
}
//Convert yellow part:
for (i = pixels.length / 3; i < pixels.length * 2 / 3; i++) {
for (j = 0; j < pixels[0].length; j++) {
pixels[i][j] = Colors.getG(pixels[i][j]);
}
}
//Convert blue part:
for (i = pixels.length * 2 / 3; i < pixels.length; i++) {
for (j = 0; j < pixels[0].length; j++) {
pixels[i][j] = Colors.getB(pixels[i][j]);
}
}
return pixels;
}
/**
*通过沿X轴将图片等分为三个部分来转换图片。
*在第一个(左)零件中,仅绘制红色零部件。第二
*(中间)零件,仅绘制绿色零部件。在第三部分(右)中,
*仅绘制蓝色组件。
*
*@param pixels输入像素。
*@返回输出像素。
*/
公共静态int[]]andyWarhol(int[][]像素){
int i,j;
//转换红色部分:
对于(i=0;i
我怀疑您的alpha为0(透明)。
我不知道它是如何使用的,但是java中的颜色通常是“argb”。
尝试使用(颜色| 0xff_00_00_00)解决。
好的,是的,正如我所怀疑的,我必须把R,B和G的每一个值再次转换成rbg值。有意义您能同时提供getG()和getB()吗?我将其添加到原始文本中,不知道如何在注释中执行。我觉得getR()等函数的返回值不是您需要写入图像数组的数字类型,这是您应该添加它的地方:-)