Skip to content

Instantly share code, notes, and snippets.

@brwnx
Created September 2, 2014 09:58
Show Gist options
  • Save brwnx/191c79b6c2b3befbfc7d to your computer and use it in GitHub Desktop.
Save brwnx/191c79b6c2b3befbfc7d to your computer and use it in GitHub Desktop.
public static boolean isDark(Bitmap bitmap){
boolean dark=false;
float darkThreshold = bitmap.getWidth()*bitmap.getHeight()*0.45f;
int darkPixels=0;
int[] pixels = new int[bitmap.getWidth()*bitmap.getHeight()];
bitmap.getPixels(pixels,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());
for(int pixel : pixels){
int color = pixels[i];
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
double luminance = (0.299*r+0.0f + 0.587*g+0.0f + 0.114*b+0.0f);
if (luminance<150) {
darkPixels++;
}
}
if (darkPixels >= darkThreshold) {
dark = true;
}
long duration = System.currentTimeMillis()-s;
return dark;
}
@ridoy-ai
Copy link

ridoy-ai commented Sep 26, 2022

That loop part can be optimized as-

for (int color : pixels) {
            int r = Color.red(color);
            int g = Color.green(color);
            int b = Color.blue(color);
            ...

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