【问题标题】:How to make if statement with color code如何使用颜色代码制作 if 语句
【发布时间】:2012-02-08 13:51:11
【问题描述】:

我有一个小问题,我想,不会很难。 但不知何故,我想念那个“不能太难”的部分:)。

我有一个代码,可以读取图像的颜色:

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.four_colors);


    //define the array size
    rgbValues = new int[bmp.getWidth()][bmp.getHeight()]; 

    //Print in LogCat's console each of one the RGB and alpha values from the 4 corners of the image
    //Top Left
    for (int j = 0; j < 1000; j++){
        for (int k = 0; k < 1000; k++){
    Log.i("Pixel Value", "pixel x="+j +k + Integer.toHexString(bmp.getPixel(j, k)));
    test = Integer.toHexString(bmp.getPixel(j,  k));


        }
    }

    //get the ARGB value from each pixel of the image and store it into the array
    for(int i=0; i < bmp.getWidth(); i++)
    {
        for(int j=0; j < bmp.getHeight(); j++)
        {
            //This is a great opportunity to filter the ARGB values
            rgbValues[i][j] = bmp.getPixel(i, j);               
        }   
    }

    System.out.println(PixelNumber);
    System.out.println(test);
}

这段代码就像一个魅力。 但我试图实现一个 if 语句,如:

if (color == black){
    number++;
}

所以他看到的每个像素都是黑色的,他必须将数字加1。

尝试了几乎所有我能想出的方法来围绕这段代码做一个好的 if 语句,但似乎没有任何效果。 那么在这段代码中正确的地方和 if 语句是什么?

也许如果您有更多时间(如果您不想要,请不要在这方面投入太多精力)是否可以检查颜色范围而不是仅检查一种颜色,因此它会选择所有绿色。 (与 Photoshop 中的颜色范围相同)。

【问题讨论】:

  • 从描述中无法理解您在哪里尝试了 if 语句,但在您的其他代码中的任何地方都没有提到可变颜色。

标签: java android colors pixel


【解决方案1】:

getPixel 以整数形式返回 4 字节的 ARGB。你应该做的是组成你想要计算的颜色(Colorargb 方法会帮助你)并用getPixel(x, y) 测试这个值。
对于黑色,这很容易:

if(Color.BLACK == bmp.getPixel(x, y))
     //doit

对于范围,您可以使用green/red/blue 方法,每个方法都返回该组件的值。如果它不为零,则像素具有来自该组件的颜色。

【讨论】:

  • 您将intColor 进行比较。这应该会产生编译时错误。
  • Color.BLACK 是一个 int 常量。查看文档:developer.android.com/reference/android/graphics/…
  • @Andreas_D Color.BLACKgetPixel() 都是整数
  • 谢谢,如果我现在看一下 if 语句,我可以给自己留个面子。我真的很感谢你的所作所为!
  • 啊,上下文错误 ;) java.awt.Color.BLACKColor 的实例,而 android.graphics.Color.BLACKint ;) 我们在这里谈论的是 android。
【解决方案2】:

您可以使用 if(rgbValues[i][j] == Color.BLACK) 之类的条件。

该代码将如下所示。

 //get the ARGB value from each pixel of the image and store it into the array
    for(int i=0; i < bmp.getWidth(); i++)
    {
        for(int j=0; j < bmp.getHeight(); j++)
        {
            //This is a great opportunity to filter the ARGB values

            rgbValues[i][j] = bmp.getPixel(i, j);   
            if(rgbValues[i][j] == Color.BLACK) {

        }   
    }

【讨论】:

    【解决方案3】:

    Bitmap#getPixel(int, int) 返回一个 int 值,表示给定像素的透明度 (alpha) 和颜色 (rgb)。

    黑色的RGB值为(0x00, 0x00, 0x00)。所以如果你想测试黑色 并且 忽略 alpha 值,测试应该是这样的:

    if ((rgbValue[i][j] & 0x00ffffff) == 0) {
       // this pixel is black
       number++;
    }
    

    颜色范围是很困难的。首先,我们需要 green 的可计算定义(例如)。 green 是相当主观的。一旦你手头有一个算法,把它放在一个不同的方法中,然后像这样调用:

    if (isGreenish(rgbValue[i][j])) {     // magic method
       // this pixel is some sort of green
       number++;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 2020-12-25
      • 1970-01-01
      相关资源
      最近更新 更多