Created
April 9, 2015 16:23
-
-
Save httnn/b1d772caf76cdc0c11e2 to your computer and use it in GitHub Desktop.
Calculate brightness of Android Bitmap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice