Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of the method getPixels for a Bitmap in Android

How do I interpret the returned array from build-in method getPixels for a Bitmap?

Here is my code:

public void foo() {
    int[] pixels;
    Bitmap bitmapFoo = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.test2);             
    int height = bitmapFoo.getHeight();
    int width = bitmapFoo.getWidth();

    pixels = new int[height * width];

    bitmapFoo.getPixels(pixels, 0, width, 1, 1, width - 1, height - 1);     
}

The array "pixels" gets returned with values from -988,602,635 to 1,242,635,509 and that was just from a few colors on a simple PNG file I made. How can I interpret the numbers that get returned from this method?

Edit: I realize this single integer represents a color. I just don't understand how to interpret this single integer into the RBG and alpha values that make up the color.

Thanks.

PS. If your asking yourself, "what is he trying to do?" I am trying to figure out a way to dynamically modify the color of a bitmap.

like image 310
user432209 Avatar asked Nov 13 '10 14:11

user432209


2 Answers

You can also do the following to retrieve colors from an int :

int mColor = 0xffffffff;

int alpha = Color.alpha(mColor);
int red = Color.red(mColor);
int green = Color.green(mColor);
int blue = Color.blue(mColor);
like image 169
Natie Klopper Avatar answered Oct 03 '22 02:10

Natie Klopper


It returns an int for the Color class.

The Color class defines methods for creating and converting color ints. Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue. The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components. The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution. Thus opaque-black would be 0xFF000000 (100% opaque but no contributes from red, gree, blue, and opaque-white would be 0xFFFFFFFF

For example, when you use the Paint object:

Paint pRed = new Paint();
pRed.setColor(Color.RED);

setColor expects an int. Color.RED is that int value for their pre-defined meaning of "red".

like image 42
Bryan Denny Avatar answered Oct 03 '22 02:10

Bryan Denny