Redman
Redman

Reputation: 51

How to convert 2d int array to bitmap in android

I need to convert a 2d integer array (subSrc) to a bitmap. Any solutions?

    private Bitmap decimation(Bitmap src){
     Bitmap dest = Bitmap.createBitmap(
       src.getWidth(), src.getHeight(), src.getConfig());

     int bmWidth = src.getWidth();
     int bmHeight = src.getHeight();`enter code here`

int[][] subSrc = new int[bmWidth/2][bmWidth/2];
       for(int k = 0; k < bmWidth-2; k++){
        for(int l = 0; l < bmHeight-2; l++){
         subSrc[k][l] = src.getPixel(2*k, 2*l); <---- ??

Upvotes: 5

Views: 4746

Answers (3)

alexander zak
alexander zak

Reputation: 939

I looked for a method that received an 2d array (int[][]) and created a Bitmap, and found none, so I wrote one myself:

public static Bitmap bitmapFromArray(int[][] pixels2d){
    int width = pixels2d.length;
    int height = pixels2d[0].length;
    int[] pixels = new int[width * height];
    int pixelsIndex = 0;
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
               pixels[pixelsIndex] = pixels2d[i][j];
               pixelsIndex ++;
        } 
    }
    return Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
}

I also wrote a reverse method:

public static int[][] arrayFromBitmap(Bitmap source){
int width = source.getWidth();
int height = source.getHeight();
int[][] result = new int[width][height];
int[] pixels = new int[width*height];
source.getPixels(pixels, 0, width, 0, 0, width, height);
int pixelsIndex = 0;
for (int i = 0; i < width; i++)
{
    for (int j = 0; j < height; j++)
    {
      result[i][j] =  pixels[pixelsIndex];
      pixelsIndex++;
    }
}
return result;
}

I hope you find it useful!

Upvotes: 11

Tony K.
Tony K.

Reputation: 5605

So, you are essentially trying to pull pixels out, do something to them, then make a Bitmap as a result?

The routines expect the pixels to be in a single-dimensional array, so you'll want to put them into the array more like this:

int data[] = new int[size]; 
data[x + width*y] = pixel(x,y);
...

Then use Bitmap.createBitmap() that accepts the single-dimensional array. You'll want to use the Bitmap.Config for ARGB in your example, since you're using b.getPixel(x,y) which always returns a color in ARGB format.

Bitmap result = Bitmap.createBitmap(data, width, height, Bitmap.Config.ARGB_8888);

Upvotes: 0

Sudar Nimalan
Sudar Nimalan

Reputation: 3992

you can use setPixel(int, int, int) or setPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height) methos od bitmap class.

     Bitmap dest = Bitmap.createBitmap(
       src.getWidth()/2, src.getHeight()/2, src.getConfig());

     int bmWidth = src.getWidth();
     int bmHeight = src.getHeight();


       for(int k = 0; k < bmWidth/2; k++){
        for(int l = 0; l < bmHeight/2; l++){
         dest.setPixel(k,l,src.getPixel(2*k, 2*l));

But this will be slower i think.

for the 2nd method you uhave to do something like this

int subSrc = new int[(bmWidth/2*)(bmHeight/2)];
       for(int k = 0; k < bmWidth-2; k++){
         subSrc[k] = src.getPixel(2*(k/bmWidth), 2*(k%bmHeight)); <---- ??

Upvotes: 0

Related Questions