【问题标题】:issue changing the color of a picture in Java在Java中更改图片颜色的问题
【发布时间】:2018-04-28 14:15:06
【问题描述】:

我得到了一些代码,用于从 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 & 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;
    }

【问题讨论】:

  • 能否也提供getG()和getB()?
  • 我把它加到原文里了,不知道怎么在cmets里做。我觉得 getR() 等函数的返回不是您需要写入图像数组的数字类型
  • 那是你应该添加它的地方:-)

标签: java image colors


【解决方案1】:

我怀疑您的 alpha 为 0(透明)。 我不知道它是如何使用的,但是java中的颜色通常是'argb'。 试试 (color | 0xff_00_00_00)

【讨论】:

    【解决方案2】:

    解决了。 好吧,是的,因为我怀疑我必须将 R、B 和 G 的每个值再次转换为 rbg 值。有道理

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2015-03-28
      • 1970-01-01
      相关资源
      最近更新 更多