0

In my Android project, Here is my code.

for (int x = 0; x < targetBitArray.length; x += weight) {
    for (int y = 0; y < targetBitArray[x].length; y += weight) {
        targetBitArray[x][y] = bmp.getPixel(x, y) == mSearchColor;
    }
}

but this code wastes a lot of time.

So I need to find way faster than bitmap.getPixel(). I'm trying to get pixel color using byte array converted from bitmap, but I can't. How to replace Bitmap.getPixel()?

phalanx89
  • 5
  • 3

1 Answers1

3

Each Bitmap.getPixel method invocation requires a lot of resources, so you need to avoid the amount of requests in order to improve the performace of your code.

My suggestion is:

  1. Read the image data row-by-row with Bitmap.getPixels method into a local array
  2. Iterate along your local array

e.g.

int [] rowData= new int [bitmapWidth];
for (int row = 0; row < bitmapHeight; row ++) {
    // Load row of pixels
    bitmap.getPixels(rowData, 0, bitmapWidth, 0, row, bitmapWidth, 1);

    for (int column = 0; column < bitmapWidth; column ++) {
        targetBitArray[column][row] = rowData(column) == mSearchColor;
    }
}

This will be a great improvement for the performace of your code

yopablo
  • 68
  • 6