如何从Android设备上的位图获取RGB值?

问题描述 投票:29回答:5

我想在android中获取位图的RGB值,但到目前为止我无法做到这一点。我的目标是获得位图的每个像素的RGB值。 Android或其他任何特定功能?另外我想知道我需要colorMatrix()函数吗?这对我的项目非常重要。谢谢

android image image-processing rgb
5个回答
25
投票

这可能会稍晚,但要清除与使用&0xff的混淆:

在Java中,int是32位,因此每个像素的(A)RGB值以4个字节打包。换句话说,ARGB_8888模型中具有值R(123),G(93),B(49)= FF7B 5D31的像素。其中Alpha = FF,R = 7B,G = 5D,B = 31.但是这被存储为int -8692431。

因此,要从-8692431中提取Green值,我们需要将5D向右移8位,如您所知。这给出00FF 7B5D。因此,如果我们只是采取该值,我们将留下16743261作为我们的绿色值。因此,我们按位 - 并且该值具有0xFF的掩码(相当于0000 00FF)并且将导致00FF 7B5D被“屏蔽”到0000 005D。所以我们提取了5D(或十进制93)的绿色值。

我们可以为每次提取使用相同的0xFF掩码,因为这些值都已被移位以将所需的两个字节暴露为最低有效字节。因此,之前建议的代码:

int p = pixel[index];

int R = (p >> 16) & 0xff;
int G = (p >> 8) & 0xff;
int B = p & 0xff;

如果它更清楚,您可以执行以下等效操作:

int R = (p & 0xff0000) >> 16;
int G = (p & 0x00ff00) >> 8;
int B = (p & 0x0000ff) >> 0;

为简洁起见,可以删除额外的0,并将其写为

int R = (p & 0xff0000) >> 16;
int G = (p & 0xff00) >> 8;
int B = p & 0xff;

但是请注意,可以使用替代颜色模型,例如RGB_555,其将每个像素存储为仅2个字节,具有RGB通道的不同精度。因此,在执行提取之前,应检查位图正在使用的模型,因为颜色可能以不同方式存储。


25
投票

Bitmap.getPixel(x, y)返回一个int,其中嵌入了颜色值和alpha值。

int colour = bitmap.getPixel(x, y);

int red = Color.red(colour);
int blue = Color.blue(colour);
int green = Color.green(colour);
int alpha = Color.alpha(colour);

18
投票

这就是我试图获得这个价值的方式。使用bitmap.getPixel()获取整数数组中的相应位图。通过使用按位旋转操作,我们将获得RGB值。

             int[] pix = new int[picw * pich];
             bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

             int R, G, B,Y;

             for (int y = 0; y < pich; y++){
             for (int x = 0; x < picw; x++)
                 {
                 int index = y * picw + x;
                 int R = (pix[index] >> 16) & 0xff;     //bitwise shifting
                 int G = (pix[index] >> 8) & 0xff;
                 int B = pix[index] & 0xff;

                 //R,G.B - Red, Green, Blue
                  //to restore the values after RGB modification, use 
 //next statement
                 pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
                 }}

7
投票

一句话少说:D

imagen.getPixels(pix, 0, picw, 0, 0, picw, pich);

    for (i = 0; i < pix.length; i++) {
        r = (pix[i]) >> 16 & 0xff;
        g = (pix[i]) >> 8 & 0xff;
        b = (pix[i]) & 0xff;
    }

4
投票

Arbitrary Bitmap Color Handling

你可以在这里阅读各种Color方法,它们将从像素int中提取颜色成分。

您可能希望将过滤器应用于位图,并返回一个字节数组。否则,您可以将此示例缩小到for循环并滚动生成字节数组的像素。

private byte[] rgbValuesFromBitmap(Bitmap bitmap)
{
    ColorMatrix colorMatrix = new ColorMatrix();
    ColorFilter colorFilter = new ColorMatrixColorFilter(
            colorMatrix);
    Bitmap argbBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(argbBitmap);

    Paint paint = new Paint();

    paint.setColorFilter(colorFilter);
    canvas.drawBitmap(bitmap, 0, 0, paint);

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int componentsPerPixel = 3;
    int totalPixels = width * height;
    int totalBytes = totalPixels * componentsPerPixel;

    byte[] rgbValues = new byte[totalBytes];
    @ColorInt int[] argbPixels = new int[totalPixels];
    argbBitmap.getPixels(argbPixels, 0, width, 0, 0, width, height);
    for (int i = 0; i < totalPixels; i++) {
        @ColorInt int argbPixel = argbPixels[i];
        int red = Color.red(argbPixel);
        int green = Color.green(argbPixel);
        int blue = Color.blue(argbPixel);
        rgbValues[i * componentsPerPixel + 0] = (byte) red;
        rgbValues[i * componentsPerPixel + 1] = (byte) green;
        rgbValues[i * componentsPerPixel + 2] = (byte) blue;
    }

    return rgbValues;
}
© www.soinside.com 2019 - 2024. All rights reserved.