Search code examples
androidperformanceandroid-ndkandroid-bitmaprenderscript

Faster alternative for getPixel and getPixel in Android Bitmap?


I'm using the methods getPixel and setPixel for my Bitmap and it's so slow (getPixels too). I want to process every pixel of the Bitmap and then create another Bitmap. How can I get access to pixels by RenderScript or using C++? I think they are faster but I don't know how to do .

This is what I do using getPixel / setPixel:

  bitmap.getPixels(colorArray, 0, width, 0, 0, width, height);

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int g = Color.green(colorArray[y * width + x]);
            int b = Color.blue(colorArray[y * width + x]);
            int r = Color.red(colorArray[y * width + x]);
            //some color changes ....
            colorArray[y * width + x] = Color.rgb(r, g, b);
            returnBitmap.setPixel(x, y, colorArray[y * width + x]);
        }
    }

Thanks!


Solution

  • Use getPixels() to get all the pixels, modify the values in the byte[], then call setPixels() to store all the pixels at once. The overhead of calling setPixel() on each individual pixel is killing your performance.

    If it's still not fast enough, you can pass the array to an NDK function.

    If you're doing live processing of Camera images, you can get even fancier.