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;
}