简体   繁体   English

没有Alpha通道的Android位图像素字节数组

[英]Android bitmap pixels byte array without alpha channel

I'm trying to get an byte array of pixels. 我正在尝试获取像素的字节数组。 I'm using the ARGB_8888 for decodeByteArray function. 我正在使用ARGB_8888的encodeByteArray函数。 The getPixels() or copyPixelsToBuffer(), return a array in RGBA form. getPixels()或copyPixelsToBuffer()返回RGBA形式的数组。 Is it possible to get only RGB from them, without creating a new array and copying bytes that i don't need. 是否有可能仅从它们中获得RGB,而无需创建新的数组和复制我不需要的字节。 I know there is a RGB_565, but it is not optimal for my case where i need a byte per color. 我知道有RGB_565,但是对于我需要每种颜色一个字节的情况,它并不是最佳选择。

Thanks. 谢谢。

Use color=bitmap.getPixel(x,y) to obtain a Color integer at the specified location. 使用color=bitmap.getPixel(x,y)在指定位置获取Color整数。 Next, use the red(color) , green(color) and blue(color) methods from the Color class, which represents each color value in the [0..255] range. 接下来,使用Color类中的red(color)green(color)blue(color)方法,它们表示[0..255]范围内的每个颜色值。

With regards to the alpha channel, one could multiply its ratio into every other color. 关于Alpha通道,可以将其比例乘以其他每种颜色。

Here is an example implementation: 这是一个示例实现:

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    ByteBuffer b = ByteBuffer.allocate(width*height*3);
    for (int y=0;y<height;y++)
        for (int x=0;x<width;x++) {
            int index = (y*width + x)*3;
            int color = bitmap.getPixel(x,y);
            float alpha = (float) Color.alpha(color)/255;
            b.put(index, (byte) round(alpha*Color.red(color)));
            b.put(index+1, (byte) round(alpha*Color.green(color)));
            b.put(index+2, (byte) round(alpha*Color.blue(color)));
        }
    byte[] pixelArray = b.array();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM