java 调节color亮度算法_获取图片亮度(亮色,正常,暗色)

本文介绍了两种Java中用于检测图片亮度的方法:一种是通过计算图片区域像素点亮度平均值,另一种是利用Palette库分析图片颜色亮度。文章指出,利用Palette的方法更为准确,并提供了相关代码实现。
摘要由CSDN通过智能技术生成

两种方法获取图片亮度

(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

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值