Java Android中Bitmap的getPixels方法说明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4172917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Explanation of the method getPixels for a Bitmap in Android
提问by user432209
How do I interpret the returned array from build-in method getPixels for a Bitmap?
如何解释位图的内置方法 getPixels 返回的数组?
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?
数组“像素”返回的值从 -988,602,635 到 1,242,635,509,这只是我制作的简单 PNG 文件中的几种颜色。如何解释从该方法返回的数字?
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.
编辑:我意识到这个单个整数代表一种颜色。我只是不明白如何将这个单个整数解释为构成颜色的 RBG 和 alpha 值。
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.
附注。如果你问自己,“他想做什么?” 我试图找出一种动态修改位图颜色的方法。
采纳答案by Bryan Denny
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
Color 类定义了创建和转换颜色整数的方法。颜色表示为压缩整数,由 4 个字节组成:alpha、red、green、blue。这些值是未预乘的,这意味着任何透明度都单独存储在 alpha 分量中,而不是存储在颜色分量中。组件存储如下 (alpha << 24) | (红色<<16) | (绿色<< 8) | 蓝色。每个组件的范围在 0..255 之间,0 表示该组件没有贡献,255 表示 100% 贡献。因此不透明黑色将是 0xFF000000(100% 不透明但没有来自红色、绿色、蓝色和不透明白色的贡献是 0xFFFFFFFF
For example, when you use the Paint object:
例如,当您使用 Paint 对象时:
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".
setColor
期望一个整数。Color.RED 是其预定义的“红色”含义的 int 值。
回答by Alexis Dufrenoy
If you have your alpha, red, green and blue values, your in color is equal to (alpha << 24) + (red << 16) + (green << 8) + blue.
如果您有 alpha、红色、绿色和蓝色值,则颜色等于 (alpha << 24) + (red << 16) + (green << 8) + blue。
To retrieve your alpha, red, green and blue values from an int, say argb:
要从 int 中检索您的 alpha、red、green 和 blue 值,请说 argb:
int alpha=argb>>24;
int red=(argb-alpha)>>16;
int green=(argb-(alpha+red))>>8;
int blue=(argb-(alpha+red+green));
回答by JohnUopini
Even more:
更:
int alpha=argb>>24;
int red=(argb & 0x00FF0000)>>16;
int green=(argb & 0x0000FF00)>>8;
int blue=(argb & 0x000000FF);
回答by Jonathan Natie Klopper
You can also do the following to retrieve colors from an int :
您还可以执行以下操作以从 int 检索颜色:
int mColor = 0xffffffff;
int alpha = Color.alpha(mColor);
int red = Color.red(mColor);
int green = Color.green(mColor);
int blue = Color.blue(mColor);
回答by Phuah Yee Keat
Besides that, I think it should be
除此之外,我觉得应该是
bitmapFoo.getPixels(pixels, 0, width, 0, 0, width, height);
The coordinates start from 0, right?
坐标是从0开始的吧?