Skip to content

Instantly share code, notes, and snippets.

@httnn
Created April 9, 2015 16:23
Show Gist options
  • Select an option

  • Save httnn/b1d772caf76cdc0c11e2 to your computer and use it in GitHub Desktop.

Select an option

Save httnn/b1d772caf76cdc0c11e2 to your computer and use it in GitHub Desktop.
Calculate brightness of Android Bitmap
/*
Calculates the estimated brightness of an Android Bitmap.
pixelSpacing tells how many pixels to skip each pixel. Higher values result in better performance, but a more rough estimate.
When pixelSpacing = 1, the method actually calculates the real average brightness, not an estimate.
This is what the calculateBrightness() shorthand is for.
Do not use values for pixelSpacing that are smaller than 1.
*/
public int calculateBrightnessEstimate(android.graphics.Bitmap bitmap, int pixelSpacing) {
int R = 0; int G = 0; int B = 0;
int height = bitmap.getHeight();
int width = bitmap.getWidth();
int n = 0;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 0; i < pixels.length; i += pixelSpacing) {
int color = pixels[i];
R += Color.red(color);
G += Color.green(color);
B += Color.blue(color);
n++;
}
return (R + B + G) / (n * 3);
}
public int calculateBrightness(android.graphics.Bitmap bitmap) {
calculateBrightnessEstimate(bitmap, 1);
}
@NeighborhoodCoding
Copy link
Copy Markdown

Thanks, it is awesome, but can you provide the calculation of colorfulness or blur? it uses std of R,G,B but implementing in java is very hard...T.T

@KevinAS28
Copy link
Copy Markdown

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment