获取图片亮度(亮色,正常,暗色)
程序员文章站
2022-06-16 13:21:20
两种方法获取图片亮度(1)获取图片区域像素点亮度值后求平均值,该算法可以调整public static boolean isDarkBitamp(Bitmap bitmap){ boolean isDark = false; try { if (bitmap != null) { int darkPixelCount = 0; int x = bitmap.getWidth() / 2; ....
两种方法获取图片亮度 (1)获取图片区域像素点亮度值后求平均值,该算法可以调整
public static boolean isDarkBitamp(Bitmap bitmap){
boolean isDark = false;
try {
if (bitmap != null) {
int darkPixelCount = 0;
int x = bitmap.getWidth() / 2;
int y = bitmap.getHeight() / 4;
//取图片0-width/2,竖1个像素点height/4
for (int i = 0; i < y; i++) {
if (bitmap.isRecycled()) {
break;
}
int pixelValue = bitmap.getPixel(x, i);
//取rgb值颜色
if (isDark(Color.red(pixelValue), Color.green(pixelValue), Color.blue(pixelValue))) {
darkPixelCount++;
}
}
isDark = darkPixelCount > y / 2;
Log.i("BlurScreenshotWallpaperUtil", "isDartTheme isDark " + isDark+" darkPixelCount = "+darkPixelCount);
}
}catch (Exception e){
Log.e("BlurScreenshotWallpaperUtil","read wallpaper error");
}
return isDark;
}
//计算像素点亮度算法
private static boolean isDark(int r,int g,int b) {
return !(r * 0.299 + g * 0.578 + b * 0.114 >= 192);
}
(2)通过Palette计算图片颜色亮度
public static boolean isDarkBitamp(Bitmap bitmap){
boolean isDark = false;
try {
if (bitmap != null) {
isDark = getBitmapSamplings(bitmap);
}
}catch (Exception e){
Log.e("BlurScreenshotWallpaperUtil","read wallpaper error");
}
return isDark;
}
public static boolean getBitmapSamplings(Bitmap bitmap){
Palette palette = Palette.from(bitmap)
.setRegion(0, 0, bitmap.getWidth(),bitmap.getHeight())
.clearFilters()
.generate();
return getBitmapPaletteDark(palette);
}
public static boolean getBitmapPaletteDark(Palette hotseatPalette) {
if (hotseatPalette != null && isSuperLight(hotseatPalette)) {
Log.d(TAG,"updateHotseatPalette isSuperLight");
return false;
} else if (hotseatPalette != null && isSuperDark(hotseatPalette)) {
Log.d(TAG,"updateHotseatPalette isSuperDark");
return true;
} else {
Log.d(TAG,"updateHotseatPalette normal");
return true;
}
}
}
public static boolean isSuperLight(Palette p) {
return !isLegibleOnWallpaper(Color.WHITE, p.getSwatches());
}
public static boolean isSuperDark(Palette p) {
return !isLegibleOnWallpaper(Color.BLACK, p.getSwatches());
}
总结:
第二种方法原生使用更加准确
本文地址:https://blog.csdn.net/zhuxingchong/article/details/109635982