2013-08-07 48 views
2

我的應用程序捕獲圖像並應用濾鏡來修改圖像的RGB值。如何顯示圖像的RGB值的直方圖?

修改完成後,我希望在圖像本身的頂部顯示每種顏色(紅色,綠色,藍色)的直方圖。

我已經知道如何獲得RGB值,我已經知道如何獲得位圖,我只是不知道如何繪製它們。對於RGB值

代碼:

int[] pixels = new int[width*height]; 
    int index = 0; 
    image.getPixels(pixels, 0, width, 0, 0, width, height); 
    Bitmap returnBitmap = Bitmap.createBitmap(width, height, 
      Bitmap.Config.ARGB_8888); 

    for (int x = 0; x < width; x++) { 
     for (int y = 0; y < height; y++) { 
      A = (pixels[index] >> 24) & 0xFF; 
      R = (pixels[index] >> 16) & 0xFF; 
      G = (pixels[index] >> 8) & 0xFF; 
      B = pixels[index] & 0xFF; 
          ++index; 

        } 
      } 

回答

5

我們做了類似的事情。我們得到的圖像與位圖:

Bitmap bmp = BitmapFactory.decodeResource(<youImageView>.getResources(), R.drawable.some_drawable); 

然後,我們的每一個像素迭代,並用下面這段代碼來獲取像素的顏色:

int color = bmp.getPixel(i, j); 
int[] rgbValues = new int[]{ 
       (color >> 16) & 0xff, //red 
       (color >> 8) & 0xff, //green 
       (color  ) & 0xff //blue 
      }; 

編輯:
我剛剛閱讀,你也可以通過使用這種紫斑得到不透明度:

int color = bmp.getPixel(i, j); 
int[] rgbValues = new int[]{ 
       (color >> 24) & 0xff, //alpha 
       (color >> 16) & 0xff, //red 
       (color >> 8) & 0xff, //green 
       (color  ) & 0xff //blue 
      }; 

如果您已經有了這些值,我會建議您使用androidplot來創建圖表。有一些例子使它易於使用。我沒有使用條形圖,但折線圖工作正常。 Here是androidplot的BarCharts的一個例子。
我只是總結了不同的值,然後(如果你想)正常化它。


要最終顯示的圖表,您可以創建佈局爲FrameLayout裏,然後this可以幫助你工作的Z順序。你現在唯一需要做的就是顯示/隱藏佈局的一部分,包含你的圖形。 (View.setVisibility

+0

嗨,看到我更新的問題,我已經知道如何獲得RGB值,我只是不知道如何根據捕獲的圖像繪製它們 – User1204501

+0

嘿,請看我編輯的答案。希望這可以幫助。 – MalaKa