Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save junlincao/f8c0cedd6483f25138de267da9a7828e to your computer and use it in GitHub Desktop.
Save junlincao/f8c0cedd6483f25138de267da9a7828e to your computer and use it in GitHub Desktop.
public static boolean isDark(Bitmap bitmap) {
final float darkThreshold = bitmap.getWidth() * bitmap.getHeight() * 0.45f;
final int w = bitmap.getWidth();
final int h = bitmap.getHeight();
int darkPixels = 0;
int[] lineColor = new int[w];
for (int i = 0; i < h - 1; i++) {
bitmap.getPixels(lineColor, 0, w, 0, i, w, 1);
for (int color : lineColor) {
final int r = Color.red(color);
final int g = Color.green(color);
final int b = Color.blue(color);
final float luminance = (0.299f * r + 0.0f + 0.587f * g + 0.0f + 0.114f * b + 0.0f);
if (luminance < 150) {
darkPixels++;
}
}
}
return darkPixels >= darkThreshold;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment