2

我的应用程序捕获图像并对其应用过滤器以修改图像 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;

                     }
            }
4

1 回答 1

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
            };

编辑:
我刚刚读到你也可以通过使用这个 insead 来获得不透明度:

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来创建图表。有一些示例使其易于使用。我没有使用条形图,但折线图效果很好。是用于 androidplot 的 BarCharts 示例。
我只想总结不同的值,然后(如果你想)对其进行规范化。


要最终显示图表,您可以将布局创建为 FrameLayout,然后可能会帮助您处理 z 顺序。您现在唯一需要做的就是显示/隐藏包含图形的布局部分。( View.setVisibility)

于 2013-08-07T10:49:28.067 回答