如何在黑莓中实现按位颜色编码

发布于 2024-11-10 14:03:38 字数 1416 浏览 3 评论 0原文

我是黑莓的新手。目前我正在研究位图颜色编码

我为更改图像颜色而实现的代码如下: 位图 greyScale = new Bitmap(original.getWidth(),original.getHeight());

    int[] argb = new int[original.getWidth() * original.getHeight()];

    original.getARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
    for (int i = argb.length - 1; i >= 0; --i)

{ int alpha = argb[i]>>> 24; int red = argb[i] >>> 16& 0xFF; int green = argb[i] >>> 8& 0xFF; int 蓝色 = 255-argb[i] & 0xFF;

         int  grey = (int) (0.3 * red + 0.59 * green + 0.11 * blue);

        int red2 = red * contrast/10+brightness;
        if (red2>0xFF) red2 = 0xFF;
        if (red2<0) red2 = 0;
        int green2 = green * contrast/10+brightness;
        if (green2>0xFF) green2 = 0xFF;
        if (green2<0) green2 = 0;
        int blue2 = blue * contrast/10+brightness;
        if (blue2>0xFF) blue2 = 0xFF;
        if (blue2<0) blue2 = 0;

        int composite = (alpha << 24) | (red2 << 16) | (green2 << 8) | blue2|red|green;
        argb[i] = composite;
    }

    greyScale.setARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());

    return greyScale;
}

我能够实现这种方法并且也能够获得各种颜色。 但任何人都可以帮助我了解 Argb 如何处理整个颜色代码。

问候 平克什·古普塔

i am new to blackberry.Currently i am working upon bitmap color coding

The code which i am implement for changing colors of image are as follows:
Bitmap greyScale = new Bitmap(original.getWidth(), original.getHeight());

    int[] argb = new int[original.getWidth() * original.getHeight()];

    original.getARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
    for (int i = argb.length - 1; i >= 0; --i)

{
int alpha = argb[i] >> 24;
int red = argb[i] >> 16 & 0xFF;
int green = argb[i] >> 8 & 0xFF;
int blue = 255-argb[i] & 0xFF;

         int  grey = (int) (0.3 * red + 0.59 * green + 0.11 * blue);

        int red2 = red * contrast/10+brightness;
        if (red2>0xFF) red2 = 0xFF;
        if (red2<0) red2 = 0;
        int green2 = green * contrast/10+brightness;
        if (green2>0xFF) green2 = 0xFF;
        if (green2<0) green2 = 0;
        int blue2 = blue * contrast/10+brightness;
        if (blue2>0xFF) blue2 = 0xFF;
        if (blue2<0) blue2 = 0;

        int composite = (alpha << 24) | (red2 << 16) | (green2 << 8) | blue2|red|green;
        argb[i] = composite;
    }

    greyScale.setARGB(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());

    return greyScale;
}

I am capable of implementing this method and capable of obtaining various colors also.
But can anyone help me that how Argb is working on the entire code for colors.

regards
Pinkesh Gupta

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

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

发布评论

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

评论(1

胡渣熟男 2024-11-17 14:03:38

谢谢,我正在使用您的代码并进行一些修改。为了回答你的问题,

argb中的每个字节都是一个32位值,代表0xAARRGGBB格式中的一个像素:

  • 8位alpha(透明度):0表示透明,255表示不透明
  • 8位红色
  • 8位绿色
  • 8位蓝色

所以0xFFFFFFFF将是不透明的白色的。

提取值的代码的工作原理如下:
要获得 apha 值,请将所有内容右移 24 位,仅保留前 8 位。

int alpha = argb[i] >> 24

要获得红色,请右移 16 位,保留前 16 位。然后使用 AND 运算符清空前 8 位。这留下了第二个 8 位

int red = argb[i] >> 16 & 0xFF

对于绿色,移动 8 位并空白前 16 位,留下第三个 8 位

int green = argb[i] >> 8 & 0xFF

最后对于蓝色,只需空白前 24 位

int blue = argb[i] & 0xFF;

此时,您可以更改 alpha,红色,绿色和蓝色值。我正在使用

int grey = ( red + green + blue ) / 3;
red = grey;
green = grey;
red = grey;

获取位图的灰度版本。

最后,要组成一个新值,将每个部分移动到正确的位置并叠加它们(这恰好与本例中添加它们相同)

int newval = (alpha << 24) | (red << 16) | (green << 8) | blue;

然后设置 argb[i] = newval 来替换像素。

Thanks, I am using your code with some modifications. To answer your question,

Each byte in argb is a 32-bit value representing one pixel in 0xAARRGGBB format:

  • 8 bits alpha (transparency): 0 for transparent and 255 for opaque
  • 8 bits red
  • 8 bits green
  • 8 bits blue

So 0xFFFFFFFF would be opaque white.

The code to extract the values works as follows:
To get the apha value, shift everything right by 24 places leaving only the first 8 bits

int alpha = argb[i] >> 24

To get red, shift right by 16, leaving the top 16 bits. Then blank out the top 8 bits using the AND operator. This leaves the second 8 bits

int red = argb[i] >> 16 & 0xFF

For green, shift by 8 bits and blank out first 16 bits, leaving the third 8 bits

int green = argb[i] >> 8 & 0xFF

And finally for blue, just blank out the top 24 bits

int blue = argb[i] & 0xFF;

At this point you can make changes to the alpha,red,green and blue values. I am using

int grey = ( red + green + blue ) / 3;
red = grey;
green = grey;
red = grey;

To get a grayscale version of the bitmap.

Finally, to make up a new value, shift every part to the correct place and superimpose them (which happens to be the same as adding them in this case)

int newval = (alpha << 24) | (red << 16) | (green << 8) | blue;

Then set argb[i] = newval to replace the pixel.

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