Search code examples
androidimagebitmappixel

Editing image using double-loop (weight and height)


I want to edit pixels around the middle of the picture and for that i need double loop of x and y each represents the height or weight of the image bitmap. This is an example:

for(x = 0; x < bm.getWeight(); x++)
{
    for(y = 0; y < bm.getHeight(); y++)
    {
        //
    }
}

The problem is that the method getPixles works as an array whose length is the weight*height and then the loop looks like that:

int [] pixels = new int [bm.getWidth() * bm.getHeight()];
for(x = 0; y < pixels.length; x++)
{
    //
}

I hope you guys understand what i am trying to explaing. So is there a way to seperate it to a double loop or get the middle pixels with another way? Thanks a lot ! :)


Solution

  • Try this:

    int width = bm.getWidth();
    int height = bm.getHeight();
    
    int[] pixels = new int[width * height];
    int curPixel;
    
    for (int columnNum = 0; columnNum < width; columnNum++) {
        for (int rowNum = 0; rowNum < height; rowNum++) {
            curPixel = pixels[rowNum * width + columnNum];
            // do stuff with curPixel
        }
    }