Haroon S.
Haroon S.

Reputation: 2613

Java: Mapping 1d array to Bitmap

I have following 2D array that holds color intensity values for RGB channels.

// create a 2d array for colors
int[][] rgbColors = {
            {255, 0, 0}, //red
            {0, 255, 0}, //green
            {0, 0, 255}, //blue
        };

I have another 1D array:

// Data array
int [] data = {0, 0, 0, 1, 1, 1, 2, 2, 2}

I want to reshape the data array into a 3x3 Bitmap Image such that all 0 values are mapped to the red, all 1 values are mapped to the green and all 2 values are mapped to blue colors in the resultant Bitmap. In other words, the resultant Bitmap would have three lines with red, green and blue colors respectively. The color values are given in rgbColors 2D array. What is the most efficient way to achieve this?

Edit:

I tried following but it did not work and produced an empty white image.

bitmap = Bitmap.createBitmap(data, 3, 3, Bitmap.Config.ARGB_8888);

Upvotes: 1

Views: 170

Answers (1)

Islam Hassan
Islam Hassan

Reputation: 712

Reading the documentation(I have no expertise at all in Android), I think that changing the data array to

    int[] data = {-65536,
    -65536,
    -65536,
    -16711936,
    -16711936,
    -16711936,
    -16776961,
    -16776961,
    -16776961
};

will work but you need to read the docs to know the meaning of the parameters.

Upvotes: 2

Related Questions