I would like to change my textviews color when the wallpaper chosen by the user is too light or too dark acting basically as the latest launchers that for instance if it is set a white wallpaper, change all the textviews to a dark color but I dont know how to detect that.
Help. Thanks in advance.
This calculates the (estimated) brightness of a bitmap image. The argument "skipPixel" defines how many pixels to skip for the brightness calculation, because it might be very runtime intensive to calculate brightness for every single pixel. Higher values result in better performance, but a more estimated result value. When skipPixel equals 1, the method actually calculates the real average brightness, not an estimated one. So "skipPixel" needs to be 1 or bigger ! The function returns a brightness level between 0 and 255, where 0 = totally black and 255 = totally bright. So you have to choose for your own, what "bright" or "dark" means to you.
public int calculateBrightness(android.graphics.Bitmap bitmap, int skipPixel) {
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 += skipPixel) {
int color = pixels[i];
R += Color.red(color);
G += Color.green(color);
B += Color.blue(color);
n++;
}
return (R + B + G) / (n * 3);
}
In order to get the Bitmap (Image) from your device, you can use this code:
final String photoPath = "path to your photo"; // Add photo path here
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);