Forked from brwnx/detect if bitmap is dark or light (android)
Last active
April 24, 2017 03:40
-
-
Save junlincao/f8c0cedd6483f25138de267da9a7828e to your computer and use it in GitHub Desktop.
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
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