1

I am trying to get RGB from bitmap with following code.

My end goal is to get continuously capture the screen and convert it to rob. following code works fine but it is very slow.

is there any way to make it faster ?

Any help would be appreciated.

//BTORGB
private byte[] rgbValuesFromBitmap(Bitmap bitmap)
{
    ColorMatrix colorMatrix = new ColorMatrix();
    ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
    Bitmap argbBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                                            Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(argbBitmap);
    Paint paint = new Paint();
    paint.setColorFilter(colorFilter);
    canvas.drawBitmap(bitmap, 0, 0, paint);

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int componentsPerPixel = 4;
    int totalPixels = width * height;
    int totalBytes = totalPixels * componentsPerPixel;

    byte[] rgbValues = new byte[totalBytes];
    @ColorInt int[] argbPixels = new int[totalPixels];
    bitmap.getPixels(argbPixels, 0, width, 0, 0, width, height);
    for (int i = 0; i < totalPixels; i++) {
        @ColorInt int argbPixel = argbPixels[i];
        int red = Color.red(argbPixel);
        int green = Color.green(argbPixel);
        int blue = Color.blue(argbPixel);
        int alpha = Color.alpha(argbPixel);

        rgbValues[i * componentsPerPixel + 0] = (byte) red;
        rgbValues[i * componentsPerPixel + 1] = (byte) green;
        rgbValues[i * componentsPerPixel + 2] = (byte) blue;
        //            rgbValues[i * componentsPerPixel + 3] = (byte) alpha;
    }

    return rgbValues;
}
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
Plutomen DEV
  • 177
  • 2
  • 9
  • 1
    If your goal is the get the dominant or least dominant color or special colors im the image, check out the Palette library in android – Ahmad Sattout Apr 22 '19 at 14:48
  • 1
    @AhmadSattout Thanks for comment. Will it helpful to get byte array of pixels like `byte[] pixels = new byte[w * h * 4];` from bitmap ? – Plutomen DEV Apr 23 '19 at 04:49
  • @AhmadSattout issue is because of loop of width and height of pixel make it slow. i.e `totalPixels` – Plutomen DEV Apr 23 '19 at 04:50
  • First, you don't want a single dimension array, you want a 2D array cause it's both width and height. You can't get rid of the loop, only thing you can do is optimize what's within it. – Ahmad Sattout Apr 23 '19 at 14:33

0 Answers0