简体   繁体   中英

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. The getPixels() or copyPixelsToBuffer(), return a array in RGBA form. Is it possible to get only RGB from them, without creating a new array and copying bytes that i don't need. I know there is a RGB_565, but it is not optimal for my case where i need a byte per color.

Thanks.

Use color=bitmap.getPixel(x,y) to obtain a Color integer at the specified location. 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.

With regards to the alpha channel, one could multiply its ratio into every other color.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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