Android- convert ARGB_8888 bitmap to 3BYTE_BGR

后端 未结 3 1375
广开言路
广开言路 2021-01-14 08:03

I get the pixel data of my ARGB_8888 bitmap by doing this:

public void getImagePixels(byte[] pixels, Bitmap image) {
    // calculate how many bytes our imag         


        
3条回答
  •  情歌与酒
    2021-01-14 08:42

    Disclaimer: There could be better/easier/faster ways of doing this, using the Android Bitmap API, but I'm not familiar with it. If you want to go down the direction you started, here's your code modified to convert 4 byte ARGB to 3 byte BGR

    public byte[] getImagePixels(Bitmap image) {
        // calculate how many bytes our image consists of
        int bytes = image.getByteCount();
    
        ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
        image.copyPixelsToBuffer(buffer); // Move the byte data to the buffer
    
        byte[] temp = buffer.array(); // Get the underlying array containing the data.
    
        byte[] pixels = new byte[(temp.length / 4) * 3]; // Allocate for 3 byte BGR
    
        // Copy pixels into place
        for (int i = 0; i < (temp.length / 4); i++) {
           pixels[i * 3] = temp[i * 4 + 3];     // B
           pixels[i * 3 + 1] = temp[i * 4 + 2]; // G
           pixels[i * 3 + 2] = temp[i * 4 + 1]; // R
    
           // Alpha is discarded
        }
    
        return pixels;
    }
    

提交回复
热议问题