How to Get the Rgb Values from a Bitmap on Android

How do you get the RGB values from a Bitmap on Android?

This may be slightly late, but to clear up the confusion with the use of &0xff:

In Java ints are 32 bits, so the (A)RGB values for each pixel are packed in 4 bytes.
In other words, a pixel with the values R(123), G(93), B(49) = FF7B 5D31 in the ARGB_8888 model. Where Alpha = FF, R = 7B, G = 5D, B = 31. But this is stored as an int as -8692431.

So, to extract the Green value from -8692431, we need to shift the 5D by 8 bits to the right, as you know. This gives 00FF 7B5D. So, if we were just to take that value we would be left with 16743261 as our Green value. Therefore, we bitwise-and that value with the mask of 0xFF (which is equivalent to 0000 00FF) and will result in 00FF 7B5D being 'masked' to 0000 005D. So we have extracted our Green value of 5D (or 93 decimal).

We can use the same mask of 0xFF for each extraction because the values have all been shifted to expose the desired two bytes as the least significant. Hence the previously suggested code of:

int p = pixel[index];

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

If it makes it clearer, you can perform the equivalent operation of:

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

For brevity, the extra 0s can be dropped, and it can be written as

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

Note however, that alternative colour models may be used, such as RGB_555 which stores each pixel as just 2 bytes, with varying precision for the RGB channels. So you should check the model that your bitmap is using before you perform the extraction, because the colours may be stored differently.

How to get average RGB value of Bitmap on Android?

I think below code for exact answer to you.
Get the Average(Number of pixels)of Red, Green and Blue value for the given bitmap.

Bitmap bitmap = someBitmap; //assign your bitmap here
int redColors = 0;
int greenColors = 0;
int blueColors = 0;
int pixelCount = 0;

for (int y = 0; y < bitmap.getHeight(); y++)
{
for (int x = 0; x < bitmap.getWidth(); x++)
{
int c = bitmap.getPixel(x, y);
pixelCount++;
redColors += Color.red(c);
greenColors += Color.green(c);
blueColors += Color.blue(c);
}
}
// calculate average of bitmap r,g,b values
int red = (redColors/pixelCount);
int green = (greenColors/pixelCount);
int blue = (blueColors/pixelCount);

How to get the RGB values from am imageview in android

The problem is you are writing code in wrong place you will get ImageView object in onActivityCreated method instead of onStart()

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);

/* Write code to get reference of ImageView and Get value of Bitmap RGB here */

}

Update :

that means ImageView Instance is not null but the drawable is

you are setting Background drawable and you need to access it like below

Drawable background = view.getBackground();
if (background instanceof BitmapDrawable)
{
Bitmap bitmap = ((BitmapDrawable)background).getBitmap();
}

Android/Tensorflow converting Bitmap.getPixels to RGB pixel values results in colours not being correct

Turned out that the RGB channels were reversed by Bitmap.getPixels so changing to BGR worked.

floatValues[i * 3 + 2] = Color.red(val);
floatValues[i * 3 + 1] = Color.green(val);
floatValues[i * 3] = Color.blue(val);

Saving RGB of a bitmap

Is there any good reason not to use short that I am missing here?

In most cases int's, four channels per int, are better.

Is it not worth casting the value to short?

It is not worth.

TL;NR

It depends a lot on what you do with the values:

  1. If you stash them in a short[], this will be more memory efficient than stashing them in a int[], assuming you use one int per channel color value. Also, consider what JeppeSRC said: you can use just byte[] and, when you use the value, you convert back to int with zero-extension like this:

    int channelColorValue = (int) myBytes[i] & 0xff;

    This will prevent using erroneous negative values.

  2. If you use only a couple of local variables, it does not even matter, but keep in mind that Java, at the bytecode level, performs operations only on 32 or 64 integers, see this. In this case there is no memory gain using short instead of int, both will take 32 bits.

  3. The most memory efficient solution is actually to keep all values in a int[], 4 channels per int (alpha, red, green, blue). You can unpack the int and select a single channel color value using bit manipulation, which is exactly what the methods you use do. Example: int alpha = color >>> 24;

Android BitmapFactory trimming color values from ARGB png

Since Android system internally handles bitmap image as premultiplied bitmap by default, all the pixels (stored in the file in non-premultiplied format) are converted to premultiplied pixels while decoding. So, RGB values become 0 where A is 0. If you'd like keep bitmap in non-premultiplied format, just specify BitmapFactory.Options.inPremultiplied=false.

val options = BitmapFactory.Options().apply {
inPreferredConfig = Bitmap.Config.ARGB_8888
inPremultiplied = false
}

originalImage = BitmapFactory.decodeResource(context.resources, R.drawable.img, options)
originalImage.setHasAlpha(true)


Related Topics



Leave a reply



Submit