Android 中位图 getPixels 方法的说明

发布于 2024-10-02 10:41:40 字数 640 浏览 3 评论 0原文

如何解释位图的内置方法 getPixels 返回的数组?

这是我的代码:

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

返回的数组“pixels”的值从 -988,602,635 到 1,242,635,509,这只是我制作的简单 PNG 文件上的几种颜色。如何解释从此方法返回的数字?

编辑:我意识到这个单个整数代表一种颜色。我只是不明白如何将这个单个整数解释为构成颜色的 RBG 和 alpha 值。

谢谢。

附言。如果你问自己:“他想做什么?”我正在尝试找出一种动态修改位图颜色的方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

皇甫轩 2024-10-09 10:41:40

您还可以执行以下操作来从中检索颜色
一个整数:

int mColor = 0xffffffff;

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

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);
残疾 2024-10-09 10:41:40

它返回 Color 类的 int。

Color 类定义了以下方法:
创建和转换颜色整数。
颜色表示为压缩整数
由 4 个字节组成:alpha、red、green、
蓝色的。这些值未预乘,
意味着存储任何透明度
仅在 alpha 分量中,而不是
在颜色成分中。这
组件存储如下
(alpha << 24)| (红色<<16)| (绿色
<强><< 8)|蓝色。各成分范围
0..255 之间,其中 0 表示否
对该组成部分的贡献,以及
255 表示 100% 贡献。因此
不透明黑色将为 0xFF000000(100%
不透明但没有红色的贡献,
绿色、蓝色和不透明白色将是
0xFFFFFFFF

例如,当您使用 Paint 对象时:

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

setColor 需要一个 int。 Color.RED 是预定义的“红色”含义的 int 值。

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".

请止步禁区 2024-10-09 10:41:40

更:

int alpha=argb>>24;
int red=(argb & 0x00FF0000)>>16;
int green=(argb & 0x0000FF00)>>8;
int blue=(argb & 0x000000FF);

Even more:

int alpha=argb>>24;
int red=(argb & 0x00FF0000)>>16;
int green=(argb & 0x0000FF00)>>8;
int blue=(argb & 0x000000FF);
枫以 2024-10-09 10:41:40

如果您有 alpha、红色、绿色和蓝色值,则颜色等于 (alpha << 24) + (红色 << 16) + (绿色 << 8) + 蓝色。

要从 int 检索 alpha、红色、绿色和蓝色值,请说 argb:

int alpha=argb>>24;
int red=(argb-alpha)>>16;
int green=(argb-(alpha+red))>>8;
int blue=(argb-(alpha+red+green));

If you have your alpha, red, green and blue values, your in color is equal to (alpha << 24) + (red << 16) + (green << 8) + blue.

To retrieve your alpha, red, green and blue values from an int, say argb:

int alpha=argb>>24;
int red=(argb-alpha)>>16;
int green=(argb-(alpha+red))>>8;
int blue=(argb-(alpha+red+green));
猫腻 2024-10-09 10:41:40

除此之外,我想应该是

bitmapFoo.getPixels(pixels, 0, width, 0, 0, width, height); 

坐标从0开始吧?

Besides that, I think it should be

bitmapFoo.getPixels(pixels, 0, width, 0, 0, width, height); 

The coordinates start from 0, right?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文