【问题标题】:How to get count of pixels red color on bitmap android如何在位图android上获取像素红色的计数
【发布时间】:2017-06-18 06:50:15
【问题描述】:

我想获得位图图像上所有红色像素的数量,在我绘制它并与背面图像合并之后。 我怎样才能做到这一点?请给我更多的细节,我将不胜感激,谢谢!

Example: Count of red pixels

【问题讨论】:

    标签: java android canvas bitmap


    【解决方案1】:

    遍历位图中的每个像素

    //define your red
    static final int RED = Color.RED;
    
    //counting
    int count = 0;
    for (int x = 0; x <= myBitmap.getWidth(); x++) {
        for (int y = 0; x <= myBitmap.getHeight(); y++) {
            if(myBitmap.getPixel(x, y))
                count++;
        }
    }
    //done. use the variable count
    

    【讨论】:

      【解决方案2】:

      您有一个Bitmap,您可以使用以下代码从中获取像素颜色:

      int countX = bitmap.getWidth();
      int countY = bitmap.getHeight();
      int redCount = 0;
      
      for (int x = 0; x < countX; x++) {
          for (int y = 0; y < countY; y--) {
              int colorXY = bitmap.getPixel(x, y);
              redCount += Color.red(colorXY);
          }
      }
      

      【讨论】:

        【解决方案3】:

        我得到了这样的东西:

            int countX = bm.getWidth();
            int countY = bm.getHeight();
        
            int redCount = 0;
        
            for (int rangeX = 0; rangeX < countX; rangeX++) {
                for (int rangeY = 0; rangeY < countY; rangeY++) {
                    int colorXY = bm.getPixel(rangeX, rangeY);
        
                    int r = Color.red(colorXY);
                    int g = Color.green(colorXY);
                    int b = Color.blue(colorXY);
        
                    if(Color.rgb(r,g,b) == Color.RED) {
                        redCount++;
                        /*bm.setPixel(rangeX,rangeY,Color.GREEN);*/
                    }
                }
            }
        

        【讨论】:

        • 为什么缺少导入?
        猜你喜欢
        • 1970-01-01
        • 2013-06-04
        • 2017-09-12
        • 2018-02-22
        • 2019-02-07
        • 1970-01-01
        • 2014-07-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多