RGBA數據int[]轉爲char[],再從char[]轉爲int[]

最近在做一個項目,c讀取RGBA數據,通過socket發送到android socket,android 通過Bitmap工具,生成bitmap圖片。

c讀取RGBA數據是存在int數組中,但是通過socket發送是先將int數組轉爲char數組,android收到char數組,再轉爲int數組,生成圖片,流程如下:

一:c中int數組轉爲char數組

char* changeRGBAToChar(int* data, int pixnum)
{
    if (data == NULL)
        return NULL;
    
    char *str = NULL;
    str = (char*)malloc(pixnum * 4);
    memset(str, 0, pixnum * 4);
    
    for (int i = 0; i < pixnum; i++) {
        str[i * 4] = (char)(data[i] >> 24 & 0xff);
        str[i * 4 + 1] = (char)(data[i] >> 16 & 0xff);
        str[i * 4 + 2] = (char)(data[i] >>  8 & 0xff);
        str[i * 4 + 3] = (char)(data[i]       & 0xff);
    }

    return str;
}

二:anddroid中char數組轉爲int數組,生成bitmap

  private int[] convertByteToColor32(byte[] data) {
        int size = data.length;
        if(size == 0 && size % 4!=0) {
            return null;
        }
        int[] color = new int[size/4];
        for (int i=0; i<color.length; ++i) {
            color[i] = (data[i*4] << 24 & 0xFF000000) |
                    (data[i*4+1] << 16 & 0x00FF0000) |
                    (data[i*4+2] << 168& 0x0000FF00)|
                    (data[i*4+3] & 0x000000FF);
        }
        return color;
    }

// 接着掉方法
 Bitmap.createBitmap(int colors[], int width, int height, Config config);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章